Check and Set Bits
Check if Bit is Set and Set/Clear Bits in C++
C++ Check and Set Bits Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#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;
}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)
Understanding Check and Set Bits
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:
Bit manipulation enables efficient flag and permission management.
---
2. Header Files Used
---
3. Understanding Bit Manipulation
Bit Position Concept
:
Common Operations
:
---
4. Checking if Bit is Set
Operation
:
bool isBitSet(int num, int pos) {
}
return (num & (1 << pos)) != 0;How it works
:
Example
:
---
5. Setting a Bit
Operation
:
int setBit(int num, int pos) {
}
return num | (1 << pos);How it works
:
Example
:
---
6. Clearing a Bit
Operation
:
int clearBit(int num, int pos) {
}
return num & ~(1 << pos);How it works
:
Example
:
---
7. Toggling a Bit
Operation
:
int toggleBit(int num, int pos) {
}
return num ^ (1 << pos);How it works
:
Example
:
---
8. When to Use Bit Manipulation
Best For
:
Example Scenarios
:
---
9. Important Considerations
Bit Position
:
Efficiency
:
Readability
:
---
10. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for learning bit manipulation, understanding efficient data structures, and preparing for system programming and optimization in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Check and Set Bits
This C++ program is part of the "Bitwise Operations Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.