Set Clear Toggle a Specific Bit in DSA C# - Time & Space Complexity
We want to understand how long it takes to change a single bit in a number.
How does the time needed grow when we work with bigger numbers?
Analyze the time complexity of the following code snippet.
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);
}
This code sets, clears, or toggles a specific bit at position pos in the integer num.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Bitwise shift and bitwise AND, OR, XOR operations.
- How many times: Each operation runs once per function call, no loops or recursion.
Changing one bit uses a fixed number of steps regardless of number size.
| Input Size (bits in number) | Approx. Operations |
|---|---|
| 10 | 3 operations (shift, mask, bitwise op) |
| 32 | 3 operations (same fixed steps) |
| 64 | 3 operations (still fixed steps) |
Pattern observation: The number of steps stays the same no matter how big the number is.
Time Complexity: O(1)
This means changing one bit takes the same amount of time no matter how big the number is.
[X] Wrong: "Changing a bit takes longer for bigger numbers because there are more bits to check."
[OK] Correct: The operation directly targets the bit using a shift and mask, so it does not check other bits one by one.
Knowing that bit operations run in constant time helps you explain efficient low-level code clearly and confidently.
"What if we wanted to set multiple bits at once? How would the time complexity change?"
Practice
number |= (1 << position) do to the bit at position in number?Solution
Step 1: Understand the bitwise OR with shifted 1
The expression1 << positioncreates a number with only the bit atpositionset to 1. Using OR (|=) with this sets that bit innumberto 1 without changing other bits.Step 2: Effect on the bit at position
Since OR with 1 always results in 1, the bit atpositionis set to 1 regardless of its previous value.Final Answer:
Sets the bit at position to 1 -> Option CQuick Check:
OR with 1 sets bit [OK]
- Confusing OR with XOR for toggling
- Thinking it clears the bit
- Assuming it checks the bit instead
pos in an integer num?Solution
Step 1: Understand clearing a bit
To clear a bit, we use AND with the complement of a mask that has 1 at the target bit. The mask is1 << pos, so complement is~(1 << pos).Step 2: Apply AND with complemented mask
Usingnum &= ~(1 << pos);clears the bit atposwhile leaving others unchanged.Final Answer:
num &= ~(1 << pos); -> Option AQuick Check:
AND with complement clears bit [OK]
- Using OR instead of AND to clear
- Using XOR which toggles instead of clears
- Using left shift alone which changes number
int num = 10; // binary 1010 int pos = 1; num ^= (1 << pos); Console.WriteLine(num);
Solution
Step 1: Understand initial number and bit position
Number 10 in binary is 1010. Position 1 means the second bit from right (0-based). Bit at position 1 is 1.Step 2: Apply XOR to toggle bit at position 1
XOR toggles the bit. Since bit at position 1 is 1, toggling it changes it to 0. New binary is 1000 which is decimal 8.Final Answer:
8 -> Option DQuick Check:
Toggle bit 1 in 1010 = 1000 = 8 [OK]
- Confusing XOR with OR or AND
- Miscounting bit positions (0-based)
- Assuming no change if bit is 1
pos in num. What is the error?int num = 5; // binary 0101 int pos = 2; num = num & (1 << pos); Console.WriteLine(num);
Solution
Step 1: Analyze the operation used
The code uses AND (&) with1 << pos. This keeps only the bit atposif it was set, clearing all others.Step 2: Compare with toggle operation
Toggling requires XOR (^), not AND. So this code does not toggle but masks all bits except the one atpos.Final Answer:
It clears all bits except the one at pos -> Option AQuick Check:
AND with mask keeps only that bit [OK]
- Using AND instead of XOR for toggle
- Expecting toggle but getting masked value
- Confusing bitwise operators
num and want to toggle the bit at position pos only if it is currently set (1). Which code snippet correctly does this?Solution
Step 1: Check if bit at pos is set
Usingnum & (1 << pos)checks if bit atposis 1. If result is not zero, bit is set.Step 2: Toggle bit only if set
If bit is set, toggle it using XOR:num ^= (1 << pos);. This flips the bit from 1 to 0.Final Answer:
if ((num & (1 << pos)) != 0) num ^= (1 << pos); -> Option BQuick Check:
Check bit with AND, toggle with XOR [OK]
- Using OR instead of AND to check bit
- Toggling without checking bit first
- Using incorrect mask complement
Ready to prove it?
Pass the test and this pattern moves unknown
