What does it mean to set a specific bit in a number?
Setting a bit means changing that bit to 1, regardless of its previous value.
Click to reveal answer
beginner
How do you clear a specific bit in a number?
Clearing a bit means changing that bit to 0, regardless of its previous value.
Click to reveal answer
beginner
What does it mean to toggle a specific bit?
Toggling a bit means changing it from 0 to 1 or from 1 to 0 (flipping its value).
Click to reveal answer
intermediate
In Rust, which operator is used to set a bit at position n?
Use the bitwise OR operator (|) with 1 shifted left by n: number | (1 << n).
Click to reveal answer
intermediate
How do you clear a bit at position n in Rust?
Use bitwise AND with the negation of 1 shifted left by n: number & !(1 << n).
Click to reveal answer
Which Rust expression sets the 3rd bit (0-based) of a number 'num'?
Anum & (1 << 3)
Bnum | (1 << 3)
Cnum ^ (1 << 3)
Dnum & !(1 << 3)
✗ Incorrect
Setting a bit uses OR (|) with 1 shifted left by the bit position.
How do you clear the 2nd bit of 'num' in Rust?
Anum | (1 << 2)
Bnum & (1 << 2)
Cnum & !(1 << 2)
Dnum ^ (1 << 2)
✗ Incorrect
Clearing a bit uses AND (&) with the negation of 1 shifted left by the bit position.
What does the XOR (^) operator do when used with 1 << n on a number?
AToggles the nth bit
BClears the nth bit to 0
CSets the nth bit to 1
DLeaves the number unchanged
✗ Incorrect
XOR with 1 flips the bit: 0 becomes 1, 1 becomes 0.
If a number is 0b0101 and you toggle the 0th bit, what is the new number in binary?
A0b0111
B0b0101
C0b0001
D0b0100
✗ Incorrect
Toggling the 0th bit flips it from 1 to 0, so 0b0101 becomes 0b0100.
Which operation would you use to check if the 4th bit of a number is set?
Anumber & (1 << 4)
Bnumber | (1 << 4)
Cnumber ^ (1 << 4)
Dnumber & !(1 << 4)
✗ Incorrect
AND with 1 shifted left by 4 isolates the 4th bit to check if it is 1.
Explain how to set, clear, and toggle a specific bit in a number using Rust operators.
Think about how each bitwise operator affects bits.
You got /3 concepts.
Describe a real-life example where toggling a bit might be useful.
Consider a light switch or a yes/no setting.
You got /3 concepts.
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
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.
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.
Final Answer:
Sets the bit at position pos to 1 -> Option A
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
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.
Step 2: AND operation clears bit at pos
Using num & !(1 << pos) clears the bit at pos to 0, keeping other bits unchanged.
Final Answer:
num & !(1 << pos) -> Option B
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
Step 1: Understand initial value and bit position
Initial num is binary 1010 (decimal 10). Position pos is 1 (counting from 0 at right).
Step 2: Toggle bit at position 1 using XOR
1 << pos is 0010. XOR toggles bit 1: 1010 XOR 0010 = 1000 (decimal 8).
Final Answer:
1000 -> Option A
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
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.
Step 2: Analyze the second operation (XOR)
num ^ (1 << pos) toggles bit 2 from 1 back to 0, resulting in 1001.
Step 3: Check final output
After both operations, bit 2 was set then toggled back, so no net change occurs.
Final Answer:
The bit is toggled twice, so no change occurs -> Option C
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
Step 1: Initial value and bit positions
Initial num is 11001100 (bits 7 to 0). Positions to toggle: 2, 3, 7.
Step 2: Toggle bit 2
Bit 2 is 1 (counting from 0 rightmost). Toggle flips it to 0: 11001100 -> 11001000.
Step 3: Toggle bit 3
Bit 3 is 1. Toggle flips it to 0: 11001000 -> 11000000.
Step 4: Toggle bit 7
Bit 7 is 1. Toggle flips it to 0: 11000000 -> 01000000.
Step 5: Final binary value
Final value is 01000000 in binary (bits 7,3,2 toggled to 0, others unchanged).
Final Answer:
01000000 -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.