Bitwise Operators in Java
In the previous blog we learned about the Operators in Java. If you want to know more about the operators visit Operators in Java. In this blog, we will go through the remaining operator from the last blog i.e. Bitwise Operator. Java provides bitwise operators used to perform bitwise operations on integer values. These operators are rarely used. The intent to explain these operators is to make you aware that these operators exist. These operators directly work on bits, meaning that for getting individual bits in numbers we can use masking techniques. Following are the bitwise operators:
Bitwise Operators | Description |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise Complement |
<< | Bitwise Left Shift |
>> | Bitwise Right Shift |
>>> | Unsigned Right Shift |
Bitwise AND(&)
e.g.
Output:
It can be calculated as
So it gives the value 64 after multiplying the two hexadecimal numbers.
Bitwise OR(|)
e.g.
Output:
It can be calculated as:
So we get the value 223 after doing OR operation on two numbers.
Bitwise XOR(^)
e.g.
Output:
It can be calculated as:
We get 159 after calculating the two numbers.
Bitwise NOT(~)
Bitwise ~ is used to perform NOT operation. Bitwise ~ revere the values of the bits.
e.g.
Bitwise Left Shift(<<)
e.g.
Output:
It can be calculated as:
Bitwise Right Shift(>>)
e.g.
Output:
It can be calculated as:
Unsigned Right Shift(>>>)
e.g.
Output:
It can be calculated as: