Bitwise operators perform operations on the bits of their operands. The operands can only be byte, short, int, long or char data types. For example, if an operand is the number 48, the bitwise operator will perform its operation on the binary representation of 48 (i.e., 110000).
There are three binary logical operators:
& performs a logical AND operation.
| performs a logical OR operation.
^ performs a logical XOR operation.
There are three bit shift operators:
>> the signed left shift operator shifts the bits of a binary number to the left.
<< the signed right shift operator shifts the bits of a binary number to the right.
>>> the unsigned right shift operator shifts the bits of a binary number to the right, filling the left most bit with a zero.
And finally the unary operator:
~ the bitwise complement operator inverts the bits of a binary number.
Examples:
The numbers 16 and 15 can be represented in binary by the 5 bit numbers 10000 and 01111 respectively: int sixteen = 16;
int fifteen = 15;
//display binary number for sixteen
System.out.println(Integer.toBinaryString(sixteen));
//display binary number for fifteen
System.out.println(Integer.toBinaryString(fifteen));
If a logical AND operation is performed on the two integers, the result will be zero:
//perform a logical AND operation on the numbers sixteen and fifteen System.out.println(sixteen & fifteen);
The reason the result is zero is because the logical AND operation returns a 1 if both its operands are 1, otherwise it returns a 0. The & operator performs the logical AND operation on each corresponding bit in each number (i.e., the first bit in each number, the second bit in each number and so on). In the case of the bit patterns for the numbers 16 and 15 the result is 00000.

