Set Clear Toggle a Specific Bit in DSA Typescript - Time & Space Complexity
We want to understand how long it takes to change a specific bit in a number.
How does the time needed grow when we work with bigger numbers or different bit positions?
Analyze the time complexity of the following code snippet.
function toggleBit(num: number, bitPosition: number): number {
const mask = 1 << bitPosition;
return num ^ mask;
}
This code flips (toggles) the bit at the given position in the number.
Look for loops or repeated steps that take time.
- Primary operation: Bit shift and bitwise XOR.
- How many times: Each operation runs once per function call.
The operations do not repeat or grow with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 (shift, XOR, return) |
| 100 | 3 (same steps) |
| 1000 | 3 (same steps) |
Pattern observation: The number of steps stays the same no matter the input size.
Time Complexity: O(1)
This means the time to toggle a bit is constant and does not change with input size.
[X] Wrong: "Toggling a bit takes longer if the number is bigger or the bit position is higher."
[OK] Correct: Bitwise operations work directly on fixed-size binary data, so the time stays constant regardless of number size or bit position.
Knowing that bit operations run in constant time helps you write efficient code and answer questions confidently in interviews.
"What if we toggle multiple bits one after another? How would the time complexity change?"
Practice
num | (1 << pos) do to the bit at position pos in the number num?Solution
Step 1: Understand bitwise OR with shifted 1
The expression1 << poscreates a number with only the bit at positionposset to 1.Step 2: Apply OR operation to set bit
Usingnum | (1 << pos)sets the bit atposto 1 without changing other bits.Final Answer:
Sets the bit at position pos to 1 -> Option DQuick Check:
Bitwise OR with 1 at pos sets bit [OK]
- Confusing set with clear operation
- Using AND instead of OR to set bit
- Thinking it toggles the bit
pos in the variable num?Solution
Step 1: Understand clearing a bit
To clear a bit, we AND the number with a mask that has 0 at the target bit and 1s elsewhere.Step 2: Create mask with NOT operator
~(1 << pos)creates a mask with 0 atposand 1s elsewhere, sonum & ~(1 << pos)clears the bit.Final Answer:
num = num & ~(1 << pos); -> Option BQuick Check:
AND with NOT mask clears bit [OK]
- Using OR instead of AND to clear bit
- Forgetting to negate the mask
- Using XOR which toggles instead of clears
let num = 10; // binary 1010 let pos = 1; num = num ^ (1 << pos); console.log(num);
Solution
Step 1: Understand initial values and bit positions
Number 10 in binary is 1010. Bit positions (0-based) from right: pos 0=0, pos 1=1, pos 2=0, pos 3=1.Step 2: Apply XOR to toggle bit at position 1
1 << 1is 2 (binary 0010). XOR toggles bit 1: 1010 ^ 0010 = 1000 (binary), which is 8 decimal.Final Answer:
8 -> Option AQuick Check:
Toggle bit 1 in 10 gives 8 [OK]
- Confusing XOR with OR or AND
- Miscounting bit positions
- Assuming toggle sets bit always to 1
pos in num. What is the error?let num = 5; // binary 0101 let pos = 2; num = num & (1 << pos); console.log(num);
Solution
Step 1: Analyze the operation used
The code usesnum & (1 << pos), which ANDsnumwith a mask having 1 only atpos.Step 2: Understand effect of AND with mask
This operation keeps only the bit atposand clears all other bits, not toggling the bit.Final Answer:
It only keeps the bit at pos and clears others -> Option AQuick Check:
AND with mask keeps only that bit [OK]
- Confusing AND with XOR for toggling
- Expecting toggle but using AND
- Ignoring effect on other bits
num = 29 (binary 11101). You want to toggle the bits at positions 0, 2, and 4 simultaneously. Which TypeScript code correctly does this?Solution
Step 1: Create mask for bits 0, 2, and 4
Using OR to combine shifted bits:(1 << 0) | (1 << 2) | (1 << 4)creates mask 10101 binary (decimal 21).Step 2: Use XOR to toggle bits
XOR with this mask toggles bits 0, 2, and 4 innum.Final Answer:
num = num ^ ((1 << 0) | (1 << 2) | (1 << 4)); -> Option CQuick Check:
XOR with combined mask toggles multiple bits [OK]
- Using AND or OR instead of XOR to toggle
- Using AND between shifted bits instead of OR
- Not combining bits before toggling
Ready to prove it?
Pass the test and this pattern moves unknown
