Challenge - 5 Problems
Bitwise Even-Odd Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Rust code checking even or odd using bits?
Consider the following Rust code that uses bitwise operations to check if a number is even or odd. What will it print?
DSA Rust
fn main() {
let num = 7;
if num & 1 == 0 {
println!("Even");
} else {
println!("Odd");
}
}Attempts:
2 left
💡 Hint
The bitwise AND with 1 checks the least significant bit of the number.
✗ Incorrect
The expression 'num & 1' extracts the least significant bit. If it is 0, the number is even; if 1, it is odd. Since 7 in binary ends with 1, it is odd.
❓ Predict Output
intermediateWhat is the output when checking even or odd for zero using bits?
What will the following Rust code print when checking if zero is even or odd using bitwise AND?
DSA Rust
fn main() {
let num = 0;
if num & 1 == 0 {
println!("Even");
} else {
println!("Odd");
}
}Attempts:
2 left
💡 Hint
Zero is an even number.
✗ Incorrect
Zero's least significant bit is 0, so 'num & 1' equals 0, making the condition true and printing "Even".
❓ Predict Output
advancedWhat is the output of this Rust code using bitwise NOT to check even or odd?
Analyze the following Rust code that uses bitwise NOT and AND to determine if a number is even or odd. What will it print?
DSA Rust
fn main() {
let num = 4;
if (!num) & 1 == 0 {
println!("Even");
} else {
println!("Odd");
}
}Attempts:
2 left
💡 Hint
The bitwise NOT operator '!' is a logical NOT in Rust, not bitwise NOT.
✗ Incorrect
In Rust, '!' is a logical NOT operator and cannot be applied to integers. This causes a compilation error.
❓ Predict Output
advancedWhat is the output of this Rust code checking even or odd with a negative number using bits?
What will the following Rust code print when checking if -3 is even or odd using bitwise AND?
DSA Rust
fn main() {
let num = -3;
if num & 1 == 0 {
println!("Even");
} else {
println!("Odd");
}
}Attempts:
2 left
💡 Hint
Bitwise AND with 1 checks the least significant bit even for negative numbers in two's complement.
✗ Incorrect
In two's complement, -3 ends with a 1 bit, so 'num & 1' equals 1, printing "Odd".
🧠 Conceptual
expertWhy does using bitwise AND with 1 correctly determine even or odd numbers?
Which explanation best describes why the expression 'num & 1' can be used to check if a number is even or odd?
Attempts:
2 left
💡 Hint
Think about how numbers are stored in binary and what the least significant bit represents.
✗ Incorrect
In binary, even numbers always end with 0 and odd numbers end with 1. The bitwise AND with 1 extracts this last bit, revealing parity.
