Javatpoint Logo
Javatpoint Logo

Types of Bitwise Operators in Java

In Java, bitwise operators are used to execute binary number bit-level operations. These operators alter the bits in a number by performing operations like bit shifting, AND, OR, NOT, and XOR. With examples and programs, we will go over the various types of bitwise operators accessible in Java.

Java Bitwise Operators

1. AND (&)

The AND operator combines two integers in a bitwise manner. If the relevant bits in the two operands are set to 1, it returns a new number with each bit set to 1. Otherwise, the bit is set to 0.

In this example, the binary value of an is 0011, whereas the binary value of b is 0110. The binary value of 0010, equivalent to 2 in decimal, is obtained by bitwise ANDing these two values.

2. OR (|)

The OR operator performs a bitwise OR operation on two numbers. It returns a new number with each bit set to 1 if either of the two operands' corresponding bits is set to 1. If not, the bit is set to 0.

In this example, the binary value of an is 0011, whereas the binary value of b is 0110. The bitwise OR of these two values yields the binary value 0111, equivalent to 7 in decimal.

3. XOR (^)

On two numbers, the XOR operator performs a bitwise exclusive OR operation. If just one of the two operands' corresponding bits is set to 1, it returns a new integer with each bit set to 1. Otherwise, the bit is set to 0.

In this example, the binary value of an is 0011, whereas the binary value of b is 0110. The binary value of 0101, equivalent to 5 in decimal, is obtained by performing a bitwise XOR operation on these two values.

4. The NOT operator (~)

The NOT operator performs a bitwise NOT operation on a single integer. It produces a new number with each bit reversed. Each 0 is replaced by a 1, and each one by a 0.

Operation of the NOT operator on this number results in the binary value of 11111111111111111111111111111100, which is equal to -4 in decimal. Java employs two's complement encoding for signed numbers, and the number's sign is represented by the leftmost bit. The leftmost bit in this example is 1, indicating a negative value.

5. Left Shift (<<)

The left shift operator moves a number's bits to the left by a given number of positions. The 0s occupy the empty spots on the right. This procedure multiplies the number by two to the power of the number of locations supplied.

6. Right Shift (>>)

The right shift operator moves the bits of an integer to the right by a given number of places. The vacant spaces on the left are filled by the sign bit (0 for positive numbers, 1 for negative values). The integer is divided by 2 to the power of the number of slots specified in this operation.

In this example, the binary value of an is 11111111111111111111111111111000. The right shift operation of a by two places results in the binary value of 11111111111111111111111111111110, which is equivalent to -2 in decimal.

7. The unsigned right shift operator (>>>)

It moves the bits of an integer to the right by several places. The 0s occupy the empty spots on the left. This procedure divides the integer by 2 to the power of the number of spots supplied, eliminating any remainders.

BitwiseOperatorsDemo.java

Output:

a & b = 2
a | b = 7
a ^ b = 5
~a = -4
a << 2 = 12
h >>> 2 = 1073741822

Explanation

We declared numerous integer variables and performed bitwise operations in this program. The bitwise AND, OR, and XOR operations are performed by the &, |, and operators, respectively. The operator carries out the bitwise NOT operation. The operator performs the left shift operation, and the >>> operator performs the unsigned right shift operation.

We used the System.out.println() function to print the results of these actions. As you can see, the output of each procedure corresponds to the predicted outcome.

Conclusion

Finally, bitwise operators are essential to programming in Java and other programming languages. They make it easy and quick to execute bitwise operations on integers and other primitive data types.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA