#include <iostream>
#include <bitset>
using namespace std;
bool isBitSet(int num, int pos) {
return (num & (1 << pos)) != 0;
}
int setBit(int num, int pos) {
return num | (1 << pos);
}
int clearBit(int num, int pos) {
return num & ~(1 << pos);
}
int toggleBit(int num, int pos) {
return num ^ (1 << pos);
}
int main() {
int num = 12; // 00001100
cout << "Original number: " << bitset<8>(num) << " (" << num << ")" << endl;
// Check bits
cout << "\nChecking bits:" << endl;
for (int i = 0; i < 8; i++) {
cout << "Bit " << i << ": " << (isBitSet(num, i) ? "SET" : "CLEAR") << endl;
}
// Set bit 0
num = setBit(num, 0);
cout << "\nAfter setting bit 0: " << bitset<8>(num) << " (" << num << ")" << endl;
// Clear bit 2
num = clearBit(num, 2);
cout << "After clearing bit 2: " << bitset<8>(num) << " (" << num << ")" << endl;
// Toggle bit 3
num = toggleBit(num, 3);
cout << "After toggling bit 3: " << bitset<8>(num) << " (" << num << ")" << endl;
return 0;
}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)
This program teaches you how to Check and Set Bits in C++. These operations are fundamental for bit manipulation, allowing you to check if a specific bit is set, set a bit, clear a bit, or toggle a bit. These operations are essential for flags, permissions, and efficient data structures.
1. What This Program Does
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.
2. Header Files Used
-
#include <iostream>
- Provides cout and cin for input/output operations.
-
#include <bitset>
- Provides bitset for binary representation display.
3. 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
4. Checking if Bit is Set
Operation:
bool isBitSet(int num, int pos) { return (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)
5. Setting a Bit
Operation:
int setBit(int num, int pos) { return 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)
6. Clearing a Bit
Operation:
int clearBit(int num, int pos) { return 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)
7. Toggling a Bit
Operation:
int toggleBit(int num, int pos) { return 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)
8. 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
9. 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
10. return 0;
This ends the program successfully.
Summary
- Check bit: num & (1 << pos) != 0, checks if bit at position is set.
- Set bit: num | (1 << pos), sets bit at position to 1.
- Clear bit: num & ~(1 << pos), clears bit at position to 0.
- Toggle bit: num ^ (1 << pos), flips bit at position.
- Understanding bit manipulation enables efficient flag and permission management.
- Essential for flags, permissions, efficient data structures, and system programming.
This program is fundamental for learning bit manipulation, understanding efficient data structures, and preparing for system programming and optimization in C++ programs.