Bird
Raised Fist0
DSA Rustprogramming~15 mins

Reverse Bits of a Number in DSA Rust - Build from Scratch

Choose your learning style10 modes available
Reverse Bits of a Number
📖 Scenario: You are working with binary numbers in a simple computer system. Sometimes, you need to reverse the order of bits in a number to fix how data is read.
🎯 Goal: Build a Rust program that takes an 8-bit number and reverses its bits. For example, if the number is 13 (binary 00001101), the reversed bits number is 176 (binary 10110000).
📋 What You'll Learn
Create a variable called num with the value 13u8.
Create a variable called reversed initialized to 0u8.
Use a for loop with variable i from 0 to 7 to reverse bits.
Inside the loop, shift bits and build the reversed number.
Print the reversed number using println!("{}", reversed).
💡 Why This Matters
🌍 Real World
Reversing bits is useful in low-level programming, data encoding, and communication protocols where bit order matters.
💼 Career
Understanding bitwise operations and loops is essential for embedded systems, systems programming, and performance-critical applications.
Progress0 / 4 steps
1
Create the initial number
Create a variable called num and set it to 13u8.
DSA Rust
Hint

Use let num: u8 = 13u8; to create the number.

2
Create a variable to hold the reversed bits
Create a variable called reversed and set it to 0u8 after the num variable.
DSA Rust
Hint

Use let reversed: u8 = 0u8; to start with zero reversed bits.

3
Reverse the bits using a for loop
Use a for loop with variable i from 0 to 7 to reverse bits. Inside the loop, update reversed by shifting it left by 1 and adding the least significant bit of num shifted right by i. Use reversed = (reversed << 1) | ((num >> i) & 1); inside the loop.
DSA Rust
Hint

Use for i in 0..8 and update reversed inside the loop as shown.

4
Print the reversed number
Print the value of reversed using println!("{}", reversed);.
DSA Rust
Hint

Use println!("{}", reversed); to show the reversed bits number.

Practice

(1/5)
1. What does the operation of reversing bits of a number mean in simple terms?
easy
A. Adding 1 to the binary number
B. Changing all 0s to 1s and 1s to 0s
C. Flipping the order of bits from left to right
D. Shifting bits to the left by one position

Solution

  1. Step 1: Understand bit representation

    Bits are the 0s and 1s that represent a number in binary form.
  2. Step 2: Define reversing bits

    Reversing bits means reading the bits from right to left instead of left to right, effectively flipping their order.
  3. Final Answer:

    Flipping the order of bits from left to right -> Option C
  4. Quick Check:

    Reverse bits = flip order [OK]
Hint: Reverse bits means flip their order, not invert values [OK]
Common Mistakes:
  • Confusing reversing bits with bitwise NOT (inverting bits)
  • Thinking reversing means shifting bits
  • Assuming reversing adds or subtracts from the number
2. Which Rust code snippet correctly reverses the bits of a 4-bit unsigned integer n?
easy
A. let mut rev = 0; for _ in 0..4 { rev <<= 1; rev |= n & 1; n >>= 1; }
B. let mut rev = 0; for _ in 0..4 { rev |= n & 1; rev <<= 1; n >>= 1; }
C. let mut rev = 0; for _ in 0..4 { rev += n & 1; n <<= 1; }
D. let mut rev = 0; for _ in 0..4 { rev <<= 1; rev |= n << 1; n >>= 1; }

Solution

  1. Step 1: Analyze bit reversal logic

    We shift rev left by 1, add the least significant bit of n, then shift n right by 1.
  2. Step 2: Check each option

    let mut rev = 0; for _ in 0..4 { rev <<= 1; rev |= n & 1; n >>= 1; } correctly does rev <<= 1; rev |= n & 1; n >>= 1; in loop. Others have wrong order or operations.
  3. Final Answer:

    let mut rev = 0; for _ in 0..4 { rev <<= 1; rev |= n & 1; n >>= 1; } -> Option A
  4. Quick Check:

    Shift rev left, add bit, shift n right [OK]
Hint: Shift rev left before adding bit, shift n right after [OK]
Common Mistakes:
  • Adding bit before shifting rev left
  • Shifting n left instead of right
  • Using addition instead of bitwise OR
3. What is the output of the following Rust code snippet?
fn reverse_bits(mut n: u8) -> u8 {
    let mut rev = 0;
    for _ in 0..8 {
        rev <<= 1;
        rev |= n & 1;
        n >>= 1;
    }
    rev
}

fn main() {
    let num = 13u8; // binary 00001101
    println!("{}", reverse_bits(num));
}
medium
A. 176
B. 11
C. 101
D. 208

Solution

  1. Step 1: Convert 13 to binary and reverse bits

    13 in 8 bits is 00001101. Reversing bits gives 10110000.
  2. Step 2: Convert reversed bits back to decimal

    10110000 binary equals 128+32+16=176 decimal.
  3. Final Answer:

    176 -> Option A
  4. Quick Check:

    00001101 reversed = 10110000 = 176 [OK]
Hint: Reverse bits visually, then convert binary to decimal [OK]
Common Mistakes:
  • Not using 8 bits, missing leading zeros
  • Confusing bit order or reversing incorrectly
  • Calculating decimal value wrong
4. Identify the bug in this Rust function that attempts to reverse bits of a 4-bit number:
fn reverse_bits(mut n: u8) -> u8 {
    let mut rev = 0;
    for _ in 0..4 {
        rev |= n & 1;
        rev <<= 1;
        n >>= 1;
    }
    rev
}
medium
A. Using bitwise OR instead of addition
B. Shifting rev left after adding bit causes incorrect result
C. Not shifting n left instead of right
D. Loop should run 8 times for 4-bit number

Solution

  1. Step 1: Analyze order of operations in loop

    rev |= n & 1 adds bit, then rev <<= 1 shifts left, causing last bit to be zeroed out.
  2. Step 2: Correct order is shift rev left first, then add bit

    Shifting rev left after adding bit loses the last bit added, so order must be reversed.
  3. Final Answer:

    Shifting rev left after adding bit causes incorrect result -> Option B
  4. Quick Check:

    Shift rev left before OR bit [OK]
Hint: Shift rev left before OR bit to avoid losing bits [OK]
Common Mistakes:
  • Adding bit before shifting rev left
  • Loop count mismatch for bit size
  • Shifting n left instead of right
5. Given a 16-bit unsigned integer, how would you efficiently reverse its bits using Rust's built-in methods combined with bitwise operations?
hard
A. Manually loop 16 times shifting bits and reconstruct reversed number
B. Use n.swap_bytes() to reverse bits
C. Convert number to string, reverse characters, then parse back
D. Use n.reverse_bits() method directly on the number

Solution

  1. Step 1: Know Rust's built-in bit reversal

    Rust's primitive integer types have a reverse_bits() method that reverses bits efficiently.
  2. Step 2: Compare options

    Use n.reverse_bits() method directly on the number uses the built-in method directly, which is efficient and simple. Others are manual or incorrect methods.
  3. Final Answer:

    Use n.reverse_bits() method directly on the number -> Option D
  4. Quick Check:

    Use built-in reverse_bits() for efficiency [OK]
Hint: Use built-in reverse_bits() method for fast bit reversal [OK]
Common Mistakes:
  • Trying manual loops instead of built-in method
  • Confusing byte swap with bit reversal
  • Using string conversion which is inefficient
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard