Bird
Raised Fist0
DSA Goprogramming~10 mins

Set Clear Toggle a Specific Bit in DSA Go - 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 modified
↓
End
Start with a number and a bit position, create a mask, then set, clear, or toggle that bit using bitwise operations.
Execution Sample
DSA Go
n := 13 // binary 1101
p := 2
mask := 1 << p
// Set bit
n = n | mask
This code sets the bit at position 2 of number 13 (binary 1101), changing it if needed.
Execution Table
StepOperationNumber (decimal)Number (binary)Mask (binary)Bit PositionResult (decimal)Result (binary)
1Start with n131101--131101
2Choose bit position p131101-2131101
3Create mask = 1 << p13110101002131101
4Set bit: n = n | mask13110101002131101
5Clear bit: n = n & ~mask1311010100291001
6Toggle bit: n = n ^ mask9100101002131101
7Toggle bit again: n = n ^ mask1311010100291001
8End------
💡 All operations complete; bit at position 2 set, cleared, and toggled as shown.
Variable Tracker
VariableStartAfter Step 3After Step 4 (Set)After Step 5 (Clear)After Step 6 (Toggle)After Step 7 (Toggle again)
n13 (1101)13 (1101)13 (1101)9 (1001)13 (1101)9 (1001)
p-22222
mask-4 (0100)4 (0100)4 (0100)4 (0100)4 (0100)
Key Moments - 3 Insights
Why does setting a bit with OR not change the number if the bit is already 1?
Because OR with 1 keeps the bit as 1, so if the bit was already 1, the number stays the same. See Step 4 in execution_table where n remains 13.
Why do we use ~mask to clear a bit?
Because ~mask flips the mask bits, turning the target bit to 0 and others to 1, so AND with ~mask clears that bit. See Step 5 in execution_table where n changes from 13 to 9.
How does toggling a bit twice restore the original number?
XOR toggles the bit: first toggle flips it, second toggle flips it back. See Steps 6 and 7 in execution_table where n goes 13 → 9 → 13.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 4, what is the binary result after setting bit 2?
A1001
B1111
C1101
D0100
💡 Hint
Check the 'Result (binary)' column at Step 4 in execution_table.
At which step does clearing the bit change the number from 13 to 9?
AStep 5
BStep 3
CStep 6
DStep 7
💡 Hint
Look at the 'Result (decimal)' column in execution_table for when number changes to 9.
If we toggle bit 2 twice starting from 13, what is the final decimal number?
A9
B13
C5
D4
💡 Hint
Check Steps 6 and 7 in execution_table for toggle operations.
Concept Snapshot
Set, Clear, Toggle a bit at position p in number n:
- mask = 1 << p
- Set: n = n | mask (turn bit ON)
- Clear: n = n & ~mask (turn bit OFF)
- Toggle: n = n ^ mask (flip bit)
Use bitwise operators to change bits without affecting others.
Full Transcript
This concept 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, which turns that bit on. To clear the bit, we AND n with the inverse of the mask, turning that bit off. To toggle the bit, we XOR n with the mask, flipping the bit. The execution table traces these steps with the number 13 and bit position 2, showing how the number changes in decimal and binary. The variable tracker shows how n, p, and mask change after each operation. Key moments clarify why OR doesn't change a bit already set, why we use ~mask to clear, and how toggling twice restores the original number. The quiz tests understanding of these steps and results.

Practice

(1/5)
1. What does the operation num |= (1 << 3) do to the number num?
easy
A. Sets the 4th bit of num to 1
B. Clears the 4th bit of num to 0
C. Toggles the 4th bit of num
D. Checks if the 4th bit of num is set

Solution

  1. Step 1: Understand bit shifting

    The expression 1 << 3 shifts 1 left by 3 bits, resulting in binary 00001000, which targets the 4th bit.
  2. Step 2: Apply bitwise OR to set bit

    Using |= with this mask sets the 4th bit of num to 1 without changing other bits.
  3. Final Answer:

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

    Set bit = |= (1 << position) [OK]
Hint: Use OR with shifted 1 to set a bit [OK]
Common Mistakes:
  • Confusing set with clear operation
  • Using wrong bit position (off by one)
  • Thinking it toggles instead of sets
