Challenge - 5 Problems
Bit Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of reversing bits for 8-bit number 13
What is the output of reversing the bits of the 8-bit number 13 (00001101 in binary)?
DSA Rust
fn reverse_bits(mut n: u8) -> u8 {
let mut result = 0;
for _ in 0..8 {
result <<= 1;
result |= n & 1;
n >>= 1;
}
result
}
fn main() {
let num = 13u8;
let reversed = reverse_bits(num);
println!("{}", reversed);
}Attempts:
2 left
💡 Hint
Think about how bits shift from right to left and how the reversed bits form a new number.
✗ Incorrect
The binary of 13 is 00001101. Reversing bits gives 10110000 which is 176 in decimal.
❓ Predict Output
intermediateOutput of reversing bits for 16-bit number 4660
What is the output of reversing the bits of the 16-bit number 4660 (0001001000110100 in binary)?
DSA Rust
fn reverse_bits(mut n: u16) -> u16 {
let mut result = 0;
for _ in 0..16 {
result <<= 1;
result |= n & 1;
n >>= 1;
}
result
}
fn main() {
let num = 4660u16;
let reversed = reverse_bits(num);
println!("{}", reversed);
}Attempts:
2 left
💡 Hint
Reverse the bits carefully and convert back to decimal.
✗ Incorrect
4660 in binary is 0001001000110100. Reversed bits are 0010110001001000 which is 11336 decimal.
🔧 Debug
advancedIdentify the error in this bit reversal code
What error does this Rust code produce when reversing bits of a u8 number?
DSA Rust
fn reverse_bits(mut n: u8) -> u8 {
let mut result = 0;
for _ in 0..8 {
result <<= 1;
result |= n & 1;
n >>= 1;
}
result
}
fn main() {
let num = 5u8;
let reversed = reverse_bits(num);
println!("{}", reversed);
}Attempts:
2 left
💡 Hint
Check if all statements end properly in Rust syntax.
✗ Incorrect
Missing semicolon after 'result <<= 1' causes a syntax error.
❓ Predict Output
advancedOutput of reversing bits for 32-bit number 4294967295
What is the output of reversing the bits of the 32-bit number 4294967295 (all bits set to 1)?
DSA Rust
fn reverse_bits(mut n: u32) -> u32 {
let mut result = 0;
for _ in 0..32 {
result <<= 1;
result |= n & 1;
n >>= 1;
}
result
}
fn main() {
let num = 4294967295u32;
let reversed = reverse_bits(num);
println!("{}", reversed);
}Attempts:
2 left
💡 Hint
All bits set to 1 reversed remain the same.
✗ Incorrect
Reversing all 32 bits set to 1 results in the same number 4294967295.
🧠 Conceptual
expertWhy is reversing bits useful in algorithms?
Which of the following is the best reason why reversing bits of a number is useful in algorithms?
Attempts:
2 left
💡 Hint
Think about algorithms that require bit-reversed ordering.
✗ Incorrect
Bit reversal is crucial in FFT algorithms to reorder input data for efficient computation.
