Bit masking in C++

Bit masking is a process that is used to access a specific bit in the bytes of data. This phenomenon is used when you are performing the process of iteration. A bitmask is said to be a mask of a sequence of N –bits that are used to encode a part of our collection. These elements of the mask can be set or cannot be. There are bitwise operators to create or toggle the bits. These operators are used to turn on the off bit or vice-versa.

To use the C++ programs in executing them on Linux, you need to have the Ubuntu file configured and in running state. Moreover, the user must have some knowledge of the C++ language. C++ source codes are written in the text editor. Whereas for the execution process, use the Ubuntu terminal.

A bitmask is also said to be a simple mask that is a sequence of n bits. It encodes the subset of the collection. The element ‘I’ is present in the subset of the ‘ith’ bit is set in the mask. For the set of elements having nth bytes, there are chances of having a 2N mask corresponding to a subset.

Why bitmasking is used

The bitmasking process stores different values in the same numbers set. For example, consider a set in which s = {1, 2, 5, 8, 6, and 7}. To represent the set of {2, 5, 7}, we can use any bitmask 010110.

The operations performed by the bitmaps are as follow:

Set the ‘ith’ bit
This is done by considering a value ‘x’. We can perform x|=x<<i for setting a bit. We shift ‘a’ in the left direction bit by bit, then perform the bitwise operation.

Unset the ‘ith’ bit
To unset the bit, there must be a bit that is already set by the user or default. So that particular bit can be unset easily. So for that, we use operators x&=~(x <<i).

Toggle a bit
This is a process in which we use an operator x^=x<<i.

In simple words, if you want to set a bit, then it means that if i-th bit is 0, so it is then set to 1. And if it is already 1 then leave it without any modification. Similarly, in the case of a bit clearance, if i-th bit is 1, then it should be cleared to 0. And if it is already 0 now, don’t change it; leave it in the way it already is. Wherever to toggle a bit, if the i-th bit is 1, now change this bit to 0. And if it is already 0, now you need to change back to 1 again.

Example of bit masking

A basic concept of bit masking is tried here to explain the working of bit masking. This example involves all three operations of bit masking that are described above in this guide.

Now starting with the input and output stream to involve reading and writing into the file.

#include <iostream>

We have first jumped to the main program of the source code so that the code becomes simpler and well-organized. Moreover, it will become more understanding for the new ones in the field of programming. This whole program is a user-interactive system. That needs user involvement at every stage of the running system. The first step in the main program is that we ask for the number on which operations are applied from the user. Before asking, a variable is set to accept the value entered by the user.

When the user enters the number, it undergoes many processes, like a while loop is used. This loop ensures the availability of numbers every time the program is executed. When the number is entered, the system displays 3 options to the user, if the user wants to set a bitmask or if he wants to clear the bitmask, and the third one is to toggle the value. And at the end, a user is asked to select any one of them. To navigate through all these operations, we need to have such logic that will select only a single option that the user enters. While all operations remain idle at that time.

So we use a switch statement here. When the user enters his choice, this is stored in a variable, and then on that variable, we perform a switch statement. Each line of the switch statement contains a function call of each option. Whatever option the user selects, the system will execute that particular program for the option. We use a break statement with every option of the switch statement. Because when one option is completed, you need to stop the program from further execution automatically until it is asked to run the program.

Now consider the first option; the first function is about setting a mask. It contains the variable to store the number entered by the user. This number will undergo a piece of arithmetic operation to provide the value that is before and then after the bit value.

X|x <<I ;

When this operation is executed completely, the value after going through the operations is processed again, and then the value is displayed.

The next option is to unset the already created or the new mask. This will also clear the first and the next foremost value of the bit.

X&=~(x <<i);

We have explained each function separately to make the concept clear for the user.  This will also provide the previous and the next value of the bitmask.

X^=x <<I;

After writing the code, save it in the file and then save the file with an extension of ‘.c’. To execute the code, we need a ‘g++’ compiler that will compile the code. ‘bit. c’ is the name of the file.

$ g++ -o bit bit.c

When we execute the code, the control is in the main program when you select the option of the function, then according to the function, the specific function call is made, and the control is passed towards that particular function. For example, according to the image, we first enter the number and then select the option.

We will select all three options line-wise. Firstly, we have selected the first option to set the bitmask. According to the function, a bit before and after the current bit is displayed randomly when the execution is completed for the function.

Again the options are displayed.  Now we want to unset the ‘3’ position. Here again, before and after clearing, the value is displayed.

Now again, when we see the option list, select the last option, the toggle option. Enter the bit you want to toggle. The previous toggle and then the after toggle values will be displayed.

This process will be continued till you keep entering the values of options. If you want to quit the system, press ‘Ctrl + c”.


The bit masking process is favorable for the iteration processes. We have used a brief example to explain the process of setting, unset the mask, and toggle the bit. We can also alter the above-mentioned example according to our program needs. We hope this article will help you in understanding the masking process of bits.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.