Welcome, future coder! Today, we're going to dive into a fascinating yet powerful concept in C programming: Bit Masking. This technique is a must-know for any programmer, as it can help solve complex problems with an elegant and efficient approach. Let's get started!
Bit Masks are a way to manipulate individual bits within a binary number. By performing bitwise operations on a mask and the number itself, we can isolate, set, clear, or toggle specific bits.
Before diving into Bit Masking, let's quickly review the basic bitwise operations:
&): Performs the bit-by-bit logical AND operation on the operands.int result = num1 & num2;|): Performs the bit-by-bit logical OR operation on the operands.int result = num1 | num2;^): Performs the bit-by-bit exclusive OR operation on the operands.int result = num1 ^ num2;~): Inverts all the bits of the operand.int result = ~num;>>): Shifts the bits of the operand to the right by the specified number of positions.int result = num >> numBits;<<): Shifts the bits of the operand to the left by the specified number of positions.int result = num << numBits;Bit masks are binary numbers that are used to manipulate specific bits in other binary numbers. To create a bit mask, we set the required bits to 1 and leave the rest as 0.
Here's an example of creating a bit mask for the 3rd bit from the right:
int mask = 1 << 3; // equals 0b1000, or decimal 8Now that we have our bit mask, let's see how we can use it to manipulate other numbers.
To isolate a specific bit, we use the bitwise AND operation between the number and the mask.
int num = 13; // equals 0b1101, or decimal 11
int mask = 1 << 2; // equals 0b1000, or decimal 8
int isolated_bit = num & mask;In this example, the isolated bit is the 3rd bit from the right in the number 13. The result will be 4 (decimal), which corresponds to the 3rd bit in the binary representation of the mask (0b1000).
To set a specific bit, we use the bitwise OR operation between the number and the mask.
int num = 12; // equals 0b1100, or decimal 12
int mask = 1 << 3; // equals 0b1000, or decimal 8
int set_bit_num = num | mask;In this example, the 3rd bit from the right in the number 12 will be set to 1. The result will be 13 (decimal), which corresponds to the binary representation of the number with the 3rd bit set to 1.
To clear a specific bit, we use the bitwise AND operation between the number and the complement of the mask.
int num = 15; // equals 0b1111, or decimal 15
int mask = 1 << 3; // equals 0b1000, or decimal 8
int clear_bit_num = num & (~mask);In this example, the 3rd bit from the right in the number 15 will be cleared to 0. The result will be 13 (decimal), which corresponds to the binary representation of the number with the 3rd bit set to 0.
Let's implement a binary search function using bit masking to make it more efficient.
#include <stdio.h>
int binarySearch(int arr[], int size, int target) {
int start = 0;
int end = size - 1;
while (start <= end) {
int mid = (start + end) >> 1; // performing a right shift by 1 to divide the array in half
int mask = 1 << mid;
if ((arr[mid] & mask) == target) {
return mid;
} else if ((arr[mid] & mask) > target) {
end = mid - 1;
} else {
start = mid + 1;
}
}
return -1;
}
int main() {
int arr[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 13;
int result = binarySearch(arr, size, target);
if (result != -1) {
printf("Element found at position %d\n", result + 1);
} else {
printf("Element not found\n");
}
return 0;
}In this example, we implement a binary search function using bit masking to determine the middle index of the array during each iteration. This approach makes the function more efficient by reducing the number of comparisons needed.
Which bitwise operation is used to perform the bit-by-bit logical AND operation on the operands?
How can we isolate the 4th bit from the right in a binary number using bit masking?
What is the time complexity of a binary search function using bit masking to determine the middle index?