C Program to check whether a Number is Even or Odd using Bitwise Operations (Without if - else statements)
// Program by Akash Tripathi (@proakash256)
#include <stdio.h>
int main()
{
int a , b;
printf("Enter two Numbers :\n");
scanf("%d%d" , &a , &b);
printf("\nThe values of a and b are %d
and %d\n\n" , a , b);
// if (Number & 1) returns 1 , then it is Odd
// if (Number & 1) returns 0 , then it is Even
printf("The value of %d BITWISE AND 1 is :
%d\n" , a , a & 1);
printf("The value of %d BITWISE AND 1 is :
%d\n\n" , b , b & 1);
// if (Number | 1) returns (Number + 1) ,
// then it is Even
// if (Number | 1) returns (Number) ,
// then it is Odd
printf("The value of %d BITWISE OR 1 is :
%d\n" , a , a | 1);
printf("The value of %d BITWISE OR 1 is :
%d\n\n" , b , b | 1);
// if (Number ^ 1) returns (Number + 1) ,
// then it is Even
// if (Number ^ 1) returns (Number - 1) ,
// then it is Odd
printf("The value of %d BITWISE XOR 1 is :
%d\n" , a , a ^ 1);
printf("The value of %d BITWISE XOR 1 is :
%d\n\n" , b , b ^ 1);
printf("The value of BITWISE NOT %d is :
%d\n" , a , ~ a);
printf("The value of BITWISE NOT %d is :
%d\n\n" , b , ~ b);
// BITWISE RIGHT SHIFT 1 returns (Number / 2)
printf("The value of %d BITWISE RIGHT SHIFT 1
is : %d\n" , a , a >> 1);
printf("The value of %d BITWISE RIGHT SHIFT 1
is : %d\n\n" , b , b >> 1);
// BITWISE LEFT SHIFT 1 returns (Number * 2)
printf("The value of %d BITWISE LEFT SHIFT 1
is : %d\n" , a , a << 1);
printf("The value of %d BITWISE LEFT SHIFT 1
is : %d\n\n" , b , b << 1);
return 0;
}
Comments
Post a Comment