Bird
Raised Fist0
DSA Pythonprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the operation num | (1 << k) do to the number num in terms of its bits?
easy
A. Sets the k-th bit of num to 1
B. Clears the k-th bit of num to 0
C. Toggles the k-th bit of num
D. Checks if the k-th bit of num is set

Solution

  1. Step 1: Understand bitwise OR with shifted 1

    The expression 1 << k creates a number with only the k-th bit set to 1.
  2. Step 2: Apply OR operation to set bit

    Using num | (1 << k) sets the k-th bit of num to 1 without changing other bits.
  3. Final Answer:

    Sets the k-th bit of num to 1 -> Option A
  4. Quick Check:

    OR with 1 at bit k sets that bit [OK]
Hint: Use OR with 1 shifted to set a bit [OK]
Common Mistakes:
  • Confusing OR with AND operation
  • Thinking it clears or toggles the bit
  • Misunderstanding bit positions starting at 0
2. Which of the following Python expressions correctly clears (sets to 0) the k-th bit of an integer num?
easy
A. num & (1 << k)
B. num ^ (1 << k)
C. num | ~(1 << k)
D. num & ~(1 << k)

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    num & ~(1 << k) -> Option D
  4. Quick Check:

    AND with inverted mask clears bit [OK]
Hint: AND with NOT of shifted 1 clears bit [OK]
Common Mistakes:
  • Using OR instead of AND to clear bits
  • Forgetting to invert the mask
  • Using XOR which toggles instead of clears
3. What is the output of the following code?
num = 42  # binary 101010
k = 1
result = num ^ (1 << k)
print(result)
medium
A. 40
B. 43
C. 44
D. 41

Solution

  1. 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.
  2. Step 2: Toggle bit 1 using XOR

    1 << 1 is 2 (binary 10). XOR toggles bit 1: original bit 1 is 1, toggled to 0. So binary becomes 101000 which is 40 decimal.
  3. Final Answer:

    40 -> Option A
  4. Quick Check:

    XOR toggles bit 1 from 1 to 0 [OK]
Hint: XOR toggles bit; check original bit to find result [OK]
Common Mistakes:
  • Confusing XOR with OR or AND
  • Miscounting bit positions
  • Forgetting bits are zero-indexed from right
4. The following code is intended to clear the 3rd bit (0-based) of num. What is the error?
num = 29  # binary 11101
k = 3
num = num | ~(1 << k)
print(num)
medium
A. Incorrect bit shift direction
B. Using OR instead of AND to clear the bit
C. Missing parentheses around bitwise NOT
D. No error, code works correctly

Solution

  1. Step 1: Understand clearing bit logic

    To clear a bit, we must AND with the inverse mask, not OR.
  2. Step 2: Identify incorrect operator usage

    The code uses OR (|) which sets bits, so it cannot clear the bit. It should use AND (&).
  3. Final Answer:

    Using OR instead of AND to clear the bit -> Option B
  4. Quick Check:

    Clear bit requires AND with NOT mask [OK]
Hint: Clear bit uses AND with NOT mask, not OR [OK]
Common Mistakes:
  • Using OR instead of AND to clear bits
  • Confusing bitwise NOT precedence
  • Assuming code works without testing
5. You have a number 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?
hard
A. 15
B. 31
C. 21
D. 10

Solution

  1. Step 1: Toggle bit 0 on num=0

    num = 0 ^ (1 << 0) = 0 ^ 1 = 1 (binary 00001)
  2. Step 2: Toggle bit 2 on num=1

    num = 1 ^ (1 << 2) = 1 ^ 4 = 5 (binary 00101)
  3. Step 3: Toggle bit 4 on num=5

    num = 5 ^ (1 << 4) = 5 ^ 16 = 21 (binary 10101)
  4. Final Answer:

    21 -> Option C
  5. Quick Check:

    Toggling bits 0,2,4 sets bits to 1, sum 1+4+16=21 [OK]
Hint: Toggle bits one by one with XOR, sum powers of two [OK]
Common Mistakes:
  • Toggling bits in wrong order
  • Miscounting bit positions
  • Adding instead of XORing bits
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard