C Program to find Number Even or Odd using Bitwise AND ( & ) Operator
C Program to understand the usage of Bitwise Operator & .
(Or)
Example c program for Bitwise operator & .
PROGRAM:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#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
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