C++ Program to Toggle Bits of a Number
To toggle bits in C++, use the bitwise XOR operator
^ with a mask. For example, result = num ^ mask; toggles bits where mask has 1s.Examples
Inputnum = 5, mask = 1
Outputresult = 4
Inputnum = 10, mask = 7
Outputresult = 13
Inputnum = 0, mask = 255
Outputresult = 255
How to Think About It
To toggle bits, think of flipping bits from 0 to 1 or 1 to 0. Using the XOR operator
^ with a mask that has 1s in positions to toggle will flip those bits in the number, leaving others unchanged.Algorithm
1
Get the input number and the mask indicating which bits to toggle.2
Apply the XOR operator between the number and the mask.3
Store the result as the toggled number.4
Return or print the toggled number.Code
cpp
#include <iostream> using namespace std; int main() { int num = 10; // original number int mask = 7; // bits to toggle int result = num ^ mask; // toggle bits using XOR cout << "Original number: " << num << endl; cout << "Mask: " << mask << endl; cout << "Number after toggling bits: " << result << endl; return 0; }
Output
Original number: 10
Mask: 7
Number after toggling bits: 13
Dry Run
Let's trace toggling bits of number 10 with mask 7 through the code
1
Initial values
num = 10 (binary 1010), mask = 7 (binary 0111)
2
Apply XOR
result = num ^ mask = 1010 ^ 0111 = 1101 (binary)
3
Convert result
1101 binary = 13 decimal
| num (binary) | mask (binary) | result (binary) | result (decimal) |
|---|---|---|---|
| 1010 | 0111 | 1101 | 13 |
Why This Works
Step 1: Using XOR to toggle bits
The XOR operator ^ flips bits where the mask has 1s and keeps bits unchanged where the mask has 0s.
Step 2: Mask defines toggle positions
The mask is a number with 1s in bit positions you want to toggle and 0s elsewhere.
Step 3: Result is the toggled number
Applying num ^ mask produces a new number with specified bits flipped.
Alternative Approaches
Toggle single bit using shift
cpp
#include <iostream> using namespace std; int main() { int num = 10; int bit_pos = 1; // toggle bit at position 1 (0-based) int result = num ^ (1 << bit_pos); cout << "Original number: " << num << endl; cout << "Number after toggling bit " << bit_pos << ": " << result << endl; return 0; }
This toggles only one bit at a time by shifting 1 to the desired position.
Toggle all bits using NOT operator
cpp
#include <iostream> using namespace std; int main() { unsigned int num = 10; unsigned int result = ~num; cout << "Original number: " << num << endl; cout << "Number after toggling all bits: " << result << endl; return 0; }
This flips all bits of the number, not selective bits.
Complexity: O(1) time, O(1) space
Time Complexity
Bitwise XOR is a single CPU operation, so toggling bits runs in constant time.
Space Complexity
No extra memory is needed besides variables to hold numbers, so space is constant.
Which Approach is Fastest?
Using XOR with a mask is the fastest and simplest way to toggle bits compared to looping or other methods.
| Approach | Time | Space | Best For |
|---|---|---|---|
| XOR with mask | O(1) | O(1) | Toggling specific bits quickly |
| Shift and XOR single bit | O(1) | O(1) | Toggling one bit at a time |
| NOT operator | O(1) | O(1) | Flipping all bits |
Use XOR with a mask of 1s at bit positions you want to toggle for easy bit flipping.
Forgetting that XOR toggles bits only where mask bits are 1, so using wrong mask leads to unexpected results.