Set Clear Toggle a Specific Bit in DSA Go - 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.
func setBit(num int, pos uint) int {
return num | (1 << pos)
}
func clearBit(num int, pos uint) int {
return num &^ (1 << pos)
}
func toggleBit(num int, pos uint) int {
return num ^ (1 << pos)
}
This code sets, clears, or toggles a bit at a given position in an integer using bitwise operations.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single bitwise operation (shift and bitwise OR/AND/XOR)
- How many times: Exactly once per function call, no loops or recursion
Changing one bit takes the same amount of work no matter how big the number is.
| Input Size (bits) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The operation count stays constant 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; it is always constant.
[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 the same.
Knowing that bit operations run in constant time helps you write efficient low-level code and answer questions confidently.
"What if we tried to set multiple bits one by one in a loop? How would the time complexity change?"
Practice
num |= (1 << 3) do to the number num?Solution
Step 1: Understand bit shifting
The expression1 << 3shifts 1 left by 3 bits, resulting in binary 00001000, which targets the 4th bit.Step 2: Apply bitwise OR to set bit
Using|=with this mask sets the 4th bit ofnumto 1 without changing other bits.Final Answer:
Sets the 4th bit of num to 1 -> Option AQuick Check:
Set bit = |= (1 << position) [OK]
- Confusing set with clear operation
- Using wrong bit position (off by one)
- Thinking it toggles instead of sets
num?Solution
Step 1: Identify clearing operation
To clear a bit, we ANDnumwith the inverse mask that has 0 at the target bit and 1 elsewhere.Step 2: Use bitwise AND with negated mask
^(1 << 1)creates a mask with all bits 1 except the 2nd bit 0. ANDing clears that bit.Final Answer:
num &= ^(1 << 1) -> Option CQuick Check:
Clear bit = &= ^(1 << position) [OK]
- Using OR instead of AND to clear
- Not negating the mask
- Confusing bit index counting
package main
import "fmt"
func main() {
num := 10 // binary 1010
num ^= (1 << 1)
fmt.Println(num)
}Solution
Step 1: Understand initial value and bit positions
Number 10 in binary is 1010. Bit positions (0-based) from right: bit0=0, bit1=1, bit2=0, bit3=1.Step 2: Toggle 2nd bit (bit index 1) using XOR
XOR with (1 << 1) toggles bit1. Bit1 is 1, toggling it changes to 0. New binary: 1000 which is 8.Final Answer:
8 -> Option BQuick Check:
Toggle bit = ^= (1 << position) [OK]
- Confusing toggle with set or clear
- Misreading bit positions
- Calculating decimal incorrectly
num. What is the error?
num := 15 num &= (1 << 2)
Solution
Step 1: Analyze the operation performed
The code doesnum &= (1 << 2), which ANDsnumwith 00000100 (only 3rd bit set).Step 2: Understand effect on bits
This clears all bits except the 3rd bit, effectively keeping only the 3rd bit if it was set, not clearing it.Final Answer:
It clears all bits except the 3rd bit -> Option DQuick Check:
Clear bit = &= ^(1 << position) [OK]
- Using AND with mask instead of negated mask
- Confusing clear with set or toggle
- Assuming &= (1 << n) clears bit
num initially 0. You want to toggle the 1st bit, set the 3rd bit, and clear the 2nd bit in this order. Which sequence of operations achieves this correctly in Go?Solution
Step 1: Toggle 1st bit (bit 0)
Use XOR with (1 << 0) to toggle the 1st bit:num ^= (1 << 0).Step 2: Set 3rd bit (bit 2)
Use OR with (1 << 2) to set the 3rd bit:num |= (1 << 2).Step 3: Clear 2nd bit (bit 1)
Use AND with negated mask to clear 2nd bit:num &= ^(1 << 1).Final Answer:
num ^= (1 << 0); num |= (1 << 2); num &= ^(1 << 1) -> Option AQuick Check:
Toggle, Set, Clear in order with ^=, |=, &= ^ [OK]
- Mixing order of operations
- Using wrong operators for each action
- Not negating mask when clearing
Ready to prove it?
Pass the test and this pattern moves unknown
