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
Similar Reads
Bitmask in C++ In the world of programming, where precision and efficiency are of great importance, bitmasking is a powerful technique that utilizes the manipulation of bits. C++ also offers a platform where programmers can efficiently represent data and unlock unparalleled computational capabilities through bit m
8 min read
CHAR_BIT in C CHAR_BIT : It is the number of bits in char. These days, almost all architectures use 8 bits per byte (But it is not the case always, some older machines used to have 7-bit byte). It can be found in Let us see an application of it. Suppose we wish to print byte by byte representation of an integer.
1 min read
Setting Bits in C In C programming, setting a bit is the process of setting a specific bit of a binary number to 1. This operation is crucial in various applications, including memory management, data processing, and hardware control.In this article, we will learn how to set a bit at a given position in a binary numb
3 min read
Extract bits in C In C programming, extracting a bit means retrieving the value (0 or 1) of a bit or a group of bits present at a specific position in the binary representation of a numberIn this article, we will learn how to extract a bit or multiple bits at given positions in a binary number. We will also explore h
3 min read
Toggling Bits in C++ In C++ programming, toggling a bit involves changing the value of a specific bit in a binary number. This operation is useful in various applications such as cryptography, data compression, and hardware control.In this article, we will explore how to toggle a bit at a given position in a binary numb
4 min read