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
#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;
}
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