0
0
DSA Pythonprogramming~10 mins

Set Clear Toggle a Specific Bit in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Set Clear Toggle a Specific Bit
Start with number n
Choose bit position p
Select operation: Set / Clear / Toggle
Create mask = 1 << p
If Set: n = n | mask
If Clear: n = n & ~mask
If Toggle: n = n ^ mask
Result: n with bit p changed
End
This flow shows how to change a specific bit in a number by creating a mask and applying bitwise operations to set, clear, or toggle that bit.
Execution Sample
DSA Python
n = 13  # binary 1101
p = 2
mask = 1 << p
n = n | mask
print(bin(n))
This code sets the bit at position 2 of number 13 (binary 1101), turning it on.
Execution Table
StepOperationNumber (decimal)Number (binary)Mask (binary)Bit PositionPointer ChangesVisual State (binary)
1Start with n131101---1101
2Choose bit position p131101-2-1101
3Create mask = 1 << p1311011002-1101
4Set bit: n = n | mask1311011002n updated1101
5Print result1311011002-1101
6End1311011002-1101
💡 Bit at position 2 was already set; number remains 13 (binary 1101)
Variable Tracker
VariableStartAfter Step 3After Step 4Final
n13 (1101)13 (1101)13 (1101)13 (1101)
p-222
mask-4 (100)4 (100)4 (100)
Key Moments - 3 Insights
Why does the number not change after setting bit 2 in this example?
Because in execution_table row 4, the bit at position 2 was already 1 in the original number (1101), so OR with mask 100 keeps it the same.
What does the mask 1 << p represent?
As shown in execution_table row 3, mask shifts 1 left by p positions to create a binary number with only the target bit set, used to change that bit.
How do we clear or toggle a bit differently?
Instead of OR (|) for set, use AND with NOT (~) mask to clear, or XOR (^) with mask to toggle, as described in concept_flow.
Visual Quiz - 3 Questions
Test your understanding
Look at execution_table row 3, what is the binary mask created for bit position 2?
A0010
B0100
C1000
D0001
💡 Hint
Check the 'Mask (binary)' column in row 3 of execution_table
At which step does the number n get updated with the new bit set?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Operation' and 'Pointer Changes' columns in execution_table
If the bit at position 2 was 0 initially, what would be the visual state after setting it?
A1111
B1001
C1101
D0101
💡 Hint
Setting bit 2 means turning that bit to 1; check how OR operation affects bits in execution_table
Concept Snapshot
Set, Clear, Toggle a bit:
- Create mask: 1 << position
- Set bit: n = n | mask
- Clear bit: n = n & ~mask
- Toggle bit: n = n ^ mask
- Bit positions start at 0 from right (LSB)
Full Transcript
This lesson shows how to change a specific bit in a number using bitwise operations. We start with a number n and a bit position p. We create a mask by shifting 1 left by p positions. To set the bit, we OR n with the mask. To clear, we AND n with the negation of the mask. To toggle, we XOR n with the mask. The example sets bit 2 of 13 (binary 1101). Since bit 2 was already set, the number stays the same. This method lets us control individual bits easily.