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 masking. In this article, we will discuss the bitmasking, and bitmasking techniques in C++.
What is a bit?
A bit is the smallest unit of data. We know that a computer system only understands binary language which consists of 0s and 1s. These 0s and 1s single-handedly are known as - 'BIT'.
Bitmasking in C++
Bitmasking is a technique used in programming to perform operations more efficiently on binary data. Bitmasking is a frequently employed technique in algorithms to enhance performance in terms of time complexity, utilizing bitwise operators for efficient operations at the bit level.
Bitmasking in C++ involves manipulating individual bits of a number to achieve the desired output. It is achieved by generating a bit mask and is used very often for the following operations:
- Bit Toggle: If a bit is set to 0, it can be toggled to 1 and vice-versa.
- Bit Setting: If a bit is set to 0 then it's called 'bit is NOT set'. We can set it by performing a toggle operation and change it to 1. This is known as bit setting.
- Bit Clearing: If a bit is set to 1 then it's called a 'SET-BIT'. We can change it to 0 by performing a toggle operation this is called a - 'Bit-clearing' operation.
- Checking specific bit is on or off: A bit is said to be on if it's 1 and off if it's 0. For example, an integer can contain multiple bits and we can check if, in that integer, a specific bit is set or not by utilizing bitwise operators.
What is a Bit Mask?
A bit mask is the fundamental technique to achieve bit masking. It is basically a binary pattern used to perform various bit-level operations like set, clear, toggle or checking if a bit is set or not.
The bit mask is created in the following way:
Assuming that we have the set of numbers, whose binary representation has 8 bits in it, we will use this as a reference in this article.
We know that 1 has only a single bit set to 1 which is the right-most bit otherwise known as the least significant bit. The remaining 7 bits are set to 0. So it would look something like this :
Shifting the set bit (the LSB) to 3 places to the left using expression (1<<3) would look something like this:
Shifting 1 to 3 placesAfter shifting, the set bit to 3 places, it becomes 8. Similarly, we can do this for any place. We'll use this shifting property in following bitwise techniques to create a bit mask and then perform various bitwise operations with it.
Performing Bitmasking in C++
Bitmasking is done by creating a bit mask for the operation that we need to perform. This bit mask will then be followed by a bitwise operation to achieve the desired output.
Bitmasking is done by putting a mask (hiding some unnecessary bits based on some criteria) and setting or clearing the remaining bits. This masking of certain bits helps us in performing the desired operation more efficiently thereby improving our algorithm's performance and optimizing memory. Before diving into examples for a better understanding, let us go through the bitwise operators which are fundamental for understanding the examples.
Following are the bitwise operators in C++ used to perform bitmasking in C++ :
- Bitwise AND (&) - return true only if both the bits are set
- Bitwise OR (|) - returns true if either of the bits is set.
- Bitwise XOR (^) - returns true if two bits are different.
- Bitwise NOT (~) - negates the bit.
- Bitwise Left Shift (<<) - Shifts all the bits to the left by 1 place.
- Bitwise Right Shift (>>) - Shifts all the bits to the right by 1 place.
Let's look at common bitwise operations and their examples.
Bitmasking Operations in C++
1. Setting a Specific Bit
Setting a specific bit basically means changing it from 0 to 1. It can be done by utilizing the Bitwise OR because of its property to give 1 if either of the bits is set to 1 and the bitwise left shift operator.
We will shift the LSB bit of 1 to the specified position that we want to set and then perform a bitwise OR Operation.
Syntax
integer | (1 << bit_position_to_be_set)
Here, the bit position to be set will be the place of the bit that we want to change to 1.
Example

Implementation
C++
// C++ program to illustrate how to set a particular bit
#include <iostream>
using namespace std;
int main()
{
int x = 11;
// setting fifth bit using bitmask
x = x | 1 << 5;
cout << "Result after setting the fifth bit: " << x ;
return 0;
}
OutputResult after setting the fifth bit: 43
2. Clearing a Bit
Clearing a bit means we set it to 0 if it is 1 without touching or affecting any other bits. This is done by using Bitwise AND and the negation operator (Bitwise NOT). The Bitwise NOT flips all the bits that are 1 to 0 and 0 to 1. This property of the bitwise NOT helps us in clearing a set bit.
Syntax
integer & ~(1 << bit_position_to_clear)
Example

