Check and Set Bits in C++
Check, set, clear, and toggle a single bit in C++ using masks built with 1 << pos and the &, |, and ^ operators. Step-by-step on the number 12 with binary output.
1#include <iostream>2#include <bitset>3using namespace std;45bool isBitSet(int num, int pos) {6 return (num & (1 << pos)) != 0;7}89int setBit(int num, int pos) {10 return num | (1 << pos);11}1213int clearBit(int num, int pos) {14 return num & ~(1 << pos);15}1617int toggleBit(int num, int pos) {18 return num ^ (1 << pos);19}2021int main() {22 int num = 12; // 000011002324 cout << "Original number: " << bitset<8>(num) << " (" << num << ")" << endl;2526 // Check bits27 cout << "\nChecking bits:" << endl;28 for (int i = 0; i < 8; i++) {29 cout << "Bit " << i << ": " << (isBitSet(num, i) ? "SET" : "CLEAR") << endl;30 }3132 // Set bit 033 num = setBit(num, 0);34 cout << "\nAfter setting bit 0: " << bitset<8>(num) << " (" << num << ")" << endl;3536 // Clear bit 237 num = clearBit(num, 2);38 cout << "After clearing bit 2: " << bitset<8>(num) << " (" << num << ")" << endl;3940 // Toggle bit 341 num = toggleBit(num, 3);42 cout << "After toggling bit 3: " << bitset<8>(num) << " (" << num << ")" << endl;4344 return 0;45}
Sample Output
Bit 2: SET
12 has a 1 at position 2.
After toggling bit 3: 00000001 (1)
Bit 3 of 9 flipped off.
In this one
- How 1 << pos builds a one-bit mask
- How & checks a bit
- How | sets, & ~ clears, and ^ toggles
- Which bit positions are safe
How it works
- 1
num = 12 (00001100).
- 2
Check bits 0 to 7: 2 and 3 are SET.
- 3
setBit(0) → 13.
- 4
clearBit(2) → 9.
- 5
toggleBit(3) → 1.
Dry run: Check and Set Bits
13 = 00001101
- 1.~(1 << 2) = 11111011
- 2.13 & 11111011 = 00001001 = 9
13 = 00001101 ~(1 << 2) = 11111011 13 & 11111011 = 00001001 = 9
Edge cases
Setting a bit that is already set
setBit(12, 2)
12 is unchanged, because OR with a 1 that is already there changes nothing.
Position 31 or more
isBitSet(num, 35)
1 << 35 is undefined for int. Use 1ULL << pos with a 64-bit type.
What's going on
Four tiny functions cover almost every single-bit job: check, set, clear, and toggle. All of them start from a mask, 1 << pos, which is a number with just one 1 at position pos. Bit 0 is the rightmost.
isBitSet ANDs the number with the mask. If that bit is 1 the result is non-zero; otherwise it is 0. For 12 (00001100), bits 2 and 3 are SET and the rest are CLEAR, which is what the loop prints.
setBit ORs with the mask, which forces that bit to 1 and leaves the others alone. Setting bit 0 turns 12 into 00001101 = 13.
clearBit ANDs with ~(1 << pos), a mask that has every bit set except pos. That forces one bit to 0. Clearing bit 2 of 13 gives 00001001 = 9.
toggleBit XORs with the mask, which flips that one bit. Toggling bit 3 of 9 turns it off, giving 00000001 = 1. Toggling it again would turn it back on.
These are the building blocks for storing many yes/no flags in one integer. For example, file permissions use one bit each for read, write, and execute.
pos must be from 0 to 30 for a 32-bit int. 1 << 31 lands on the sign bit and gives a negative number, and 32 or more is undefined. Use 1u << pos or 1ULL << pos for higher bits.
The headers come first. <iostream> is there because it provides cout and cin for input/output operations. <bitset> is there because it provides bitset for binary representation display. Leave one out and the compiler stops at the first name it does not recognize.
Understanding Bit Manipulation
Bit Position Concept:
Common Operations:
Checking if Bit is Set
Operation:
bool isBitSet(int num, int pos) {
return (num & (1 << pos)) != 0;
}
What happens here:
Example:
Setting a Bit
Operation:
int setBit(int num, int pos) {
return num | (1 << pos);
}
What happens here:
Example:
Clearing a Bit
Operation:
int clearBit(int num, int pos) {
return num & ~(1 << pos);
}
What happens here:
Example:
Toggling a Bit
Operation:
int toggleBit(int num, int pos) {
return num ^ (1 << pos);
}
What happens here:
Example:
When to Use Bit Manipulation
Good fits:
Example Scenarios:
Important Considerations
Bit Position:
Efficiency:
Readability:
return 0;
return 0; closes main and tells the operating system the program finished without an error.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Which expression clears bit 4 of x?
Pick an answer to check it.
Question 2
Toggling bit 0 of 6 gives what?
Pick an answer to check it.
Questions people ask
How do I check if a number is odd with bits?
(n & 1) != 0.
How do I clear the lowest set bit?
n & (n - 1).
Is there a library type for this?
std::bitset has set(), reset(), flip(), and test().