Bird
Raised Fist0
DSA Rustprogramming~20 mins

Set Clear Toggle a Specific Bit in DSA Rust - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Bitwise Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output after setting the 3rd bit of 0b0101?
Consider the following Rust code that sets a specific bit in a number. What will be printed?
DSA Rust
fn main() {
    let mut num = 0b0101u8;
    let bit_position = 3;
    num |= 1 << bit_position;
    println!("{:08b}", num);
}
A"00001001"
B"00001101"
C"00000101"
D"00011101"
Attempts:
2 left
💡 Hint
Setting a bit means turning it to 1 using OR operation.
❓ Predict Output
intermediate
2:00remaining
What is the output after clearing the 2nd bit of 0b1111?
Look at this Rust code that clears a specific bit. What will it print?
DSA Rust
fn main() {
    let mut num = 0b1111u8;
    let bit_position = 2;
    num &= !(1 << bit_position);
    println!("{:04b}", num);
}
A"1011"
B"1101"
C"1110"
D"0111"
Attempts:
2 left
💡 Hint
Clearing a bit means turning it to 0 using AND with NOT mask.
❓ Predict Output
advanced
2:00remaining
What is the output after toggling the 1st bit of 0b1010?
This Rust code toggles a specific bit. What will it print?
DSA Rust
fn main() {
    let mut num = 0b1010u8;
    let bit_position = 1;
    num ^= 1 << bit_position;
    println!("{:04b}", num);
}
A"0010"
B"1011"
C"1110"
D"1000"
Attempts:
2 left
💡 Hint
Toggling flips the bit: 0 to 1 or 1 to 0 using XOR.
🔧 Debug
advanced
2:00remaining
What error does this Rust code produce when trying to set a bit?
Identify the error when running this code snippet.
DSA Rust
fn main() {
    let mut num = 0b0010u8;
    let bit_position = 8;
    num |= 1 << bit_position;
    println!("{:08b}", num);
}
Apanic: attempt to shift left by 8 which is equal to or greater than the type size
Bprints "00000010" without error
Ccompilation error: mismatched types
Druntime error: integer overflow
Attempts:
2 left
💡 Hint
Shifting by a number equal or larger than the bit size of the type causes panic.
🧠 Conceptual
expert
2:00remaining
How many bits are set after toggling bits 0, 1, and 2 on 0b0000?
Starting with 0b0000, toggle bits at positions 0, 1, and 2 one by one. How many bits are set to 1 at the end?
A2
B0
C3
D1
Attempts:
2 left
💡 Hint
Toggling a bit flips it from 0 to 1 or 1 to 0.

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