Java - Operator Bitwise Operators

What is Bitwise Operators?

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.

OperatorsMeaning Type UsageResult
&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 shiftBinary 25 >>> 2 6
&=, !=, ^=, <<=, >>=, >>>= Compound assignment Bitwise operators Binary N/A N/A

Compound bitwise assignment operator

A compound bitwise assignment operator is used in the following form:

operand1 op= operand2
  • op is one of the bitwise operators of &, |, ^, <<, >>, and >>>.
  • operand1 and operand2 are of primitive integral data type
  • operand1 must be a variable.

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