What if you could flip any switch instantly without touching the others?
Why Set Clear Toggle a Specific Bit in DSA Javascript?
Imagine you have a long list of light switches, each representing a feature in a game or app. You want to turn one specific switch on, off, or flip it without touching the others.
Manually checking and changing each switch one by one is slow and confusing. It's easy to make mistakes, like turning off the wrong switch or forgetting which one you changed.
Using bit operations lets you control each switch with a simple number. You can quickly turn a specific bit on, off, or toggle it using just a few commands, without affecting the others.
let switches = [false, false, false, false]; switches[2] = true; // turn on 3rd switch // To toggle, need if-else checks
let switches = 0b0000; switches |= 1 << 2; // set 3rd bit on switches ^= 1 << 2; // toggle 3rd bit
This lets you manage many on/off settings quickly and safely using just numbers, saving time and avoiding errors.
Game developers use this to track which achievements a player has unlocked by turning bits on or off in a single number.
Manual switch control is slow and error-prone.
Bit operations let you set, clear, or toggle specific bits easily.
This method is fast, safe, and perfect for managing many on/off states.
Practice
num | (1 << k) do to the number num in terms of bits?Solution
Step 1: Understand bitwise OR with shifted 1
The expression1 << kcreates a number with only the k-th bit set to 1.Step 2: Apply OR operation to set 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 BQuick Check:
Bitwise OR with 1 at position k sets that bit [OK]
- Confusing set with clear operation
- Thinking it toggles the bit
- Assuming it checks the bit instead
num?Solution
Step 1: Understand clearing a bit
To clear the k-th bit, we want to set it to 0 while keeping others unchanged.Step 2: Use AND with NOT mask
The mask~(1 << k)has all bits 1 except the k-th bit 0. ANDing with this clears the k-th bit.Final Answer:
num & ~(1 << k) -> Option DQuick 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 num = 10; // binary 1010 let k = 1; num = num ^ (1 << k); console.log(num);
Solution
Step 1: Understand initial values and bit positions
num = 10 (binary 1010), k = 1 means second bit from right (0-based).Step 2: Apply XOR to toggle k-th bit
1 << 1= 2 (binary 0010). XOR toggles bit 1: 1010 ^ 0010 = 1000 (binary), which is 8 decimal.Final Answer:
8 -> Option AQuick Check:
XOR toggles bit 1: 10 -> 8 [OK]
- Confusing XOR with OR or AND
- Miscounting bit positions (0-based)
- Not converting binary to decimal correctly
num. What is the error?function toggleBit(num, k) {
return num & (1 << k);
}
console.log(toggleBit(5, 0));Solution
Step 1: Identify intended toggle operation
Toggling a bit requires flipping it: 0 to 1 or 1 to 0.Step 2: Check the operator used
The code uses AND (&) which masks bits, not toggles. XOR (^) is needed to toggle bits.Final Answer:
Using AND instead of XOR to toggle the bit -> Option CQuick Check:
Toggle needs XOR, not AND [OK]
- Using AND or OR instead of XOR for toggle
- Confusing toggle with set or clear
- Ignoring operator precedence
updateBit(num, k, bitValue) that sets the k-th bit of num to bitValue (0 or 1). Which of the following implementations is correct?Solution
Step 1: Clear the k-th bit first
Usenum & ~(1 << k)to clear the k-th bit regardless of its current value.Step 2: Set the k-th bit to bitValue
ShiftbitValueto k-th position and OR it with cleared number to set the bit correctly.Final Answer:
return (num & ~(1 << k)) | (bitValue << k); -> Option AQuick Check:
Clear then set bit with OR [OK]
- Using XOR which toggles instead of sets
- Not clearing bit before setting
- Using AND which masks bits incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
