 Bitwise Operators In C Language
Bitwise operators are used to perform operations at binary level. Decimal values are converted into binary values these operators are used for testing the bits, or shifting them right or left. These operators are not applicable to float or double. Following are the Bitwise operators with their meanings.
Operator Meaning
& Bitwise AND
| Bitwise OR
^ Bitwise Exclusive – OR
<< Left Shift
>> Right Shift
~ Complement

Consider x=40 and y=80, binary form of these values are

x=00101000
y=01010000
x&y=00000000=0
x|y=01111000=120
x^y=01111000=120

x y x&y x/y x^y
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0

X<<1 means that the bits will be left shifted by one place, if we use x<<2 then ie means that the bits will be left shifted by 2 places

X<<1=01010000=80
x>>1=00010100=20

Program : A program to illustrate the use of Bitwise Operators
``````#include <stdio.h>
int main()
{
int num=212,i;
for (i=0;i<=2;++i)
printf(“Right shift by %d: %d\n “, i, num>>i);
printf(“\n”);
for (i=0;i<=2;++i)
printf(“Left shift by %d: %d\n” , i ,num<<i);
return 0;
}
``````
Output :

Right shift by 0:212
Right shift by 1:106
Right shift by 2:53

Left shift by 0:212
Left shift by 1:424
Left shift by 2:848