Bitmasking In C
Last Updated :
22 Sep, 2023
In this article, we are going to learn about Bitmask in C which is a powerful way to basically work with bits and functions upon boolean logic. It can be used to store data compactly and with much more efficiency in certain cases.
What is a Bit?
A bit is the smallest unit of data which can either store a 0 or 1 inside it. All the data in the computer is stored using these bits. These 2 possible values can also be represented as boolean values that are True or False. Using this we can apply boolean logic to manipulate data stored on the computer.
Bitmasking in C
Bitmasking is a technique that involves bit manipulation. It is basically like putting a mask over certain bits and hiding the other un-useful bits, so as to make the program much more efficient and optimize the memory.
A bitmask is a sequence of bits that can also be known as a bitset or bit field and is used to perform bitwise operations on the given data.
There are basically 6 bitwise operators in C that can be used to manipulate bits which are as follows:
- & (Bitwise AND Operator)
- | (Bitwise OR Operator)
- ^ (Bitwise XOR Operator)
- ~ (Bitwise NOT Operator)
- >> (RIght Shift Operator)
- << (Left Shift Operator)
Using these operators, we perform different bit masking techniques according to the requirements. Let’s discuss these techniques and how to implement them.
Bitmasking Techniques
1. Setting a Bit
In this technique, we set a particular bit to 1 without touching any of the other bits. For this, we use the bitwise OR ( | ) operator and the left shift (<<) operator.
Basically, we take the integer 1 and using the left shift operator, shift the binary representation of 1 (that is 1 only) to n places where (n+1) is the place of bit which we want to set. Then using the bitwise OR operator we turn the given number’s (n+1)th bit to 1.
Syntax
number | (1 << bit_position_to_set)
Example:
C
#include <stdio.h>
int main()
{
int x = 13;
printf ( "Ans: %d" , 13 | (1 << 5));
return 0;
}
|
2. Clearing a Bit
In this operation, we set a specific bit to 0 (as opposed to 1 in the previous case) without touching any of the other bits. We use the bitwise AND operator (&), bitwise NOT operator (~), and the left shift operator (<<) to achieve the task.
Basically, we again take 1 and shift it the the specified position. Then, we perform the NOT operation on this to convert that into a 0 and other bits of the value (1<<n) to 1. Then we do the AND operation to clear the specified bit and obtain the result.
Syntax:
number & ~(1 << bit_position_to_clear)
Example:
C
#include<stdio.h>
int main() {
int x = 13;
printf ( "Ans: %d" , 13 & ~(1 << 2) );
return 0;
}
|
3. Flipping a Bit
In this operation, we flip a specific bit that is if the bit is 0 then turn it to 1 else turn it to 0. This operation requires the use of bitwise XOR (^) operator along with the left shift (<<) operator.
Basically, as in previous operations, we shift 1 to the specified number of positions and then perform the XOR operation to flip the bit of the given number.
Syntax:
number ^ (1 << bit_position_to_flip)
Example:
C
#include<stdio.h>
int main() {
int x = 13;
printf ( "Ans: %d" , 13 ^ (1 << 3) );
return 0;
}
|
4. Checking a Bit
In this operation, we check if a particular bit is 1 or not using the bitwise AND operator (&) along with the left shift operator (<<).
We shift 1 using the left shift operator to the specified position and then perform the bitwise AND operation on that so as to check if that specific bit is 0 or 1. If the bit is 0 then the result would be 0 else the result would be 2^(bit_position).
Syntax:
number & (1 << bit_position_to_check)
Example:
C
#include<stdio.h>
int main() {
printf ( "Ans: %d \n" , 13 & (1 << 3) );
printf ( "Ans: %d \n" , 13 & (1 << 4) );
return 0;
}
|
Application of Bitmasking in C
Following are some main applications of bit masking in the C programming language.
- It is used in data compression to decrease its size.
- It is used in cryptography to encrypt the data.
- It is used in the optimization of a lot of algorithms.
Related Articles
Share your thoughts in the comments
Please Login to comment...