Bird
Raised Fist0
DSA Rustprogramming~30 mins

Set Clear Toggle a Specific Bit in DSA Rust - Build from Scratch

Choose your learning style10 modes available
Set Clear Toggle a Specific Bit
📖 Scenario: You are working with a simple device that stores settings as bits in a number. Each bit can be turned on, turned off, or flipped. You want to control one specific bit in this number.
🎯 Goal: Build a Rust program that can set, clear, or toggle a specific bit in a number using bitwise operations.
📋 What You'll Learn
Create a variable number with the value 0b1010_1100 (172 in decimal).
Create a variable bit_position with the value 3 (counting bits from 0, right to left).
Create three variables set_bit, clear_bit, and toggle_bit that hold the results of setting, clearing, and toggling the bit at bit_position in number.
Print the binary form of set_bit, clear_bit, and toggle_bit.
💡 Why This Matters
🌍 Real World
Controlling device settings or flags often requires changing specific bits in a number.
💼 Career
Understanding bitwise operations is important for embedded systems, low-level programming, and performance optimization.
Progress0 / 4 steps
1
Create the initial number
Create a variable called number and set it to the binary value 0b1010_1100.
DSA Rust
Hint

Use let number = 0b1010_1100; to create the variable.

2
Create the bit position variable
Create a variable called bit_position and set it to 3.
DSA Rust
Hint

Use let bit_position = 3; to create the variable.

3
Set, clear, and toggle the bit
Create three variables called set_bit, clear_bit, and toggle_bit. Use bitwise operations to set, clear, and toggle the bit at bit_position in number.
DSA Rust
Hint

Use | to set, & ! to clear, and ^ to toggle the bit.

4
Print the results in binary
Print the values of set_bit, clear_bit, and toggle_bit in binary format using println!.
DSA Rust
Hint

Use println!("{:08b}", value); to print binary with leading zeros.

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