These operators are used for the manipulation of data at the bit level. We can’t apply these operators with floating point data types.
Operator | Meaning | Description |
---|---|---|
& | Bit-wise AND | For example, we have x = 2, and y = 3;(In binary x = 0000 0010 and y = 0000 0011), then x&y = 2(0000 0010)(representing only in 8-bits) The AND operation is carried out between consecutive bits. |
| | Bit-wise OR | For example, we have x = 2, and y = 3;(In binary x = 0000 0010 and y = 0000 0011), then x|y = 3(0000 0011) |
^ | Bit-wise XOR | For example, we have x = 2, and y = 3;(In binary x = 0000 0010 and y = 0000 0011), then x^y = 1(0000 0001) |
<< | Bit-wise LEFT SHIFT | For example, we have x = 2, and y = 3;(In binary x = 0000 0010), then x<<2 = 8(0000 1000) |
>> | Bit-wise RIGHT SHIFT | For example, we have x = 2, and y = 3;(In binary x = 0000 0010), then x>>1 = 1(0000 0001) |
Example:
/*
Bit-wise operators
*/
#include <stdio.h>
int main(){
int x = 2, y = 3;
printf("x&y = %d\n", x&y);
printf("x|y = %d\n", x|y);
printf("x^y = %d\n", x^y);
printf("x<<2 = %d\n", x<<2);
printf("x>>1 = %d\n", x>>1);
return 0;
}
Expected Output:
x&y = 2
x|y = 3
x^y = 1
x<<2 = 8
x>>1 = 1