Bird
Raised Fist0
DSA Rustprogramming~10 mins

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

Choose your learning style10 modes available
Concept Flow - Set Clear Toggle a Specific Bit
Start with number
↓
Choose bit position
↓
Select operation: Set / Clear / Toggle
↓
Create bitmask with 1 << position
↓
Apply operation on number using bitmask
↓
Resulting number with bit changed
↓
End
This flow shows how to pick a bit position and apply set, clear, or toggle operations on that bit in a number.
Execution Sample
DSA Rust
let mut num = 0b1010; // 10 in decimal
let pos = 1;
// Set bit
num |= 1 << pos;
println!("{:04b}", num);
This code sets the bit at position 1 of the number 1010 (binary), changing it if not already set.
Execution Table
StepOperationNumber BeforeBit PositionBitmask (1 << pos)Number AfterVisual State (binary)
1Start0b1010 (10)--0b1010 (10)1010
2Create bitmask-10b0010 (2)--
3Set bit0b1010 (10)10b0010 (2)0b1010 (10)1010
4Clear bit0b1010 (10)30b1000 (8)0b0010 (2)0010
5Toggle bit0b0010 (2)00b0001 (1)0b0011 (3)0011
6Toggle bit0b0011 (3)10b0010 (2)0b0001 (1)0001
7End0b0001 (1)----
💡 All operations applied; final number is 0b0001 (1 decimal).
Variable Tracker
VariableStartAfter Step 3 (Set)After Step 4 (Clear)After Step 5 (Toggle)After Step 6 (Toggle)Final
num0b1010 (10)0b1010 (10)0b0010 (2)0b0011 (3)0b0001 (1)0b0001 (1)
pos-1 (set)3 (clear)0 (toggle)1 (toggle)-
bitmask-0b0010 (2)0b1000 (8)0b0001 (1)0b0010 (2)-
Key Moments - 3 Insights
Why does setting a bit that is already 1 not change the number?
Because in step 3, the bitmask 0b0010 ORed with 0b1010 keeps the bit at position 1 set, so the number stays 0b1010 as shown in the execution_table row 3.
How does clearing a bit work using bitmask?
In step 4, clearing bit 3 uses bitmask 0b1000. We invert the bitmask and AND it with the number, turning that bit off. The number changes from 0b1010 to 0b0010 as shown in execution_table row 4.
What happens when toggling a bit that is 1?
Toggling flips the bit. In step 6, toggling bit 1 changes the number from 0b0011 to 0b0001 by flipping that bit off, as shown in execution_table row 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the binary state of 'num' after setting bit at position 1?
A0011
B0010
C1010
D0001
💡 Hint
Check execution_table row 3 under Visual State (binary).
At which step does clearing a bit occur and what is the resulting number in decimal?
AStep 4, 2
BStep 3, 10
CStep 5, 3
DStep 6, 1
💡 Hint
Look at execution_table row 4 for the clear operation and Number After column.
If we toggle bit at position 0 when num is 0b0010, what will be the new binary state?
A0010
B0011
C0001
D1010
💡 Hint
Refer to execution_table row 5 for toggle operation at position 0.
Concept Snapshot
Set, Clear, Toggle a bit in a number:
- Use bitmask = 1 << position
- Set: num |= bitmask (turn bit ON)
- Clear: num &= !bitmask (turn bit OFF)
- Toggle: num ^= bitmask (flip bit)
- Positions start at 0 (rightmost bit)
- Binary visual helps track changes
Full Transcript
This lesson shows how to change a specific bit in a number using set, clear, and toggle operations. We start with a number and choose a bit position. We create a bitmask by shifting 1 left by that position. To set a bit, we OR the number with the bitmask. To clear a bit, we AND the number with the inverted bitmask. To toggle a bit, we XOR the number with the bitmask. The execution table traces each step with the number before and after, the bitmask used, and the binary visual state. The variable tracker shows how 'num', 'pos', and 'bitmask' change after each operation. Key moments clarify why setting an already set bit doesn't change the number, how clearing works, and what toggling does. The quiz tests understanding of these steps by asking about specific states and operations. This helps beginners see exactly how bits change in a number step-by-step.

Practice

(1/5)
1. What does the operation num | (1 << pos) do to the bit at position pos in num?
easy
A. Sets the bit at position pos to 1
B. Clears the bit at position pos to 0
C. Toggles the bit at position pos
D. Checks if the bit at position pos is set

