Bitwise Operators in Java
This type of operator is used to perform operations on bits. Here are some operations that we can, and are going to perform on bits –
- & → this is the bitwise AND operator.
- | → this is the bitwise OR operator.
- ^ → this is a bitwise XOR operator.
- ~ → this is a bitwise complement operator.
- << → this is a bitwise left shift.
- >> → This is a bitwise right shift.
So, let’s understand all these bitwise operators.
First, let’s understand the Bitwise & (AND) operator. This somewhat resembles the logical AND operator, but not completely. Here, we are working with bits. Here is the truth table for & the operator –
A | B | A&B |
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
So, from the above table, we understand that for the output to be 1, all the inputs should be 1. Any input that is 0, will result in a 0 output as we can see in the above table. We can consider that 0 is false and 1 is true.
So, if we do 9&7 for example, then the 9 will be converted to binary and the 7 will be converted to binary, and then the AND operation according to the above table and the output comes out to be 1. Here is the answer to the above question –
1001
0111
————-
0001
So, if we convert 0001 to decimal, it comes out to be 1, which is the answer. So this now becomes self-explanatory for why is 9&7 are 1.
If you are still confused about the output, you need to have a look at the general AND operation related to the bits. You can understand the general concept first, which is pretty simple, and then you will get familiar with what we are doing.
Now let’s understand the OR operator. Here, we have to have at least one true input, or let’s say at least one 1 out of all the inputs. Have a look at the below table, to understand the bitwise OR operator.
A | B | A|B |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
In this situation, you can see that we at least need one 1 for the output to be 1. This may seem confusing, but can resemble this quite to the logical OR operator, but not completely, since we are working with bits here.
So, let’s have an example so that we can better understand what is going on in the bitwise OR operator. Let’s say that we have two numbers again, 9 and 7. now, if we try performing the bitwise OR operation on them, it would be something like this – first, both the numbers would be converted to binary, and the OR operation would be done.
1001
0111
————-
1111
As you can see, we got the answer as 1111, and if you try to attempt the same thing in the program, we get the output as 15, which is 1111 in binary.
The next symbol, which is defining the XOR operation over here, is the “^”. Well, if you are already familiar with the XOR operation, it is well and good, otherwise, we are going to get to the point right now.
When it comes to XOR operation, we get the output bit as 1, if there is an odd number of true (1). Have a look at the below table, to understand the XOR operation.
A | B | A^B |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
So, if we try to perform the XOR operation between two inputs, let’s say 9 and 12, the output comes out to be 5. Here is the demonstration –
1001
1100
0101
This 0101 is 5.
If we come to the left shift or right shift things, we are just shifting the bit to left, or right, by the specified number of bits. For example, we can say that shift the bits to left by 2 bits. Let’s see what happens if we shift the bits by a specified number of bits to the left.
This is how the left-shift operator is symbolized → <<
Again, we can do something like this → 10 << 2.
This simply means that we are saying that bits of the value 10(which is a decimal value), should be left-shifted by 2 bits. Let’s see how this gets going –
Firstly, let’s consider the binary for the decimal number 10, which comes out to be 00001010.
Now, if we are going to shift the bits to the left, by 2 bits, it becomes something like this → 00101000.
So, this becomes 40 in the decimal system. So, when we are shifting 2 bits left for the data 10, we get 40 as output.
On the other hand, if we are talking about the right shift, it simply means shifting the bits to the right by the specified number of bits. Let’s have a look at this too →
Firstly, the right shift operator is symbolized as → >>
Now, if we say, for example, 10>>1, this simply means that the bits for the decimal data 10, should be shifted to the right by 1 bit.
So, first, let’s consider the binary for the decimal number 10, which comes out to be 00001010 again.
Now, if we shift the bits by 1 to the right, it becomes something like this → 00000101.
So, this becomes 5 in the decimal system. So, when we are shifting 1 bit right for the data 10, the output comes out to be 5.
public class Main{
public static void main(String[] args) {
System.out.println(9&7);
System.out.println(9|7);
System.out.println(9^12);
System.out.println(~9);
System.out.println(10<<2);
System.out.println(10>>1);
}
}
If we try to execute the above java program, we get the relevant outputs accordingly. As the name of the operators says, we use these operators, when we are working with some kind of bit-by-bit operations.