Java Bit Manipulation
You must be aware of bit. When there is a flow of current through the circuits of a computer system, it forms ‘1’ bit and in case of no current, it is ‘0’ bit. With these ‘1’s and ‘0’s binary number system is formed.
Just like
decimal number system (0-9) of mathematics, binary number system (0-1) is also
very important. Binary number system has many advantages such as it helps
reducing time complexity of complex algorithms.
In our
previous lesson, we had already learned about shift operators such as left
shift operator and right shift operator in Java. These shift operators will be
used in bit manipulation in Java.
We will
learn about 04 major bit operations in java as given below:
Suppose we
have a binary number ‘001010’, with the help of below bit operators we will be
able to specified operations.
1. Get Bit Operation – To Check which bit is there (0 or 1) at a given index position.
2. Set Bit – To
set bit 1 at a given index position
3. Clear Bit – To
clear bit and make it 0 at a given index position
4. Update Bit – To
update bit. From 0 to 1 or vice versa.
Let us learn more about these operations in detail one by one:
Get Bit Operations:
Suppose we have to calculate the 2nd bit of a
number (0011). 2nd bit means index position 1 starting from right
hand side of the binary number.
In order to perform
the get bit operation to ascertain the value of 2nd bit, first we
have to calculate the bitmask of 1.
To calculate bitmask of 1, we have to left shift the binary
of 1 by 1. Why left shift by 1? The answer is simple, since we have to
calculate the value of 1st index position hence we have to left
shift the binary 1 by 1. Had it been index position 2, we would have been doing
it by 2.
The binary of 1 is 0001. If we get the bitmask of 0001 it
becomes 0010. Once we get the
bitmask, now we will perform ‘AND’ operation
between the bit mask and the binary number 0011.
After performing ‘AND’
operations, we get a binary number – 0010.
Now let us understand how to perform ‘AND’
operation. To perform ‘AND’ operation
we just have to see in both the binary number’s index values should be ‘1’. If it
is 1, then the result will be 1 otherwise the result will be ‘0’.
After performing ‘AND’
operation the binary number which we get is having 3 zeros and a 1. Since ‘1’
is there in the result of ‘AND’ operation,
hence we can say 2nd bit of a number 0011 is ‘1’ and it can be
ascertained by programming with the same logic.
Had it been ‘0’ after the ‘AND’ operation, the result would have been zero.
public class Main { public static void main(String[] args) { int n = 3; //0011 int pos = 1; int bitmask = 1 << pos; // 0001 if((bitmask & n) == 0) { System.out.println("The 2nd
position number is zero."); }else { System.out.println("The 2nd
position number is one."); } } } |
Set Bit Operation:
Set bit operation means at a given position of a binary
number we have to set the value ‘1’ irrespective of the existing value. Which means if the existing value is ‘0’ it
will be set at ‘1’ and if the existing value is ‘1’, it will be set at ‘1’
itself.
The process of set bit operation involves operation ‘OR’.
But before doing ‘OR’
operation, we have to calculate the bit mask.
Now let us try to understand how to perform the ‘OR’ operation. In binary ‘OR’ operation we have to see the same index position of both number are
‘1’ and the final result will be ‘1’. If any index position is having ‘0’ of
any of the two binary numbers, the result will be ‘0’.
The ‘OR’ operation between
two binary numbers will generate a
new binary number which will contain a value after performing set bit
operation.
public class Main { public static void main(String[] args) { int n = 6; //0110 int pos = 2; int bitmask = 1 << pos; // 0001 int new_num = bitmask | n; System.out.println(new_num); } } |
Clear Bit Operation:
In clear bit operation we have to ascertain the value of bit
mask first. Thereafter we have to perform the ‘NOT’ operation with the bitmask. After ‘NOT’ operation we have to perform ‘AND’ operation with the original number and the number which
arrived after ‘NOT’ operation. This
is how we can perform clear bit operation.
Let us see the programming example of clear bit operation
below.
public class Main { public static void main(String[] args) { // My_Window w = new My_Window(); int n = 5; //0101 int pos = 2; int bitmask = 1 << pos; // 0001 int not_of_bitmask = ~(bitmask); int new_num = not_of_bitmask & n; System.out.println(new_num); } } |
Update bit operation:
In update bit operation there will be two conditions involved.
Update bit will make ‘0’ to ‘1’ or the vice versa, which is none other than set
bit or clear bit operation respectively. So first we have to pass whether we
have to perform set bit operation or clear bit operation. Then conditionally we
have to perform any one of the operation.
Conclusion:
We hope the concept of Java bit manipulation is clear to you. If you have any question please write us.
No comments: