What does the bitwise AND operator (&) do in Rust?
It compares each bit of two numbers and returns a new number where each bit is 1 only if both bits in the original numbers were 1; otherwise, the bit is 0.
Click to reveal answer
beginner
Explain the bitwise OR operator (|) in Rust.
It compares each bit of two numbers and returns a new number where each bit is 1 if at least one of the bits in the original numbers was 1; otherwise, the bit is 0.
Click to reveal answer
beginner
What is the purpose of the bitwise XOR operator (^) in Rust?
It compares each bit of two numbers and returns a new number where each bit is 1 if the bits are different (one is 1 and the other is 0); if the bits are the same, the bit is 0.
Click to reveal answer
beginner
How does the bitwise NOT operator (~) work in Rust?
It flips every bit of a number: all 1 bits become 0, and all 0 bits become 1. Note: In Rust, the bitwise NOT operator is ~, not !.
Click to reveal answer
intermediate
What do the left shift (<<) and right shift (>>) operators do in Rust?
Left shift (<<) moves all bits in a number to the left by a specified number of places, adding zeros on the right. Right shift (>>) moves bits to the right, discarding bits on the right and filling in on the left depending on the type.
Click to reveal answer
What is the result of 5 & 3 in Rust? (5 = 0101, 3 = 0011 in binary)
A0
B1
C6
D7
✗ Incorrect
Bitwise AND compares bits: 0101 & 0011 = 0001, which is 1 in decimal.
Which operator flips all bits of a number in Rust?
A~
B|
C&
D^
✗ Incorrect
The ~ operator is the bitwise NOT operator that flips all bits.
What does the expression 4 << 1 evaluate to in Rust?
A8
B2
C5
D1
✗ Incorrect
Left shifting 4 (0100) by 1 bit gives 1000, which is 8.
Which bitwise operator returns 1 only if bits are different?
A&
B|
C~
D^
✗ Incorrect
The XOR (^) operator returns 1 when bits differ.
What is the result of 7 | 2 in Rust? (7 = 0111, 2 = 0010 in binary)
A5
B3
C7
D2
✗ Incorrect
Bitwise OR: 0111 | 0010 = 0111, which is 7.
Describe how the bitwise AND, OR, and XOR operators work with simple examples.
Think about comparing bits like switches being on or off.
You got /4 concepts.
Explain what happens when you use left shift (<<) and right shift (>>) operators on a number.
Imagine sliding beads on a string to the left or right.
You got /3 concepts.
Practice
(1/5)
1. Which bitwise operator in Rust returns 1 only if both bits are 1? Example: 5 & 3
easy
A. AND (&)
B. OR (|)
C. XOR (^)
D. NOT (!)
Solution
Step 1: Understand the AND operator
The AND operator (&) compares each bit of two numbers and returns 1 only if both bits are 1.
Step 2: Compare with other operators
OR (|) returns 1 if either bit is 1, XOR (^) returns 1 if bits differ, NOT (!) flips bits.
Final Answer:
AND (&) -> Option A
Quick Check:
AND returns 1 only if both bits are 1 [OK]
Hint: AND is the only operator needing both bits as 1 [OK]
Common Mistakes:
Confusing AND with OR which returns 1 if either bit is 1
Thinking XOR returns 1 if both bits are 1
Using NOT (!) as a binary operator instead of unary
2. Which of the following is the correct syntax for a bitwise XOR operation in Rust between variables a and b?
easy
A. a ! b
B. a & b
C. a | b
D. a ^ b
Solution
Step 1: Recall XOR operator symbol
In Rust, the bitwise XOR operator is represented by the caret symbol (^).
Step 2: Check other operators
& is AND, | is OR, and ! is unary NOT, so only a ^ b is correct for XOR.
Final Answer:
a ^ b -> Option D
Quick Check:
XOR uses ^ symbol in Rust [OK]
Hint: XOR uses ^ symbol, not & or | [OK]
Common Mistakes:
Using & or | instead of ^ for XOR
Trying to use ! as a binary operator
Confusing syntax with other languages
3. What is the output of this Rust code?
let x = 0b1010; // binary 10
let y = 0b1100; // binary 12
let result = x | y;
println!("{:04b}", result);
medium
A. 1000
B. 1110
C. 0110
D. 1010
Solution
Step 1: Convert binary to bits and apply OR
x = 1010, y = 1100. OR (|) returns 1 if either bit is 1.
1010 | 1100 = 1110
Step 2: Print result in 4-bit binary
Result is 1110 in binary, which matches 1110.
Final Answer:
1110 -> Option B
Quick Check:
Bitwise OR of 1010 and 1100 = 1110 [OK]
Hint: OR sets bits where either input bit is 1 [OK]
Common Mistakes:
Mixing up OR with AND results
Reading binary digits incorrectly
Forgetting to print in binary format
4. Identify the error in this Rust code snippet:
let a = 0b1010;
let b = 0b0101;
let c = a && b;
println!("{}", c);
medium
A. Incorrect binary literal syntax
B. Missing semicolon after let b
C. Using && instead of & for bitwise AND
D. println! macro missing format specifier
Solution
Step 1: Check operator usage
The code uses && which is a logical AND, not bitwise AND. Bitwise AND uses &.
Step 2: Verify other syntax
Semicolons are present, binary literals are correct, and println! has a format specifier, so no errors there.
Final Answer:
Using && instead of & for bitwise AND -> Option C
Quick Check:
Bitwise AND uses & not && [OK]
Hint: Use & for bitwise AND, && is logical AND [OK]
Common Mistakes:
Confusing logical AND (&&) with bitwise AND (&)
Assuming binary literals need prefix changes
Missing format specifier in println! macro
5. Given a number n, which Rust expression flips (toggles) the 3rd bit (counting from 0) of n using bitwise operators?
hard
A. n ^ (1 << 3)
B. n & (1 << 3)
C. n | (1 << 3)
D. ~n & (1 << 3)
Solution
Step 1: Understand bit flipping with XOR
XOR (^) with 1 flips the bit: if bit is 0 it becomes 1, if 1 it becomes 0. Shifting 1 by 3 sets the mask for the 3rd bit.
Step 2: Analyze other options
& with mask clears bits except the 3rd bit, | sets the 3rd bit to 1, ~n & mask is unrelated to flipping a single bit.
Final Answer:
n ^ (1 << 3) -> Option A
Quick Check:
XOR with bit mask flips that bit [OK]
Hint: XOR with 1 at bit position flips that bit [OK]
Common Mistakes:
Using & or | which clear or set bits instead of flipping
Confusing ~ (NOT) usage
Not shifting 1 to correct bit position
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.