What bit operation can be used to check if a number is even or odd?
The bitwise AND operation with 1 (number & 1) is used. If the result is 0, the number is even; if 1, the number is odd.
Click to reveal answer
beginner
Why does checking the least significant bit (LSB) tell us if a number is even or odd?
Because in binary, even numbers always end with 0 and odd numbers end with 1 in their least significant bit.
Click to reveal answer
beginner
Rust code snippet: How to check if a number `n` is odd using bits?
if n & 1 == 1 {
println!("Odd");
} else {
println!("Even");
}
Click to reveal answer
beginner
What is the output of checking 10 & 1 in binary?
10 in binary is 1010. 1010 & 0001 = 0000, so the result is 0, meaning 10 is even.
Click to reveal answer
intermediate
Is using bitwise operations to check even or odd faster than using modulo (%)?
Yes, bitwise operations are generally faster because they work directly on bits without division.
Click to reveal answer
Which bitwise operation helps to check if a number is odd or even?
AAND with 1
BOR with 1
CXOR with 1
DNOT operation
✗ Incorrect
AND with 1 isolates the least significant bit, which determines if a number is odd or even.
What does the expression `n & 1` return if `n` is even?
A1
B-1
Cn
D0
✗ Incorrect
For even numbers, the least significant bit is 0, so `n & 1` returns 0.
In Rust, which of these prints "Odd" for an odd number `n`?
Aif n | 1 == 1 { println!("Odd"); }
Bif n & 1 == 0 { println!("Odd"); }
Cif n & 1 == 1 { println!("Odd"); }
Dif n ^ 1 == 1 { println!("Odd"); }
✗ Incorrect
Checking if `n & 1` equals 1 means the number is odd.
What is the binary representation of the number 7?
A111
B101
C110
D100
✗ Incorrect
7 in binary is 111.
Why is bitwise checking for even/odd preferred in some cases over modulo?
AIt uses less memory
BIt is faster and uses simpler CPU instructions
CIt is easier to read
DIt works only for positive numbers
✗ Incorrect
Bitwise operations are faster because they use simple CPU instructions without division.
Explain how to check if a number is even or odd using bitwise operations.
Think about the last bit in binary numbers.
You got /4 concepts.
Write a simple Rust code snippet to print whether a number is even or odd using bits.
Use if condition with n & 1.
You got /3 concepts.
Practice
(1/5)
1. What does the expression num & 1 check when determining if a number is even or odd?
easy
A. It checks the least significant bit of the number
B. It checks if the number is greater than 1
C. It divides the number by 2
D. It adds 1 to the number
Solution
Step 1: Understand bitwise AND operation
The expression num & 1 performs a bitwise AND between the number and 1, which isolates the least significant bit (rightmost bit).
Step 2: Interpret the least significant bit
If the least significant bit is 1, the number is odd; if it is 0, the number is even.
Final Answer:
It checks the least significant bit of the number -> Option A
Quick Check:
Least significant bit = odd/even check [OK]
Hint: Check last bit with & 1 to find odd or even fast [OK]
Common Mistakes:
Thinking & 1 divides the number
Confusing bitwise AND with addition
Assuming it checks if number > 1
2. Which Rust code snippet correctly checks if a number n is odd using bitwise operations?
easy
A. if n ^ 1 == 1 { println!("Odd"); }
B. if n | 1 == 1 { println!("Odd"); }
C. if n & 0 == 1 { println!("Odd"); }
D. if n & 1 == 1 { println!("Odd"); }
Solution
Step 1: Identify correct bitwise operator for odd check
The bitwise AND operator & with 1 isolates the least significant bit, which indicates oddness.
Step 2: Verify condition correctness
Checking if n & 1 == 1 means the number is odd. Other operators like OR | or XOR ^ do not correctly check oddness.
Final Answer:
if n & 1 == 1 { println!("Odd"); } -> Option D
Quick Check:
Use & 1 == 1 to check odd [OK]
Hint: Use & 1 == 1 to check odd numbers in Rust [OK]
Common Mistakes:
Using OR or XOR instead of AND
Comparing with 0 instead of 1
Using bitwise operators incorrectly
3. What will be the output of this Rust code?
let num = 14;
if num & 1 == 0 {
println!("Even");
} else {
println!("Odd");
}
medium
A. Even
B. Odd
C. 14
D. Compilation error
Solution
Step 1: Evaluate bitwise AND of 14 and 1
Binary of 14 is 1110, and 1 is 0001. 1110 & 0001 = 0.
Step 2: Check condition and print output
Since num & 1 == 0 is true, the code prints "Even".
Final Answer:
Even -> Option A
Quick Check:
14 & 1 = 0 means Even [OK]
Hint: If num & 1 equals 0, number is even [OK]
Common Mistakes:
Assuming 14 is odd
Confusing bitwise AND with arithmetic AND
Expecting output to be the number itself
4. Identify the error in this Rust code snippet that checks if a number is odd:
let n = 7;
if n & 1 = 1 {
println!("Odd");
} else {
println!("Even");
}
medium
A. Using & instead of && in if condition
B. Missing semicolon after println!
C. Using single = instead of == in if condition
D. Variable n is not declared
Solution
Step 1: Check the if condition syntax
The condition uses single equals = which is assignment, not comparison.
Step 2: Correct the comparison operator
It should use double equals == to compare n & 1 with 1.
Final Answer:
Using single = instead of == in if condition -> Option C
Quick Check:
Use == for comparison, not = [OK]
Hint: Use == for comparison, not = assignment [OK]
Common Mistakes:
Confusing assignment (=) with equality (==)
Thinking & is logical AND instead of bitwise
Ignoring compiler error messages
5. Given a list of integers nums = [2, 3, 4, 5, 6], which Rust code correctly uses bitwise operations to filter only odd numbers and print them?
hard
A. for num in nums.iter() {
if num & 0 == 1 {
println!("{}", num);
}
}
B. for &num in &nums {
if num & 1 == 1 {
println!("{}", num);
}
}
C. for num in nums {
if num | 1 == 1 {
println!("{}", num);
}
}
D. for num in nums {
if num ^ 1 == 1 {
println!("{}", num);
}
}
Solution
Step 1: Understand iteration and bitwise odd check
Iterate over references to numbers and use num & 1 == 1 to check odd numbers.
Step 2: Verify correct syntax and logic
for &num in &nums {
if num & 1 == 1 {
println!("{}", num);
}
} correctly iterates with for &num in &nums and checks oddness with bitwise AND. Other options misuse operators or iteration.
Final Answer:
for &num in &nums {
if num & 1 == 1 {
println!("{}", num);
}
} -> Option B
Quick Check:
Use & 1 == 1 inside loop over &nums [OK]
Hint: Iterate references and use & 1 == 1 to print odd numbers [OK]
Common Mistakes:
Using wrong bitwise operators like | or ^
Iterating without references causing ownership errors
Checking with & 0 instead of & 1
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.