Bitwise Operators : AND,OR,XOR

Bitwise Operators : AND,OR,XOR

AND,OR,XOR are three main Bitwise Operators in C Programming Language.AND Operator is used to mask particular Bits.Consider following table -

Input Bits AND OR XOR
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0

Example : Consider two numbers 12 and 10

a = 12
b = 10
---------------------------------
a in Binary : 0000 0000 0000 1100
b in Binary : 0000 0000 0000 1010
---------------------------------
a | b       : 0000 0000 0000 1110
a & b       : 0000 0000 0000 1000
a ^ b       : 0000 0000 0000 0110
---------------------------------

Rules from above table :

  1. If Two bits are same Then Resultant XOR is 0 .
  2. If Two bits are different Then Resultant XOR is 1.
  3. If any of the bit is 1 then Resultant OR is 1
  4. If both bits are 0 then Resultant OR is 0
  5. If any of the bit is 0 then Resultant AND is 0.

Conclusion :

a | b       : 0000 0000 0000 1110   = 14
a & b       : 0000 0000 0000 1000   = 8
a ^ b       : 0000 0000 0000 0110   = 6

Live Example : Bitwise Operator (AND,OR,XOR)

#include<stdio.h>
int main()
{
int a=12,b=10;
printf("\nNumber1 AND Number2 : %d",a & b);
printf("\nNumber  OR  Number2 : %d",a | b);
printf("\nNumber  XOR Number2 : %d",a ^ b);
return(0);
}

Output :

Number1 AND Number2 : 8
Number  OR  Number2 : 14
Number  XOR Number2 : 6