Bitwise AND

AND operation returns 1, if the both the bits are 1, else in any case, it returns 0.

int a = 10&41;

Binary representation of 10 is - 00000000 00000000 00000000 00001010

Binary representation of 41 is - 00000000 00000000 00000000 00101001

------------------------------------------------------

AND of 10 and 41 is 00000000 00000000 00000000 00001000

And, this represents 8.

Bitwise OR

OR operation returns 1, if any of the bit is 1.

int a = 10|41;

Binary representation of 10 is - 00000000 00000000 00000000 00001010

Binary representation of 41 is - 00000000 00000000 00000000 00101001

------------------------------------------------------

OR of 10 and 41 is 00000000 00000000 00000000 00101011

And, this represents 43.

Bitwise XOR

XOR returns 1 if exactly one bit in 2 bits is 1

int a = 10^41;

Binary representation of 10 is - 00000000 00000000 00000000 00001010

Binary representation of 41 is - 00000000 00000000 00000000 00101001

------------------------------------------------------

OR of 10 and 41 is 00000000 00000000 00000000 00100011

And, this represents 35.

Shift Left

Shift left operator shifts every bit of the input to the right.

int a = 10<<2;

This means, every bit of 10 will be shifted to left by 2 bits and the empty spaces will be replaced by 0.

Binary representation of 10 is - 00000000 00000000 00000000 00001010

Therefore, 10<

Which is actually 40 in Integer representation.

Shift Right (Also known as Signed Shift)

Shift left operator shifts every bit of the input to the right.

int a = 10>>2;

This means, every bit of 10 will be shifted to right by 2 bits.

Binary representation of 10 is - 00000000 00000000 00000000 00001010

Therefore, 10>>2 is - 00000000 00000000 00000000 00000010

Which is actually 2 in Integer representation.

Here the interesting point to note is that, this shift preserves the sign of the input. Thus, as 10 is a positive integer, it preserves the positive sign. Now, let us take an example of a negative number.

Binary representation of -10 is – 11111111 11111111 11111111 11110110

Therefore, -10>>2 is - 11111111 11111111 11111111 11111101

The bits are shifted to the right, but the empty spaces created in the left side are replaced by 1, to preserve the sign of the input, which is negative.

The result is -3 in Integer representation.

Shift right ZERO (Unsigned Shift)

This difference or >>> operator to >> operator is that: >>> doesn’t preserve the sign of the input. It, replaces the empty places created of the left side while shifting with zeros.