How to set, clear and toggle a single bit in C++
In this tutorial, we will learn how to set, clear and toggle a single bit in C++. We will use bitwise operators in our program in order to do this. See more on this further in this tutorial.
C++ code to set, clear and toggle a single bit
We will discuss setting, clearing and toggling a bit in a number one by one.
Setting a bit
To set a bit, we need to use the bitwise OR operator as shown below in the example program. Suppose we have a number num. Now let’s say we need to set the nth bit in num. We can use the following expression to do this.
num | (1 << n);
<< is left shift operator and shifts all the bits of 1 to left by n number of bits. Suppose we have number 8 (1000 in binary) and we want to set the second bit. The expression (1 << 2) gives the result as 100 (in binary). When we do the OR operation between this result and 8(1000) we get the following:
1000
0100
____
1100 =12
____
12 is the required output.
See the below code.
#include <iostream> using namespace std; int set(int num, int n); int main() { int num = 8; cout << "Setting second bit in given number " << num <<"\n"; cout << "Result = " << set(num, 2); return 0; } int set(int num, int n) { return num| (1 << n); }
Output:
Setting second bit in given number 8 Result = 12
Note that if the bit that we want to set is already set then the above code does not make any changes in the number.
Clearing a bit
To clear a bit in a given number we use bitwise AND operator as you can see in the example program. To clear nth bit in number num, we can use the following expression.
num & ~(1 << n);
Let’s say the given number is 12 and we want to clear the second bit.
We first evaluate the expression (1<<2) which gives 100. Bitwise NOT operator compliments all the bits of 100 and gives 1111 1011 (in 8 bit). On doing AND operation with 1111 1011 and 12( 0000 1100) we get the following.
1111 1011
0000 1100
_________
0000 1000 =8
_________
8 is the required result.
See the code implementation below.
#include <iostream> using namespace std; int clear(int num, int n); int main() { int num = 12; cout << "Clearing second bit in given number " << num <<"\n"; cout << "Result = " << clear(num, 2); return 0; } int clear(int num, int n) { return num & ~(1 << n); }
Output:
Clearing second bit in given number 12 Result = 8
Note that if the bit that we want to clear is already clear then the above code does not make any changes in the number.
Toggling a bit
To toggle a bit we use the bitwise XOR operator. The following expression toggles the nth bit in the number num.
num ^ (1 << n);
Say, we want to toggle the zeroth bit in 7.
First we evaluate (1 << 0) which gives the result 0000 0001. Now we XOR this result with 7. We get the following.
0000 0111
0000 0001
_________
result = 0000 0110 = 6
_________
6 is the requierd output.
See the code implementation below.
#include <iostream> using namespace std; int toggle(int num, int n); int main() { int num = 7; cout << "Toggling zeroth bit in given number " << num <<"\n"; cout << "Result = " << toggle(num, 0); } int toggle(int num, int n) { return num ^ (1 << n); }
Output:
Toggling zeroth bit in given number 7 Result = 6
Thank you.
Also, read: C++ program to find the position of rightmost unset bit of a number
Leave a Reply