What if you could flip a single switch in a huge control panel instantly and perfectly every time?
Why Set Clear Toggle a Specific Bit in DSA Typescript?
Imagine you have a long list of light switches, each representing a feature in a game or a setting in an 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 easy to mess up. You might accidentally change the wrong switch or forget to update some, causing bugs or wrong settings.
Using bit operations lets you quickly and safely change just the switch you want by working with numbers. You can turn on, turn off, or flip a specific bit without affecting the rest.
let flags = 0b1010; // To turn on bit 1 manually flags = flags | (1 << 1);
function setBit(flags: number, position: number): number {
return flags | (1 << position);
}This lets you control many on/off settings efficiently using just numbers, saving memory and speeding up your program.
In a video game, you can store which power-ups a player has using bits. Turning a bit on means the player got that power-up, turning it off means they lost it, and toggling flips the state.
Manual changes to individual bits are slow and error-prone.
Bit operations let you change one bit without affecting others.
This technique is fast, memory-efficient, and widely used in programming.
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