Implementation
C++
// C++ program to illustrate how to clear a particular bit
#include <iostream>
using namespace std;
int main()
{
int x = 11;
// clearing bit at third position
x = x & ~(1 << 3);
cout << "Result after clearing the 3rd bit: " << x;
return 0;
}
OutputResult after clearing the 3rd bit: 3
3. Toggle a Bit
In this operation, we flip a bit. If it's set to 1 we make it 0 and if it's set to 0 then we flip it to 1. This is easily achievable by the Bitwise XOR operator (^) and the left shift (<<). We will utilize the property of the XOR operator to flip the bits if the bits of 2 different numbers are not the same.
The same approach is used that is, by shifting 1 to a specific position which we want to flip.
Syntax
Integer ^ (1 << bit_position_to_toggle)
Example

Implementation
C++
// C++ program to illustrate how to toggle a bit
#include <iostream>
using namespace std;
int main()
{
int x = 11;
// toggling zeroth bit
x = x ^ 1 << 0;
cout << "Result after toggling the zeroth bit: " << x;
return 0;
}
OutputResult after toggling the zeroth bit: 10
4. Check if a Bit is Set or not
In this operation, we check if a bit at a specific position is set or not. This is done by using the bitwise AND (&) and the Left shift operator. We basically left shift the set bit of 1 to the specified position for which we want to perform a check and then perform a bitwise AND Operation.
If the bit is set then the answer will be - 2(bit_position) For example, if the bit position is 3, then the answer will be 23 = 8. Else if the bit is 0 (not set) then the answer will be 0.
Syntax
Integer & (1 << bit_position_to_check)
Example

Implementation
C++
// C++ program to check if the bit is set or not
#include <iostream>
using namespace std;
int main()
{
int x = 11;
// the AND will return a non zero number if the bit is
// set, otherwise it will return zero
if (x & (1 << 3)) {
cout << "Third bit is set\n";
}
else {
cout << "Third bit is not set\n";
}
return 0;
}
Conclusion
Bit masks in C++ are a great way to manipulate the bits of the number and alter their actual value. Performing operations at the bit level is efficient and is done to optimize the algorithm.
Similar Reads
Bitmasking In C
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 st
4 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++
Setting a bit in a binary number means changing a specific bit's value to 1. This is a fundamental operation in programming, especially useful in areas like memory management, data processing, and hardware control. In this article, we'll explore how to set a bit at a specific position in a binary nu
4 min read
Extract Bits in C++
Extracting bits from a given number involves extracting 'k' bits starting from a specified position 'pos' by using bitwise operations like AND (&) and shifts (<<, >>). In this article, we will learn how to extract bits in C++. Example: Input:num = 214k = 3pos = 2Output:5Extracting Bi
2 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 num
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 number In this article, we will learn how to extract a bit or multiple bits at given positions in a binary number. We will also explore
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 num
4 min read
Clearing Bits in C++
In C++ programming, clearing a bit involves setting the value of a specific bit in a binary number to 0. This operation is useful in various applications such as bit masking, controlling hardware, and optimizing algorithms. In this article, we will learn how to clear a bit at a given position in a b
4 min read
What is Bitmasking
In computer programming, the process of modifying and utilizing binary representations of numbers or any other data is known as bitmasking. A binary digit is used as a flag in bitmasking to denote the status or existence of a feature or trait. To accomplish this, certain bits within a binary number
4 min read
Toggling bits in C
In C programming, toggling a bit is the process of flipping a specific bit of the binary number. It means that the bit that is 0 becomes 1 and a bit that is 1 becomes 0. This operation is useful in the various applications including the cryptography, data compression and hardware control. In this ar
3 min read