C Program to Toggle Bits of a Number
To toggle bits in C, use the bitwise XOR operator
^ with a mask; for example, result = number ^ mask; toggles bits where mask has 1s.Examples
Inputnumber = 5, mask = 1
Outputresult = 4
Inputnumber = 10, mask = 3
Outputresult = 9
Inputnumber = 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. Use a mask where bits set to 1 indicate which bits to toggle in the original number. Applying XOR
^ between the number and mask flips those bits.Algorithm
1
Get the input number and the mask value.2
Apply XOR operation between the number and the mask to toggle bits.3
Store the result of the XOR operation.4
Print or return the toggled result.Code
c
#include <stdio.h> int main() { unsigned int number = 5; // Example number unsigned int mask = 1; // Bit mask to toggle unsigned int result = number ^ mask; // Toggle bits using XOR printf("Original number: %u\n", number); printf("Mask: %u\n", mask); printf("Result after toggling bits: %u\n", result); return 0; }
Output
Original number: 5
Mask: 1
Result after toggling bits: 4
Dry Run
Let's trace toggling bits of number 5 with mask 1 through the code
1
Initial values
number = 5 (binary 0101), mask = 1 (binary 0001)
2
Apply XOR
result = 5 ^ 1 = 4 (binary 0100)
3
Print result
Output result = 4
| number (binary) | mask (binary) | result (binary) |
|---|---|---|
| 0101 | 0001 | 0100 |
Why This Works
Step 1: Using XOR to toggle bits
The XOR operator ^ flips bits where the mask has 1s and leaves other bits unchanged.
Step 2: Mask defines which bits to toggle
Bits set to 1 in the mask indicate positions to flip in the original number.
Step 3: Result is the toggled number
The output number has bits toggled only at mask positions, giving the desired result.
Alternative Approaches
Toggle all bits
c
#include <stdio.h> int main() { unsigned int number = 5; unsigned int result = ~number; // Toggle all bits using NOT operator printf("Original number: %u\n", number); printf("Result after toggling all bits: %u\n", result); return 0; }
This toggles every bit, not selective bits; useful when you want to invert the entire number.
Toggle specific bit by position
c
#include <stdio.h> int main() { unsigned int number = 5; int pos = 1; // bit position to toggle (0-based from right) unsigned int mask = 1 << pos; unsigned int result = number ^ mask; printf("Original number: %u\n", number); printf("Result after toggling bit at position %d: %u\n", pos, result); return 0; }
This toggles a single bit at a given position, useful for precise bit manipulation.
Complexity: O(1) time, O(1) space
Time Complexity
Bitwise XOR is a single CPU operation, so toggling bits takes constant time regardless of input size.
Space Complexity
No extra memory is needed beyond a few variables, so space complexity is constant.
Which Approach is Fastest?
Using XOR with a mask is the fastest and simplest way to toggle bits selectively compared to looping or other methods.
| Approach | Time | Space | Best For |
|---|---|---|---|
| XOR with mask | O(1) | O(1) | Selective bit toggling |
| NOT operator | O(1) | O(1) | Toggle all bits |
| Shift and XOR single bit | O(1) | O(1) | Toggle one bit by position |
Use XOR
^ with a mask to toggle only the bits you want.Forgetting that XOR toggles bits only where mask bits are 1, so using wrong mask leads to unexpected results.