Solution

  1. Step 1: Understand the bitwise OR with shifted 1

    The expression 1 << pos creates a number with only the bit at position pos set to 1, all others 0.
  2. Step 2: Apply OR operation to set the bit

    Using num | (1 << pos) sets the bit at pos to 1 regardless of its previous value, leaving other bits unchanged.
  3. Final Answer:

    Sets the bit at position pos to 1 -> Option A
  4. Quick Check:

    OR with 1 at pos sets bit to 1 [OK]
Hint: OR with 1 at position sets that bit to 1 [OK]
Common Mistakes:
  • Confusing OR with XOR for toggling
  • Thinking it clears the bit instead of setting
  • Assuming it checks the bit instead of modifying
2. Which of the following Rust code snippets correctly clears (sets to 0) the bit at position pos in num?
easy
A. num & (1 << pos)
B. num & !(1 << pos)
C. num | !(1 << pos)
D. num ^ (1 << pos)

Solution

  1. Step 1: Create mask with 0 at pos and 1 elsewhere

    The expression !(1 << pos) creates a mask with all bits 1 except bit pos which is 0.
  2. Step 2: AND operation clears bit at pos

    Using num & !(1 << pos) clears the bit at pos to 0, keeping other bits unchanged.
  3. Final Answer:

    num & !(1 << pos) -> Option B
  4. Quick Check:

    AND with inverted mask clears bit [OK]
Hint: AND with NOT mask clears bit at position [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 Rust code?
let mut num = 0b1010; // binary 10
let pos = 1;
num ^= 1 << pos;
println!("{:04b}", num);
medium
A. 1000
B. 1010
C. 1011
D. 1110

Solution

  1. Step 1: Understand initial value and bit position

    Initial num is binary 1010 (decimal 10). Position pos is 1 (counting from 0 at right).
  2. Step 2: Toggle bit at position 1 using XOR

    1 << pos is 0010. XOR toggles bit 1: 1010 XOR 0010 = 1000 (decimal 8).
  3. Final Answer:

    1000 -> Option A
  4. Quick Check:

    XOR toggles bit 1: 1010 -> 1000 [OK]
Hint: XOR with 1 toggles the bit at position [OK]
Common Mistakes:
  • Confusing XOR with OR or AND
  • Miscounting bit positions
  • Printing decimal instead of binary
4. Identify the error in this Rust code that tries to toggle the bit at position pos in num:
let mut num = 0b1001;
let pos = 2;
num = num | (1 << pos);
num = num ^ (1 << pos);
println!("{:04b}", num);
medium
A. The code will cause a compile error
B. The bit position is out of range
C. The bit is toggled twice, so no change occurs
D. The OR operator should be AND to toggle

Solution

  1. Step 1: Analyze the first operation (OR)

    num | (1 << pos) sets bit 2 to 1. Bit 2 was 0 in 1001, so num becomes 1101.
  2. Step 2: Analyze the second operation (XOR)

    num ^ (1 << pos) toggles bit 2 from 1 back to 0, resulting in 1001.
  3. Step 3: Check final output

    After both operations, bit 2 was set then toggled back, so no net change occurs.
  4. Final Answer:

    The bit is toggled twice, so no change occurs -> Option C
  5. Quick Check:

    Setting then toggling same bit cancels out [OK]
Hint: Setting then toggling same bit cancels effect [OK]
Common Mistakes:
  • Assuming OR then XOR always toggles bit
  • Ignoring bit position indexing
  • Expecting compile error where none exists
5. Given a 8-bit number num = 0b11001100, write the result after toggling bits at positions 2, 3, and 7 in sequence using Rust bitwise operations. What is the final binary value?
hard
A. 10001100
B. 01001100
C. 11001100
D. 01000000

Solution

  1. Step 1: Initial value and bit positions

    Initial num is 11001100 (bits 7 to 0). Positions to toggle: 2, 3, 7.
  2. Step 2: Toggle bit 2

    Bit 2 is 1 (counting from 0 rightmost). Toggle flips it to 0: 11001100 -> 11001000.
  3. Step 3: Toggle bit 3

    Bit 3 is 1. Toggle flips it to 0: 11001000 -> 11000000.
  4. Step 4: Toggle bit 7

    Bit 7 is 1. Toggle flips it to 0: 11000000 -> 01000000.
  5. Step 5: Final binary value

    Final value is 01000000 in binary (bits 7,3,2 toggled to 0, others unchanged).
  6. Final Answer:

    01000000 -> Option D
  7. Quick Check:

    Toggle bits 2,3,7 flips those bits [OK]
Hint: Toggle bits one by one with XOR [OK]
Common Mistakes:
  • Miscounting bit positions from right
  • Forgetting to toggle bits in sequence
  • Confusing toggle with set or clear
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