C Program to understand the usage of Bitwise Operator & .
(Or)
Example c program for Bitwise operator & .
PROGRAM:
#include<stdio.h>
#include<conio.h>
main()
{
 int n,i;
 printf("\n Enter a Integer Number to find Even or Odd:");
 scanf("%d",&n);
 i=!(n&1);  // & is a bitwise and operator
 if ((i))
   printf("\n The Entered Number Is Even");
 else
   printf("\n The Entered Number is Odd");
 getch();
 return(0);
}

 

Sample Output: (using GNU GCC Compiler with Code Blocks IDE)
1) Enter a Integer Number to find Even or Odd: 5
The Entered Number is Odd
2) Enter a Integer Number to find Even or Odd:
The Entered Number Is Even
Explanation :
      n       1     (n&1)     !(n&1)
n=2  0010   0001    0000      returns 1 ( if statment executes)    
n=3  0011   0001    0001      returns 0 ( else statment executes)
Truth Table:
    p    q     p&q
    0    0      0
    0    1      0
    1    0      0
    1    1      1

Leave a Reply

Your email address will not be published.