An operator that acts on individual bits (0 or 1) of the operands is called bitwise operator in java.
It acts only integer data types such as byte, short, int, and long. Bitwise operators in java cannot be applied to float and double data types.
The internal representation of numbers in the case of bitwise operators is represented by the binary number system. Binary number is represented by two digits 0 or 1.
Therefore, these operators are mainly used to modify bit patterns (binary representation).
Types of Bitwise Operators in Java
In Java, there are seven types of bitwise operators. They are listed in the below table form.
Table: Bitwise Operators
|1. &||bitwise AND|
|2. |||bitwise OR|
|3. ^||bitwise exclusive OR|
|4. ~||one’s complement (unary)|
|5. <<||shift left|
|6. >>||shift right|
|7. >>>||unsigned right shift|
The classification of bitwise operators in java can also be seen in the below figure.
Bitwise AND Operator (&) in Java
This operator is used to perform bitwise AND operation between two integral operands. The AND operator is represented by a symbol & which is called ampersand. It compares each bit of the left operand with the corresponding bit of right operand.
Let’s consider the truth table given in the figure to understand the operation of AND operator.
Truth table is a table that gives the relationship between inputs and output. In AND operation, on multiplying two or more input bits, we get output bit.
From the truth table shown in figure, if both compared input bits are 1, we get output 1. Otherwise, we get output 0.
From the truth table, On multiplying the individual bits of x and y, we get x & y = 0 0 0 0 1 0 1 0. It is nothing but 10 in decimal form.
Bitwise OR Operator ( | ) in Java
This operator is used to perform OR operation on bits of numbers. It is represented by a symbol | called pipe symbol.
In OR operation, each bit of first operand (number) is compared with the corresponding bit of the second operand.
To understand OR operation, consider truth table given in the above figure. If both compared bits are 0, the output is 0. If any one bit is 1 in both bits, the output is 1.
On adding input bits of (x = 20) and (y = 10), we get output bits 0 0 0 0 1 1 1 1. This is nothing but 15 in decimal form.
Bitwise Exclusive OR (XOR) Operator (^)
The operator ^ performs exclusive OR (XOR) operation on the bits of numbers. This operator compares each bit first operand (number) with the corresponding bit of the second operand.
Exclusive OR operator is represented by a symbol ^ called cap. Let us consider the truth table of the XOR operator to understand exclusive OR operation.
To understand exclusive OR (XOR) operation, consider the truth table as shown in the above figure. If we have odd number of 1’s in input bits, we get output bit as 1. In other words, if two bits have the same value, the output is 0, otherwise, the output is 1.
From the truth table, when we get odd number of 1’s then notice that the output is 1 otherwise the output is 0. Hence, x ^ y = 0 0 0 0 1 1 1 1 is nothing but 15 in decimal form.
Hope that this tutorial has covered almost all important points related to the bitwise operator in java with suitable examples. I hope that you will have understood this topic and enjoyed it.
Thanks for reading!!!
Next ⇒ Shift Operator in Java⇐ PrevNext ⇒