Bit Manipulation Tricks You Must Know
Bit Manipulation
Bit manipulation is the process of applying logical operations on a sequence of bits to achieve a required result. It is an act of algorithmically manipulating bits or other pieces of data that are shorter than a word.
We use bit manipulation to change a bit or a group of bits, invert bits, set bits, clear bits, shift bits etc…
We use basic bitwise operators like AND(&), OR(|), NOT(~), XOR(^), Left Shift(<<), Right Shift(>>) etc.. to manipulate bits.
I’ll show you how to perform some basic bit manipulation tricks, which will help you to manipulate bits in whichever way you want to get required results.
All the binary numbers in the examples that I’ll use will show you will be in unsigned binary form.
1. Set bit
Setting a bit means turning a bit in a specified index to 1. So necessarily it is turning a bit in kth index to 1 if it is 0, or leaving it unchanged if it is already 1.
Let’s look at the code to do that and understand how it is done.
Here,
Number = 10 = 00001010 ( In binary )
Mask is 1 right-shifted by 2 (Index) positions = 00000100 ( In binary )
We perform bitwise OR on the number and mask, so it will set 1 at the position of the number, where we have 1 in the mask.
2. Clear Bit
Clearing a bit means turning a bit in a specified index to 0. So necessarily it is turning a bit in kth index to 0 if it is 1, or leaving it unchanged if it is already 0.
Let’s look at the code to do that and understand how it is done.
Here,
Number = 10 = 00001010 ( In binary )
Mask is 1 right-shifted by 3 (Index) positions and Inverted = 11110111
We perform bitwise AND on the number and mask, so it will clear bit/set 0 at the position of the number, where we have 0 in the mask.
3. Flip Bit
Flipping a bit means turning a bit in a specified index to the opposite/inverse of that bit. So necessarily it is turning a bit in kth index to 0 if it is 1, and turning to 1 if it is 0.
Let’s look at the code to do that and understand how it is done.
Here,
Number = 10 = 00001010 ( In binary )
Mask is 1 right-shifted by 3 (Index) positions = 00001000 ( In binary )
We perform bitwise XOR on the number and mask, so it will flip the bit at that position ( sets 0 to 1 or 1 to 0 ).
4. Check Bit
It is used to check if a bit in a specified index is 1. So necessarily it is checking if a bit in kth index is 1. If the bit in kth index is 1 it returns 1, and if it is 0 it returns 0.
Let’s look at the code to do that and understand how it is done.
Here,
Number = 10 = 00001010 ( In binary )
Check is number right-shifted by 3 (Index) positions = 00000001
The number to be checked is now brought to rightmost position
We perform bitwise AND on check and 1, so it will return 1 if the checked bit is 1, and returns 0 if the checked bit is 0.
5. Is Odd
Now let’s see how we can check whether a number is odd or even using bit tricks.
In an odd number the rightmost bit will always be 1, and in an even number the rightmost bit will always be 0. So we can check whether the rightmost bit is 0 or 1 and return whether it is odd or even.
Let’s look at the code to do that and understand how it is done.
Here,
Number = 11 = 00001011 ( In binary )
We perform bitwise AND on the number and 1, so it will return 1 if it is odd, and returns 0 if it is even.
Conclusion
We looked into some common tricks and functions to perform basic manipulations on bitsets. I encourage anyone who is interested on this to further learn about bits and gain more knowledge on advanced topics.
We familiarized with :
- Setting Bits
- Clearing Bits
- Flipping Bits
- Checking If a bit is Set
- Checking if a number is Odd
There are a lot more to learn in bit manipulations, which can be very helpful while doing programming tasks include low-level device control, error detection and correction algorithms, data compression, encryption algorithms, and optimization.
If you found this useful, be sure to like and share. Do you find bit manipulation wonderful ? Let me know in the comments!