Bit Manipulations Demystified
Hi! coder, Bit manipulation is a technique that involves the manipulation of individual bits of a binary number. These operations are frequently used in programming to optimize code, perform certain computations more efficiently, and implement specific algorithms. In this article, we’ll explore the basics of bitwise operations and bit manipulation with examples in Python3.x .
1. Setting a bit
To set a bit at a given position in a number, we can use the bitwise OR (|) operator with a mask that has a 1 in the bit position we want to set. Here’s an example code:
def set_bit(num, pos):
return num | (1 << pos)
Explanation:
The function takes two parameters: the number (num) and the position (pos) of the bit we want to set.
The function then creates a mask by shifting the value 1 to the left by the position we want to set, so that the 1 is in the correct bit position.
The function then performs a bitwise OR operation between the original number and the mask to set the bit at the specified position.
Finally, the function returns the modified number with the bit set.
Example:
Let us consider num 13, which in binary is 1101. To count the set bits (i.e., 1s) in this number, we can use the following code:
def count_set_bits(num):
count = 0
while num:
count += num & 1
num >>= 1
return count
count_set_bits(13) # returns 3
Here’s a step-by-step process of how the function counts the set bits in the number 13:
num = 1101
count = 0
# Iteration 1: num & 1 = 1, so count = 1
num >>= 1 # num = 0110
# Iteration 2: num & 1 = 0, so count = 1
num >>= 1 # num = 0011
# Iteration 3: num & 1 = 1, so count = 2
num >>= 1 # num = 0001
# Iteration 4: num & 1 = 1, so count = 3
num >>= 1 # num = 0000
# Loop ends, return count = 3
2. Clearing a Bit
To clear a bit at a given position in a number, we can use the bitwise AND (&) operator with a mask that has a 0 in the bit position we want to clear. Here’s an example code:
def clear_bit(num, pos):
return num & ~(1 << pos)
Explanation:
The function takes two parameters: the number (num) and the position (pos) of the bit we want to clear.
The function then creates a mask by shifting the value 1 to the left by the position we want to clear and then taking the complement of the mask using the NOT (~) operator. This gives us a mask with a 0 in the bit position we want to clear.
The function then performs a bitwise AND operation between the original number and the mask to clear the bit at the specified position.
Finally, the function returns the modified number with the bit cleared.
Example:
Suppose we have the number 12, which in binary is 1100. Let’s say we want to clear the second bit from the right (i.e., make it 0). We can use the following code to accomplish this:
def clear_bit(num, pos):
mask = ~(1 << pos)
return num & mask
num = 12 # 1100 in binary
pos = 1 # clear the second bit from the right
result = clear_bit(num, pos)
print(result) # prints 8, which is 1000 in binary
Here’s a view of how the function clears the second bit in the number 12:
num = 1100
pos = 1
# Step 1: create a mask with a 0 in the position to be cleared
mask = ~(1 << pos) # mask = 1011
# Step 2: AND the original number with the mask to clear the bit
result = num & mask # result = 1000
# Return the result, which is the number with the specified bit cleared
3. Toggling a Bit
To toggle a bit at a given position in a number, we can use the bitwise XOR (^) operator with a mask that has a 1 in the bit position we want to toggle. Here’s an example code:
def toggle_bit(num, pos):
return num ^ (1 << pos)
Explanation:
The function takes two parameters: the number (num) and the position (pos) of the bit we want to toggle.
The function then creates a mask by shifting the value 1 to the left by the position we want to toggle, so that the 1 is in the correct bit position.
The function then performs a bitwise XOR operation between the original number and the mask to toggle the bit at the specified position.
Finally, the function returns the modified number with the bit toggled.
Example:
Suppose we have the number 13, which in binary is 1101. To toggle the second bit from the right (i.e., the bit in the 2¹ position), we can use the following code:
def toggle_bit(num, bit):
return num ^ (1 << bit)
toggle_bit(13, 1) # returns 15 (i.e., 1111 in binary)
Here’s a view of how the function toggles the second bit from the right in the number 13:
num = 1101
bit = 1
# Shift 1 to the left by the value of bit (i.e., 1) to get the bitmask
bitmask = 1 << bit # bitmask = 0010
# XOR the original number with the bitmask to toggle the bit
result = num ^ bitmask # result = 1111
# Return the result, which is the original number with the specified bit toggled
4. Checking a Bit
To check if a bit is set or not in a number, we can use the bitwise AND (&) operator with a mask that has a 1 in the bit position we want to check. Here’s an example code:
def check_bit(num, pos):
return (num & (1 << pos)) != 0
Explanation:
The function takes two parameters: the number (num) and the position (pos) of the bit we want to check.
The function then creates a mask by shifting the value 1 to the left by the position we want to check, so that the 1 is in the correct bit position.
The function then performs a bitwise AND operation between the original number and the mask. If the result is not zero, then the bit at the specified position is set, and the function returns true. Otherwise, the bit is not set, and the function returns false.
Example:
Suppose we have the number 13, which in binary is 1101. We want to check if the third bit (i.e., the 2² bit) is set. We can use the following code to do this:
def check_bit(num, bit):
return (num & (1 << bit)) != 0
check_bit(13, 2) # returns True
Here’s a view of how the function checks if the third bit is set:
num = 1101
bit = 2
# Step 1: Shift 1 to the left by the bit position we want to check
# (i.e., 2 in this case) to create a mask with a 1 in that position
mask = 1 << bit # mask = 0100
# Step 2: Bitwise AND the number with the mask to extract the bit we want to check
result = num & mask # result = 0100
# Step 3: Check if the result is non-zero to determine if the bit is set
return result != 0 # True, since the third bit is set
Thank you and Hope this helps!
For more concepts and examples please go through the given link.