Set Clear Toggle a Specific Bit in DSA C - Time & Space Complexity
We want to understand how fast operations like setting, clearing, or toggling a specific bit run.
How does the time to change one bit grow as the number size grows?
Analyze the time complexity of the following code snippet.
unsigned int setBit(unsigned int num, int pos) {
return num | (1U << pos);
}
unsigned int clearBit(unsigned int num, int pos) {
return num & ~(1U << pos);
}
unsigned int toggleBit(unsigned int num, int pos) {
return num ^ (1U << pos);
}
This code sets, clears, or toggles a single bit at position pos in the number num.
Each function performs a fixed number of bitwise operations.
- Primary operation: Bitwise shift and bitwise AND, OR, XOR.
- How many times: Exactly once per function call, no loops or recursion.
The number size does not affect the number of operations since only one bit is changed.
| Input Size (bits) | Approx. Operations |
|---|---|
| 8 | 3 |
| 32 | 3 |
| 64 | 3 |
Pattern observation: The operations stay the same regardless of input size.
Time Complexity: O(1)
This means the time to set, clear, or toggle a bit does not grow with the size of the number.
[X] Wrong: "Changing a bit takes longer for bigger numbers because there are more bits to check."
[OK] Correct: Bitwise operations directly access the bit position without checking others, so time stays constant.
Knowing that bitwise operations run in constant time helps you write efficient low-level code and answer questions confidently.
"What if we tried to set multiple bits at once using a loop? How would the time complexity change?"
Practice
num |= (1 << k) do to the integer num?Solution
Step 1: Understand the bit shift operation
1 << kcreates a number with only the k-th bit set to 1 and others 0.Step 2: Apply bitwise OR to set the bit
Usingnum |= (1 << k)sets the k-th bit ofnumto 1 without changing other bits.Final Answer:
Sets the k-th bit of num to 1 -> Option DQuick Check:
Set bit = OR with 1 shifted [OK]
- Confusing set with clear operation
- Using AND instead of OR to set bits
- Misunderstanding bit positions starting from 0
num?Solution
Step 1: Understand clearing a bit
Clearing means setting the bit to 0. We use AND with the complement of the bit mask.Step 2: Analyze the options
num &= ~(1 << k);clears the k-th bit by ANDing with all 1s except 0 at k-th bit.Final Answer:
num &= ~(1 << k); -> Option BQuick Check:
Clear bit = AND with NOT of shifted 1 [OK]
- Using OR instead of AND to clear bits
- Forgetting to negate the mask
- Using XOR which toggles instead of clears
int num = 10; // binary 1010
int k = 1;
num ^= (1 << k);
printf("%d", num);Solution
Step 1: Understand initial value and bit positions
num = 10 (binary 1010), k = 1 means second bit from right (0-based).Step 2: Apply XOR to toggle the k-th bit
Toggle bit 1: bit at position 1 is 1 in 1010, toggling it changes to 0, so binary becomes 1000 which is 8.Final Answer:
8 -> Option CQuick Check:
Toggle bit 1 in 1010 = 1000 = 8 [OK]
- Confusing toggle with set or clear
- Miscounting bit positions starting from 1 instead of 0
- Assuming XOR always sets bit
num. What is the error?int num = 15; // binary 1111 int k = 3; num = num & (1 << k);
Solution
Step 1: Analyze the bitwise AND operation
The code doesnum & (1 << k), which keeps only the k-th bit and clears others.Step 2: Understand the intended operation
To clear the k-th bit, we must AND with the complement of the bit mask, not the mask itself.Final Answer:
It clears all bits except the 3rd bit -> Option AQuick Check:
AND with mask keeps only that bit, not clears it [OK]
- Using mask instead of its complement to clear bit
- Confusing AND with OR for clearing bits
- Assuming AND with mask clears bit
num simultaneously. Which expression correctly does this in C?Solution
Step 1: Understand toggling multiple bits
Toggling bits uses XOR. To toggle multiple bits, XOR with mask having those bits set.Step 2: Create mask for bits 0 and 2
Mask is(1 << 0) | (1 << 2), which sets bits 0 and 2 to 1.Step 3: Apply XOR with mask
Expressionnum ^= (1 << 0) | (1 << 2);toggles both bits simultaneously.Final Answer:
num ^= (1 << 0) | (1 << 2); -> Option AQuick Check:
XOR with combined mask toggles multiple bits [OK]
- Using AND or OR instead of XOR for toggling
- Incorrectly combining masks with AND
- Misusing shift operations in toggle
Ready to prove it?
Pass the test and this pattern moves unknown