2. Which of the following Go code snippets correctly clears the 2nd bit (bit index 1) of an integer variable num?
easy
A. num &= (1 << 1)
B. num |= (1 << 1)
C. num &= ^(1 << 1)
D. num ^= (1 << 1)

Solution

  1. Step 1: Identify clearing operation

    To clear a bit, we AND num with the inverse mask that has 0 at the target bit and 1 elsewhere.
  2. Step 2: Use bitwise AND with negated mask

    ^(1 << 1) creates a mask with all bits 1 except the 2nd bit 0. ANDing clears that bit.
  3. Final Answer:

    num &= ^(1 << 1) -> Option C
  4. Quick Check:

    Clear bit = &= ^(1 << position) [OK]
Hint: Use AND with negated shifted 1 to clear bit [OK]
Common Mistakes:
  • Using OR instead of AND to clear
  • Not negating the mask
  • Confusing bit index counting
3. What is the output of the following Go code?
package main
import "fmt"
func main() {
    num := 10 // binary 1010
    num ^= (1 << 1)
    fmt.Println(num)
}
medium
A. 10
B. 8
C. 12
D. 14

Solution

  1. Step 1: Understand initial value and bit positions

    Number 10 in binary is 1010. Bit positions (0-based) from right: bit0=0, bit1=1, bit2=0, bit3=1.
  2. Step 2: Toggle 2nd bit (bit index 1) using XOR

    XOR with (1 << 1) toggles bit1. Bit1 is 1, toggling it changes to 0. New binary: 1000 which is 8.
  3. Final Answer:

    8 -> Option B
  4. Quick Check:

    Toggle bit = ^= (1 << position) [OK]
Hint: XOR toggles bit: 1->0 or 0->1 [OK]
Common Mistakes:
  • Confusing toggle with set or clear
  • Misreading bit positions
  • Calculating decimal incorrectly
4. The following Go code is intended to clear the 3rd bit (bit index 2) of num. What is the error?
num := 15
num &= (1 << 2)
medium
A. It sets the 3rd bit instead of clearing it
B. No error, it correctly clears the 3rd bit
C. It toggles the 3rd bit instead of clearing
D. It clears all bits except the 3rd bit

Solution

  1. Step 1: Analyze the operation performed

    The code does num &= (1 << 2), which ANDs num with 00000100 (only 3rd bit set).
  2. Step 2: Understand effect on bits

    This clears all bits except the 3rd bit, effectively keeping only the 3rd bit if it was set, not clearing it.
  3. Final Answer:

    It clears all bits except the 3rd bit -> Option D
  4. Quick Check:

    Clear bit = &= ^(1 << position) [OK]
Hint: Use AND with negated mask to clear bit [OK]
Common Mistakes:
  • Using AND with mask instead of negated mask
  • Confusing clear with set or toggle
  • Assuming &= (1 << n) clears bit
5. You have an integer num initially 0. You want to toggle the 1st bit, set the 3rd bit, and clear the 2nd bit in this order. Which sequence of operations achieves this correctly in Go?
hard
A. num ^= (1 << 0); num |= (1 << 2); num &= ^(1 << 1)
B. num |= (1 << 0); num ^= (1 << 2); num &= ^(1 << 1)
C. num &= ^(1 << 1); num ^= (1 << 0); num |= (1 << 2)
D. num |= (1 << 2); num ^= (1 << 0); num &= ^(1 << 1)

Solution

  1. Step 1: Toggle 1st bit (bit 0)

    Use XOR with (1 << 0) to toggle the 1st bit: num ^= (1 << 0).
  2. Step 2: Set 3rd bit (bit 2)

    Use OR with (1 << 2) to set the 3rd bit: num |= (1 << 2).
  3. Step 3: Clear 2nd bit (bit 1)

    Use AND with negated mask to clear 2nd bit: num &= ^(1 << 1).
  4. Final Answer:

    num ^= (1 << 0); num |= (1 << 2); num &= ^(1 << 1) -> Option A
  5. Quick Check:

    Toggle, Set, Clear in order with ^=, |=, &= ^ [OK]
Hint: Toggle ^=, Set |=, Clear &= ^ in correct order [OK]
Common Mistakes:
  • Mixing order of operations
  • Using wrong operators for each action
  • Not negating mask when clearing
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