Set Clear Toggle a Specific Bit in DSA Rust - Time & Space Complexity
We want to understand how fast operations like setting, clearing, or toggling a specific bit run.
How does the time needed change when we work with bigger numbers or more bits?
Analyze the time complexity of the following code snippet.
fn set_bit(num: u32, pos: u8) -> u32 {
num | (1u32 << pos)
}
fn clear_bit(num: u32, pos: u8) -> u32 {
num & !(1u32 << pos)
}
fn toggle_bit(num: u32, pos: u8) -> u32 {
num ^ (1u32 << pos)
}
This code sets, clears, or toggles a bit at a given position in a 32-bit number.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Bitwise shift and bitwise logical operations.
- How many times: Each operation runs exactly once per function call.
Each operation works on fixed-size 32-bit numbers, so the time does not grow with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 (shift, OR/AND/XOR) |
| 100 | 3 (same fixed steps) |
| 1000 | 3 (still same fixed steps) |
Pattern observation: The number of steps stays the same no matter how big the number is.
Time Complexity: O(1)
This means the operation takes the same amount of time regardless of input size.
[X] Wrong: "Setting a bit takes longer if the number is bigger or has more bits."
[OK] Correct: Bitwise operations work on fixed-size numbers and do not depend on the number's value or size.
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 a bit in a very large number stored as an array of bits? How would the time complexity change?"
Practice
num | (1 << pos) do to the bit at position pos in num?Solution
Step 1: Understand the bitwise OR with shifted 1
The expression1 << poscreates a number with only the bit at positionposset to 1, all others 0.Step 2: Apply OR operation to set the bit
Usingnum | (1 << pos)sets the bit atposto 1 regardless of its previous value, leaving other bits unchanged.Final Answer:
Sets the bit at position pos to 1 -> Option AQuick Check:
OR with 1 at pos sets bit to 1 [OK]
- Confusing OR with XOR for toggling
- Thinking it clears the bit instead of setting
- Assuming it checks the bit instead of modifying
pos in num?Solution
Step 1: Create mask with 0 at pos and 1 elsewhere
The expression!(1 << pos)creates a mask with all bits 1 except bitposwhich is 0.Step 2: AND operation clears bit at pos
Usingnum & !(1 << pos)clears the bit atposto 0, keeping other bits unchanged.Final Answer:
num & !(1 << pos) -> Option BQuick Check:
AND with inverted mask clears bit [OK]
- Using OR instead of AND to clear bits
- Forgetting to invert the mask
- Using XOR which toggles instead of clears
let mut num = 0b1010; // binary 10
let pos = 1;
num ^= 1 << pos;
println!("{:04b}", num);Solution
Step 1: Understand initial value and bit position
Initialnumis binary 1010 (decimal 10). Positionposis 1 (counting from 0 at right).Step 2: Toggle bit at position 1 using XOR
1 << posis 0010. XOR toggles bit 1: 1010 XOR 0010 = 1000 (decimal 8).Final Answer:
1000 -> Option AQuick Check:
XOR toggles bit 1: 1010 -> 1000 [OK]
- Confusing XOR with OR or AND
- Miscounting bit positions
- Printing decimal instead of binary
pos in num:
let mut num = 0b1001;
let pos = 2;
num = num | (1 << pos);
num = num ^ (1 << pos);
println!("{:04b}", num);Solution
Step 1: Analyze the first operation (OR)
num | (1 << pos)sets bit 2 to 1. Bit 2 was 0 in 1001, so num becomes 1101.Step 2: Analyze the second operation (XOR)
num ^ (1 << pos)toggles bit 2 from 1 back to 0, resulting in 1001.Step 3: Check final output
After both operations, bit 2 was set then toggled back, so no net change occurs.Final Answer:
The bit is toggled twice, so no change occurs -> Option CQuick Check:
Setting then toggling same bit cancels out [OK]
- Assuming OR then XOR always toggles bit
- Ignoring bit position indexing
- Expecting compile error where none exists
num = 0b11001100, write the result after toggling bits at positions 2, 3, and 7 in sequence using Rust bitwise operations. What is the final binary value?Solution
Step 1: Initial value and bit positions
Initialnumis 11001100 (bits 7 to 0). Positions to toggle: 2, 3, 7.Step 2: Toggle bit 2
Bit 2 is 1 (counting from 0 rightmost). Toggle flips it to 0: 11001100 -> 11001000.Step 3: Toggle bit 3
Bit 3 is 1. Toggle flips it to 0: 11001000 -> 11000000.Step 4: Toggle bit 7
Bit 7 is 1. Toggle flips it to 0: 11000000 -> 01000000.Step 5: Final binary value
Final value is 01000000 in binary (bits 7,3,2 toggled to 0, others unchanged).Final Answer:
01000000 -> Option DQuick Check:
Toggle bits 2,3,7 flips those bits [OK]
- Miscounting bit positions from right
- Forgetting to toggle bits in sequence
- Confusing toggle with set or clear
Ready to prove it?
Pass the test and this pattern moves unknown
