Bitwise Operators


Java bitwise operators perform the bitwise operation on single or multiple operands and return the result.

Bit operators work on 32 bits numbers.

1. & (Bitwise AND)

System.out.print(5 == 10 & 15 == 15);

// prints false

2. | (Bitwise OR)

System.out.print(5 == 10 | 15 == 15);

// prints true

3. ^ (Bitwise XOR)

System.out.print(5 == 10 ^ 15 == 15);

// prints true

4. ~ (Bitwise NOT)

System.out.print(~5);

// prints -5

5. << (Bitwise Left Shift)

System.out.print(2 << 1);

// prints 4

6. >> (Bitwise Right Shift)

System.out.print(2 >> 1);

// prints 1

7. >>> (Bitwise Right Shift with zero)

System.out.print(2 >>> 1);

// prints 1