Bitwise operators are used to perform bit-level operations. Let’s say you are performing an AND operation on two numbers (a & b), then these numbers are converted into binary numbers and then the AND operation is performed. Finally, the compiler returns decimal equivalent of the output binary number.
Bitwise Operators in Java
1. Bitwise AND & Operator
In the following example, we are finding out the bitwise AND of two integers 6 and 10. In bitwise AND operation, numbers are compared bit by bit. The output bit of bitwise AND is 1, if the corresponding bits of two operands is 1. If either bit of an operand is 0, the output bit is 0.
6 = 00000110 (In Binary) 10 = 00001010 (In Binary) Bitwise AND Operation of 6 and 10 00000110 & 00001010 ________ 00000010 = 2 (In decimal)
Java Program:
public class JavaExample { public static void main(String[] args) { int num1 = 6, num2 = 10; //Bitwise AND Operation on two integers System.out.println("Value of num1 & num2: "+ (num1 & num2)); } }
Output:
Value of num1 & num2: 2
2. Bitwise OR | Operator
In bitwise OR operator, the output bit is 0 if the corresponding bits of both the operands are 0. If either bit of an operand is 1, the output bit is 1. Let’s find the Bitwise OR of numbers 6 and 10
6 = 00000110 (In Binary) 10 = 00001010 (In Binary) Bitwise OR Operation of 6 and 10 00000110 | 00001010 ________ 00001110 = 14 (In decimal)
Java Program:
public class JavaExample { public static void main(String[] args) { int num1 = 6, num2 = 10; //Bitwise OR of 6 and 10 System.out.println("Value of num1|num2: "+ (num1|num2)); } }
Output:
Value of num1|num2: 14
3. Bitwise XOR ^ Operator
Bitwise XOR operator is also known as exclusive OR. The output bit of Bitwise XOR is 1 if the corresponding bits of two operands are opposite and the output bit is 0 if the corresponding bits are same.
6 = 00000110 (In Binary) 10 = 00001010 (In Binary) Bitwise OR Operation of 6 and 10 00000110 ^ 00001010 ________ 00001100 = 12 (In decimal)
Java Program:
public class JavaExample { public static void main(String[] args) { int num1 = 6, num2 = 10; //Bitwise Exclusive OR of numbers 6 and 10 System.out.println("Value of num1^num2: "+ (num1^num2)); } }
Output:
Value of num1^num2: 12
4. Bitwise Complement ~ Operator
Bitwise complement is a unary operator . It works by changing 1’s to 0’s and 0’s to 1’s. It is denoted by ~ (tilde) symbol.
38 = 00100110 (In Binary) Bitwise complement(~) of Integer 38: In complement, the bits are reversed 1's become 0's and 0's become 1's. ~ 00100110 ________ 11011001 = 217 (In decimal)
However, the compiler prints the 2’s complement of this number.
2's complement of 217 = 1's complement of 217 + 1 = 1's of (11011001) + 1 = 00100110 + 1 = 00100111 = -39 (2's complement are represented by negative sign)
Java Program:
public class JavaExample { public static void main(String[] args) { int num = 38; //displaying bitwise complement of num System.out.println("Bitwise complement of 38 is: "+(~num)); } }
Output:
Bitwise complement of 38 is: -39
5. Bitwise Shift Operators
Left Shift: Left shift operator is denoted by << symbol. It shifts all bits towards left by a certain number of specified bits, for example: num<<2 will shift the bits to the left by two positions. The bit positions that have been vacated by the left shift operator are filled with 0’s.
38 = 00100110 (In binary) 38<<2 = 10011000 (In binary) [Left shift by two bits] = 152 (In decimal)
Right Shift: Right shift operator is denoted by >> symbol. It shifts all bits towards right by certain number of specified bits. For example: num>>2 will shift the bits to the right by two positions. The bit positions that have been vacated by the right shift operator are filled with 0’s.
38 = 00100110 (In binary) 38>>2 = 00001001 (In binary) [Right shift by two bits] = 9 (In decimal)
Java Program:
public class JavaExample { public static void main(String[] args) { int num = 38; //Left shift System.out.println("num<<2: "+(num<<2)); //Right shift System.out.println("num>>2: "+(num>>2)); } }
Output:
num<<2: 152 num>>2: 9