Java Bitwise Operators and Examples

Bitwise operators are Bitwise AND (&), Bitwise OR (|), Bitwise XOR (^) and Bitwise Compliment (~) Operators. In this tutorial, Left Shift (<<), Right Shift (>>) and Zero Fill Right Shift (>>>) opearators are also exemplified.

Java Bitwise Operators and Examples

Bitwise OR, Bitwise AND and Bitwise XOR

Example: These operators returns bit by bit OR, AND, XOR of input values.

a = 7 = 0111

b = 3 = 0011

----------------

Bitwise OR
0111
0011
------------
0111 = 7


Bitwise AND
0111
0011
------------
0011 = 3

Bitwise XOR
0111
0011
------------
0100 = 4

Bitwise Complement Operator is a unary operator and used to inverse the bits.

Example: a=3

a = 0011

~   1100