Bitwise operators work on the individual bits of an integral value. In Java they accept byte, short, char, int and long, and they are rejected on boolean, float and double.
| Operator | Name | Operands | Result bit is 1 when |
|---|---|---|---|
& | Bitwise AND | Two | Both bits are 1 |
| | Bitwise OR | Two | At least one bit is 1 |
^ | Bitwise XOR | Two | The two bits differ |
~ | Bitwise complement | One | Unary, flips every bit |
<< | Left shift | Two | Unary family, moves bits toward the high end |
>> | Arithmetic right shift | Two | Copies the sign bit into the vacated bits |
>>> | Logical right shift | Two | Fills the vacated bits with zeros |
The single most common mistake is writing & or | when you meant && or ||. They look similar and behave very differently, as section 10 shows. There is also no ~ for logical negation; that is !.