in this short tutorial we see the basic use of comparing and working with data at bit level. Here we check for odd and even numbers by comparing them at the bit level.
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter a number to check divisibility");
scanf("%d",&a);
if (a&1)
{
printf("\nOdd");
}
else
printf("\nEven");
}
Here the number we enter is checked with the binary of one at the bit level.thus any number divisible with 2 will have 0 at the first place and 1 if it is odd. The & operation returns a TRUE to the if statement if the condition is true and the number is odd else not.