A bitwise operator manipulates individual bits of its operands.
All bitwise operators work with only integers.
Java Bitwise operators are listed in the following table.
Operators | Meaning | Type | Usage | Result |
---|---|---|---|---|
& | Bitwise AND | Binary | 25 & 24 | 24 |
| | Bitwise OR | Binary | 25 | 2 | 27 |
^ | Bitwise XOR | Binary | 25 ^ 2 | 27 |
~ | Bitwise complement (1's complement) | Unary | ~25 | -26 |
<< | Left shift | Binary | 25 << 2 | 100 |
>> | Signed right shift | Binary | 25 >> 2 | 6 |
>>> | Unsigned right shift | Binary | 25 >>> 2 | 6 |
&=, !=, ^=, <<=, >>=, >>>= | Compound assignment Bitwise operators | Binary | N/A | N/A |
A compound bitwise assignment operator is used in the following form:
operand1 op= operand2
The above expression is equivalent to the following expression:
operand1 = (Type of operand1) (operand1 op operand2)
The following table lists the equivalent expression for compound bitwise assignment operators.
Expression | is equivalent to |
---|---|
i &= j | i = i & j |
i |= j | i = i | j |
i ^= j | i = i ^ j |
i <<= j | i = i << j |
i >>= j | i = i >> j |
i >>>= j | i = i >>> j |