## How bitwise operators work in Java

This section presents the syntax and working of each bitwise operator in Java.

**Bitwise AND(&): **This operator is related to the logical AND operation of Java. However, the bitwise AND operator compares the bits of operands and returns 1 if both numbers have 1 in the same place. If the bits at the same location have other than the 1,1 combination, then it will replace 0 in the resulting bit. The syntax provided below helps in applying bitwise AND operation in Java.

**Bitwise OR(|): **This operator compares bits of operands and returns 1 if the operands have other than 0,0 bits in place. The bitwise OR is practiced using the following syntax:

**Bitwise XOR(^): **This works differently from bitwise OR and bitwise AND. It returns 1 if both operands have different bits and it returns 0 in case of the same bits of operands. The following syntax must be followed to exercise the bitwise XOR:

**Bitwise complement(~): **Bitwise complement(~). This refers to changing the bits of a number from 0 to 1 and 1 to 0. The syntax provided below must be followed to apply the bitwise complement operator:

**Note: **The Java compiler takes the 2’s complement to use the bitwise complement operator.

**Bitwise left shift operator(<<): **This operator shifts the bit of number to the left by a specific number. The following syntax must be followed to apply the left shift operator:

**Bitwise right shift operator(>>): **The bitwise right shift operator shifts the binary of a number to the right side and fills the vacant space with a signed bit (the most significant bit which is at the leftmost position of the binary number). To use the right shift operator, the following syntax is provided:

**Bitwise unsinged right shift(>>>): **This also refers to shifting to the right by occupying the vacant space with “**0**“. The syntax provided below can be used to apply the bitwise unsigned operator:

While dealing with shift operators, it is recommended that operand2 must be less than operand1, otherwise, an error may be returned. Moreover, the negative numbers cannot be used to apply the shift operators as the result could be infinity.

## How to use bitwise operators in Java

This section provides the implementation of several bitwise operators in Java.

**Using bitwise OR (|): **The Java code written below is practiced using the bitwise OR operator.

public class BitwiseOp {

public static void main(String[]args) {

//initializing variables

int a=4, b=6;

//printing the binary of variable a

System.out.println(Integer.toBinaryString(a));

//printing the binary of variable b

System.out.println(Integer.toBinaryString(b));

//using the OR operator on a and b

System.out.println("The result of a|bis :" +(a|b));

//printing the binary of a|b

System.out.println(Integer.toBinaryString(a|b));

}

}

The description of the code is:

– initializing two variables **a** and **b**

– printing the binaries of variables, **a** and **b**

– prints the result of **a|b**

– gets the binary of **a|b**

**Note: **Printing/getting the binary of the integer in the above code is optional. We have used it for better understanding, otherwise, Java automatically performs the bitwise operations on the equivalent binary of the number.

The output of the code is provided below:

The output shows that the binary numbers of “a=4” and “b=6**” **are “100” and “110” respectively. And when the bitwise “OR” operator is applied, the result is 6 and its equivalent binary is “110”.

**Using bitwise AND (&): **To demonstrate the usage of bitwise AND, we have practiced the following Java code.

public class BitwiseOp {

public static void main(String[]args) {

//initializing variables

int x=5, y=7;

//printing the binary of variable x

System.out.println(Integer.toBinaryString(x));

//printing the binary of variable y

System.out.println(Integer.toBinaryString(y));

//using the AND operator on x and y

System.out.println("The result of x&y is: " +(x&y));

//printing the binary of x&y

System.out.println(Integer.toBinaryString(x&y));

}

}

The above-stated code is described as:

– initializes two variables **x** and **y**

– printing the binary of **x**

– printing the binary of **y**

– applying **& **operator on **x**,**y**

– printed the binary of **x&y**

The output of the above code is shown below:

From the output, it is observed that the binary of “x=5” and “y=7” is “101” and “111” respectively. When bitwise **AND **is applied on these, the result is “5” which has a binary value “101”.

**Using bitwise complement (~): **The bitwise complement operator is used in the code provided blew.

The above code gets the value of **z=2** and prints the bitwise complement of **z**.

The output can be seen below:

**Using bitwise left shift(<<): **We have practiced the following Java code to implement the bitwise left shift operator.

public class BitwiseOp {

public static void main(String[]args) {

//initializing variable

int a=4;

//binary of a

System.out.println(Integer.toBinaryString(a));

//using the bitwise left shift on a

System.out.println(a<<2);

//binary of a<<2

System.out.println(Integer.toBinaryString(a<<2));

}

}

The above code is described below:

– **a** variable is initialized

– printed the binary of **a**

– using bitwise shift operator on **a**

– getting the binary of **a<<2 **(number of bits that will be shifted)

The output of the code is shown below:

From the output, it is observed that the binary of “a=4” is “100” and when 2bits are shifted the binary would be “10000” and its equivalent decimal would be “16”.

**Using bitwise right shift(>>): **The applicability of the right shift operator is described in the following code.

public class BitwiseOp {

public static void main(String[]args) {

//initializing variable

int a=7;

//binary of a

System.out.println(Integer.toBinaryString(a));

//using the bitwise right shift on a

System.out.println(a>>2);

//binary of a>>2

System.out.println(Integer.toBinaryString(a>>2));

}

}

The code is described as:

– variable **a** is initialized

– binary of **a** is printed

– applied right shift on **a**

– printed the binary of **a>>2**.

The output of the code is provided here:

The output shows that the right 2bits are removed from “111”(binary of 7) and the resulting binary is “1”.

**Using bitwise unsigned right shift(>>>): **The following code shows the usage of the bitwise unsigned right shift operator.

public class BitwiseOp {

public static void main(String[]args) {

//initializing variable

int x=11;

//binary of x

System.out.println(Integer.toBinaryString(x));

//using the bitwise unsigned right shift on x

System.out.println(x>>>2);

//binary of x>>>2

System.out.println(Integer.toBinaryString(x>>>2));

}

}

The description of the code is as:

– initialized a variable **x**

– printed the binary of **x**

– printed the result of **x>>>2**

– obtained the binary of **x>>>2**

The output can be seen in the following image:

The unsigned right shift operator moves the bit to the right and the vacant space is occupied by 2(as we have set the number of bits to 2) 0’s. Moreover, it is observed from the output that the rightmost 2bits are removed.

## Conclusion

The bitwise operators in Java are practiced by performing several operations on bitwise patterns. The bitwise pattern considers the bits for manipulating the data. This article demonstrates several bitwise operators in Java. The bitwise operators include bitwise AND, bitwise OR, bitwise complement, XOR etc. You would have learned the basic working and usage of all these bitwise operators in Java.