Bitwise Operators in Java
-
Last Updated: September 20, 2023
-
By: javahandson
-
Series
In this article, we will learn what Bitwise operators are and what are different Bitwise operators in Java with proper examples. If you want to learn about other Java topics you can click the above Java menu or you can click on this link Java topics.
Bitwise operators are not understood by many of the developers. These operators are not used much so we can get away without learning them. But we should learn it anyhow because we don’t know when we will need them.
Bitwise operators work on individual bits. So if we want to work with binary numbers and perform some manipulations on them then we need to use bitwise operators.
If a decimal number is given and we have to perform the bitwise manipulation on that number then we have to first convert that decimal number to a binary number then only we can perform the bitwise operation.
Bitwise operation can be performed on binary operands or a single operand. Bitwise Operator follows the logical gates. There are 6 bitwise operators:
Bitwise And operator is denoted with ampersand symbol &. Bitwise And operator works on 2 operands. If both the bits are 1 then only the result is 1 else the result is 0.
a | b | a & b |
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
7 | 6 | 7 & 6 |
1 | 1 | 1 |
1 | 1 | 1 |
1 | 0 | 0 |
Output : 7 & 6 = 1 1 0 = 6
Note* If you want to check how decimal numbers are converted to binary please check this like decimal to binary
Bitwise Or operator is denoted with single pipe symbol |. Bitwise Or operator works on 2 operands. If either one or both of the bits is 1 then the result is 1 else the result is 0.
a | b | a | b |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
7 | 6 | 7 | 6 |
1 | 1 | 1 |
1 | 1 | 1 |
1 | 0 | 1 |
Output : 7 | 6 = 1 1 1 = 7
package com.java.handson.operators; public class BitwiseOperators { public static void main(String[] args) { int a = 7; int b = 6; int result = a & b; System.out.println("Result of 7 & 6 is : "+result); result = a | b; System.out.println("Result of 7 | 6 is : "+result); } } Output : Result of 7 & 6 is : 6 Result of 7 | 6 is : 7
Bitwise Exclusive Or Operator is denoted with symbol ^. XOR operator will work on 2 operands. XOR Operator says if one bit is opposite of the other then the result is 1 else the result is 0. It also means both the bits should be different.
a | b | a ^ b |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
7 | 6 | 7 ^ 6 |
1 | 1 | 0 |
1 | 1 | 0 |
1 | 0 | 1 |
Output : 7 ^ 6 = 0 0 1 = 1
Not Operator is denoted with symbol ~. The Not operator will work on a single operand.
The Not operator is going to give us the opposite value for each of the bits. The Not operator will take a number convert it into binary bits and then flip the value of each of the bits to output a new value.
a | ~ a |
0 | 1 |
1 | 0 |
6 | ~ 6 |
1 | 0 |
1 | 0 |
0 | 1 |
Output : ~ 6 = 0 0 1 = 1 ( But this answer is wrong )
Here we are expecting the output as 1 but that is not the case. We will probably be getting a negative number. It is because of the signed bits. When we are dealing with signed bits the first bit of the number denotes the sign.
When we are using the Not operator it is going to flip all the bits. We will be using 8-bit numbers to represent numbers.
6 = 0 0 0 0 0 1 1 0
~ 6 = 1 1 1 1 1 0 0 1
If the first bit is 1 then it is a negative Number. If the first bit is 0 then it is a positive Number.
Binary numbers use 2’s complement representation i.e. MSB ( Most significant bit ) is the signed bit and the remaining bits are magnitude. Hence ~ 6 will be calculated as:
1 1 1 1 1 0 0 1 = -(2^7 *1) + 2^6 *1 + 2^5 *1 + 2^4 *1 + 2^3 *1 + 2^2 *0 + 2^1 *0 + 2^0 *1 = -128 + 64 + 32 + 16 + 8 + 0 + 0 + 1 = -7
package com.java.handson.operators; public class BitwiseOperators { public static void main(String[] args) { int a = 7; int b = 6; int result = a ^ b; System.out.println("Result of 7 ^ 6 is : "+result); System.out.println("Result of ~ 6 is : "+ (~b)); } } Output : Result of 7 ^ 6 is : 1 Result of ~ 6 is : -7
Shift Left Operator shifts the bits of a number to the left. The shift operator works on only one number. The second number decides how many bits you want to shift the number by.
6 = 0 0 0 0 0 1 1 0 6 << 2 ( Shift left 6 by 2 ) We are shifting the bits by 2 to the left = 0 0 0 1 1 0 0 0 = 2^7 *0 + 2^6 *0 + 2^5 *0 + 2^4 *1 + 2^3 *1 + 2^2 *0 + 2^1 *0 + 2^0 *0 = 0 + 0 + 0 + 16 + 8 + 0 + 0 + 0 = 24
When we are shifting numbers to the left the leftmost bits of the number will be discarded.
In the above case, the leftmost digits are 0 so it will not make much difference but if we have a number that has all ones then we will lose some of the bits.
Shift Right Operator shifts the bits of a number to the right. The shift operator works on only one number. The second number decides how many bits you want to shift the number by.
24 = 0 0 0 1 1 0 0 0 24 >> 2 ( Shift Right 24 by 2 ) It should give us 6 because in the above example we have done a left shift 6 << 2 which gave us 24. Now if we right shift 24 >> 2 it should give us 6 ) We are shifting the bits by 2 to the right = 0 0 0 0 0 1 1 0 = 2^7 *0 + 2^6 *0 + 2^5 *0 + 2^4 *0 + 2^3 *0 + 2^2 *1 + 2^1 *1 + 2^0 *0 = 0 + 0 + 0 + 0 + 0 + 4 + 2 + 0 = 6
package com.java.handson.operators; public class BitwiseOperators { public static void main(String[] args) { int a = 6; int result = a << 2; System.out.println("Result of 6 << 2 is : "+result); a = result >> 2; System.out.println("Result of 24 >> 2 is : "+a); } } Output : Result of 6 << 2 is : 24 Result of 24 >> 2 is : 6
So this is all about Bitwise Operators in Java. I hope you like the article. If you are having any questions on this topic please raise them in the comments section.