BITWISE OPERATOR In processor, mathematical operations like: addition, subtraction, addition and division are done in bit-level to process faster and save power. To perform bit-level operations in C programming, bitwise operators are used.

Operators Meaning of operators

& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right

Bitwise AND operator &

The output of bitwise AND is 1 if the corresponding bits of all operands is 1. If either bit of an operand is 0, the result of corresponding bit is evaluated to 0.

Let us suppose the bitwise AND operation of two integers 12 and 25.

12 = 00001100 (In Binary)
25 = 00011001 (In Binary)

Bit Operation of 12 and 25
00001100
& 00011001
________
00001000 = 8 (In decimal)

Example #1: Bitwise AND
#include
int main()
{
int a = 12, b = 25;
printf("Output = %d", a&b);
return 0;
}

Output
Output = 8
Bitwise OR operator |
The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. In C Programming, bitwise OR operator is denoted by |.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)

Bitwise OR Operation of 12 and 25
00001100
| 00011001
________
00011101 = 29 (In decimal)

Example #2: Bitwise OR
#include
int main()
{
int a = 12, b = 25;
printf("Output = %d", a|b);
return 0;
}

Output
Output = 29

Bitwise XOR (exclusive OR) operator ^
The result of bitwise XOR operator is 1 if the corresponding bits of two operands are opposite. It is denoted by ^.
12 = 00001100 (In Binary)
25 = 00011001 (In Binary)
Bitwise XOR Operation of 12 and 25
00001100
| 00011001
________
00010101 = 21 (In decimal)

Example #3: Bitwise XOR
#include
int main()
{
int a = 12, b = 25;
printf("Output = %d", a^b);
return 0;
}

Output
Output = 21
Bitwise complement operator ~
Bitwise compliment operator is an unary operator (works on one operand only). It changes the 1 to 0 and 0 to 1. It is denoted by ~.

35=00100011 (In Binary)

Bitwise complement Operation of 35
~ 00100011
________
11011100 = 220 (In decimal)

Bitwise And(&):-
int a=11,b=6,c;
c=a&b; c=2
a=1011
b=0110
c=0010

Bitwise or (|):-
int a=11,b=6,c;
c=a|b; c=20
a=1011
b=0110
c=1111

Bitwise right shift operator:-
int a=11,c; c=a>>1; a=1011
c=0101
c=5;

Bitwise left shift operator:-
int a=11,c; c=a<<1; a=1011
c=10110
c=22;