What if you could flip a single switch in a huge control panel with just one quick move?
Why Set Clear Toggle a Specific Bit in DSA Python?
Imagine you have a long list of light switches, each representing a feature in a software. You want to turn on, turn off, or flip just one switch 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.
Using bit operations lets you directly target and change only the switch you want, quickly and safely, without affecting the others.
flags = 0b1010 # To turn on 3rd switch flags = flags | 0b0100 # To turn off 2nd switch flags = flags & 0b1101
def set_bit(flags, position): return flags | (1 << position) def clear_bit(flags, position): return flags & ~(1 << position) def toggle_bit(flags, position): return flags ^ (1 << position)
This lets you control individual features or settings efficiently using just numbers.
In video games, bit flags control which abilities a character has active without needing many separate variables.
Manual changes are slow and error-prone.
Bit operations target specific bits directly.
They make managing multiple on/off settings easy and fast.
Practice
num | (1 << k) do to the number num in terms of its 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 AQuick Check:
OR with 1 at bit k sets that bit [OK]
- Confusing OR with AND operation
- Thinking it clears or toggles the bit
- Misunderstanding bit positions starting at 0
num?Solution
Step 1: Understand clearing a bit
To clear the k-th bit, we want to keep all bits except the k-th bit set to 0.Step 2: Use AND with NOT mask
The mask~(1 << k)has all bits set to 1 except the k-th bit which is 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
num = 42 # binary 101010 k = 1 result = num ^ (1 << k) print(result)
Solution
Step 1: Convert num and identify bit
42 in binary is 101010. The bit positions (0-based) from right are: bit 0=0, bit 1=1, bit 2=0, bit 3=1, bit 4=0, bit 5=1.Step 2: Toggle bit 1 using XOR
1 << 1is 2 (binary 10). XOR toggles bit 1: original bit 1 is 1, toggled to 0. So binary becomes 101000 which is 40 decimal.Final Answer:
40 -> Option AQuick Check:
XOR toggles bit 1 from 1 to 0 [OK]
- Confusing XOR with OR or AND
- Miscounting bit positions
- Forgetting bits are zero-indexed from right
num. What is the error?num = 29 # binary 11101 k = 3 num = num | ~(1 << k) print(num)
Solution
Step 1: Understand clearing bit logic
To clear a bit, we must AND with the inverse mask, not OR.Step 2: Identify incorrect operator usage
The code uses OR (|) which sets bits, so it cannot clear the bit. It should use AND (&).Final Answer:
Using OR instead of AND to clear the bit -> Option BQuick Check:
Clear bit requires AND with NOT mask [OK]
- Using OR instead of AND to clear bits
- Confusing bitwise NOT precedence
- Assuming code works without testing
num = 0. You want to toggle bits 0, 2, and 4 in sequence using the expression num = num ^ (1 << k). What is the final decimal value of num after toggling these bits?Solution
Step 1: Toggle bit 0 on num=0
num = 0 ^ (1 << 0) = 0 ^ 1 = 1 (binary 00001)Step 2: Toggle bit 2 on num=1
num = 1 ^ (1 << 2) = 1 ^ 4 = 5 (binary 00101)Step 3: Toggle bit 4 on num=5
num = 5 ^ (1 << 4) = 5 ^ 16 = 21 (binary 10101)Final Answer:
21 -> Option CQuick Check:
Toggling bits 0,2,4 sets bits to 1, sum 1+4+16=21 [OK]
- Toggling bits in wrong order
- Miscounting bit positions
- Adding instead of XORing bits
Ready to prove it?
Pass the test and this pattern moves unknown
