Bitwise operators work on binary digits or bits of input values. We can apply these to the integer types – long, int, short, char, and byte.

Bitwise operators work on a binary equivalent of decimal numbers and perform operations on them bit by bit as per the given operator:

Bitwise Logical Operators

Bitwise OR (|)

The OR operator compares each binary digit of two integers and gives back 1 if either of them is 1.

int result = 6 | 5;
0110
0101
-----
0111
result : 7

Here, we can see that using OR, 0 and 0 will result in 0, while any combination with at least a 1 will result in 1.

Bitwise AND (&)

The AND operator compares each binary digit of two integers and gives back 1 if both are 1, otherwise it returns 0.

int result = 6 & 5;
0110
0101
-----
0100
result : 4

Bitwise XOR (^)

The XOR operator compares each binary digit of two integers and gives back 1 if both the compared bits are different