Check and Set Bits in C++

Check if Bit is Set and Set/Clear Bits in C++.

BeginnerBitwise Operations ProgramsExample 2 of 5
check-and-set-bits.cpp
1#include <iostream>
2#include <bitset>
3using namespace std;
4
5bool isBitSet(int num, int pos) {
6 return (num & (1 << pos)) != 0;
7}
8
9int setBit(int num, int pos) {
10 return num | (1 << pos);
11}
12
13int clearBit(int num, int pos) {
14 return num & ~(1 << pos);
15}
16
17int toggleBit(int num, int pos) {
18 return num ^ (1 << pos);
19}
20
21int main() {
22 int num = 12; // 00001100
23
24 cout << "Original number: " << bitset<8>(num) << " (" << num << ")" << endl;
25
26 // Check bits
27 cout << "\nChecking bits:" << endl;
28 for (int i = 0; i < 8; i++) {
29 cout << "Bit " << i << ": " << (isBitSet(num, i) ? "SET" : "CLEAR") << endl;
30 }
31
32 // Set bit 0
33 num = setBit(num, 0);
34 cout << "\nAfter setting bit 0: " << bitset<8>(num) << " (" << num << ")" << endl;
35
36 // Clear bit 2
37 num = clearBit(num, 2);
38 cout << "After clearing bit 2: " << bitset<8>(num) << " (" << num << ")" << endl;
39
40 // Toggle bit 3
41 num = toggleBit(num, 3);
42 cout << "After toggling bit 3: " << bitset<8>(num) << " (" << num << ")" << endl;
43
44 return 0;
45}

Output

Original number: 00001100 (12)

Checking bits:
Bit 0: CLEAR
Bit 1: CLEAR
Bit 2: SET
Bit 3: SET
Bit 4: CLEAR
Bit 5: CLEAR
Bit 6: CLEAR
Bit 7: CLEAR

After setting bit 0: 00001101 (13)
After clearing bit 2: 00001001 (9)
After toggling bit 3: 00000001 (1)

What's going on

The program demonstrates bit manipulation operations:

Checking if a specific bit is set
Setting a bit to 1
Clearing a bit to 0
Toggling a bit (flip value)

Bit manipulation enables efficient flag and permission management.

Header Files Used

1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <bitset>
Provides bitset for binary representation display.

Understanding Bit Manipulation

Bit Position Concept

:

Bits numbered from right (0-indexed)
Position 0: rightmost bit (LSB)
Position n-1: leftmost bit (MSB)
Each position represents 2^position

Common Operations

:

Check: is bit set?
Set: make bit 1
Clear: make bit 0
Toggle: flip bit value

Checking if Bit is Set

Operation

:

bool isBitSet(int num, int pos) {

}

1return (num & (1 << pos)) != 0;

How it works

:

1 << pos: creates mask with bit at position pos set
num & mask: extracts bit at position
!= 0: checks if bit is set
Returns true if bit is 1

Example

:

num = 12 (00001100), pos = 2
1 << 2 = 4 (00000100)
12 & 4 = 4 (bit 2 is set)

Setting a Bit

Operation

:

int setBit(int num, int pos) {

}

1return num | (1 << pos);

How it works

:

1 << pos: creates mask with bit at position pos
num | mask: sets bit at position
OR operation sets bit to 1
Other bits unchanged

Example

:

num = 12 (00001100), pos = 0
1 << 0 = 1 (00000001)
12 | 1 = 13 (00001101)

Clearing a Bit

Operation

:

int clearBit(int num, int pos) {

}

1return num & ~(1 << pos);

How it works

:

1 << pos: creates mask with bit at position pos
~mask: inverts mask (all 1s except position pos)
num & ~mask: clears bit at position
AND operation clears bit to 0

Example

:

num = 12 (00001100), pos = 2
1 << 2 = 4 (00000100)
~4 = 251 (11111011)
12 & 251 = 8 (00001000)

Toggling a Bit

Operation

:

int toggleBit(int num, int pos) {

}

1return num ^ (1 << pos);

How it works

:

1 << pos: creates mask with bit at position pos
num ^ mask: flips bit at position
XOR operation toggles bit
0 becomes 1, 1 becomes 0

Example

:

num = 12 (00001100), pos = 3
1 << 3 = 8 (00001000)
12 ^ 8 = 4 (00000100)

When to Use Bit Manipulation

Best For

:

Flags and permissions
Efficient data structures
Memory optimization
System programming
Performance-critical code

Example Scenarios

:

File permissions (read, write, execute)
Feature flags
State machines
Bit arrays
Compression algorithms

Important Considerations

Bit Position

:

Usually 0-indexed from right
Position 0: least significant bit
Be careful with position values
Validate position range

Efficiency

:

Very fast operations
Single CPU instruction
No loops needed
Optimal performance

Readability

:

Can be less readable
Document operations
Use helper functions
Clear variable names

return 0;

This ends the program successfully.