Bird
Raised Fist0
DSA C#programming~20 mins

Reverse Bits of a Number in DSA C# - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Bit Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of reversing bits of 13 (00001101) in 8 bits?
Given the 8-bit binary representation of the number 13, what is the decimal value after reversing its bits?
DSA C#
int ReverseBits(int n) {
    int result = 0;
    for (int i = 0; i < 8; i++) {
        result <<= 1;
        result |= (n & 1);
        n >>= 1;
    }
    return result;
}

int input = 13;
int output = ReverseBits(input);
Console.WriteLine(output);
A104
B212
C134
D176
Attempts:
2 left
💡 Hint
Think about how the bits move from right to left when reversed.
❓ Predict Output
intermediate
2:00remaining
What is the output of reversing bits of 1 in 4 bits?
Given the 4-bit binary representation of the number 1, what is the decimal value after reversing its bits?
DSA C#
int ReverseBits4(int n) {
    int result = 0;
    for (int i = 0; i < 4; i++) {
        result <<= 1;
        result |= (n & 1);
        n >>= 1;
    }
    return result;
}

int input = 1;
int output = ReverseBits4(input);
Console.WriteLine(output);
A8
B4
C1
D2
Attempts:
2 left
💡 Hint
Reverse the bits of 0001 in 4 bits.
🔧 Debug
advanced
2:30remaining
Why does this ReverseBits function produce wrong output for 32-bit numbers?
Consider this C# function to reverse bits of a 32-bit unsigned integer. It returns wrong results for some inputs. Identify the bug.
DSA C#
uint ReverseBits(uint n) {
    uint result = 0;
    for (int i = 0; i < 32; i++) {
        result <<= 1;
        result |= (n & 1);
        n >>= 1;
    }
    return result;
}

uint input = 4294967295; // all bits 1
uint output = ReverseBits(input);
Console.WriteLine(output);
ANo bug, output is 4294967295
BBug: result should be shifted after OR operation
CBug: Using signed int for n causes wrong shifts
DBug: The loop should run 16 times, not 32
Attempts:
2 left
💡 Hint
Check how unsigned integers behave with bit shifts.
🧠 Conceptual
advanced
1:30remaining
How many bits are flipped when reversing bits of a number?
If you reverse the bits of a 8-bit number, what is the maximum number of bits that can change their value (flip from 0 to 1 or 1 to 0)?
A8
B7
C6
D4
Attempts:
2 left
💡 Hint
Consider a number with bits mostly 1s except one bit.
🚀 Application
expert
3:00remaining
What is the output after reversing bits of 0b10110011100011110000111100001111 (32 bits)?
Given the 32-bit binary number 10110011100011110000111100001111, what is the decimal value after reversing its bits?
DSA C#
uint ReverseBits(uint n) {
    uint result = 0;
    for (int i = 0; i < 32; i++) {
        result <<= 1;
        result |= (n & 1);
        n >>= 1;
    }
    return result;
}

uint input = 0b10110011100011110000111100001111;
uint output = ReverseBits(input);
Console.WriteLine(output);
A4026531833
B4026531855
C4026531823
D4026531847
Attempts:
2 left
💡 Hint
Reverse the bits carefully or use the function to verify.

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. Shifting bits to the left by one position
D. Flipping the order of bits from left to right

Solution

  1. Step 1: Understand bit order

    Bits are arranged from left (most significant) to right (least significant).
  2. Step 2: Reverse means flip order

    Reversing bits means the leftmost bit becomes rightmost and vice versa.
  3. Final Answer:

    Flipping the order of bits from left to right -> Option D
  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 of the following C# code snippets correctly reverses the bits of a 4-bit number stored in an integer variable n?
easy
A. int reversed = 0; for(int i=0; i<4; i++) { reversed <<= 1; reversed |= (n & 1); n >>= 1; }
B. int reversed = 0; for(int i=0; i<4; i++) { reversed |= (n & 1) << i; n >>= 1; }
C. int reversed = n << 4;
D. int reversed = ~n;

Solution

  1. Step 1: Analyze the loop logic in int reversed = 0; for(int i=0; i<4; i++) { reversed <<= 1; reversed |= (n & 1); n >>= 1; }

    It shifts reversed left by 1, adds least bit of n, then shifts n right by 1, effectively reversing bits.
  2. Step 2: Check other options

    int reversed = 0; for(int i=0; i<4; i++) { reversed |= (n & 1) << i; n >>= 1; } does not reverse bits, it copies bits in same order. int reversed = n << 4; shifts bits left, not reverse. int reversed = ~n; inverts bits, not reverse.
  3. Final Answer:

    Option A code correctly reverses bits -> Option A
  4. Quick Check:

    Shift left + OR + shift right = reverse bits [OK]
Hint: Shift reversed left, OR with last bit, shift input right [OK]
Common Mistakes:
  • Using bitwise NOT (~) instead of reversing bits
  • Shifting bits without combining properly
  • Not updating reversed variable inside loop
3. What is the output of the following C# code snippet?
int n = 13; // binary 1101
int reversed = 0;
for (int i = 0; i < 4; i++) {
  reversed <<= 1;
  reversed |= (n & 1);
  n >>= 1;
}
Console.WriteLine(reversed);
medium
A. 11
B. 7
C. 13
D. 14

Solution

  1. Step 1: Convert 13 to 4-bit binary

    13 in binary (4 bits) is 1101.
  2. Step 2: Reverse bits step-by-step

    Bits: 1 1 0 1 reversed: 1 0 1 1 which is binary 1011 = decimal 11.
  3. Final Answer:

    11 -> Option A
  4. Quick Check:

    1101 reversed = 1011 = 11 [OK]
Hint: Reverse bits manually and convert to decimal [OK]
Common Mistakes:
  • Not limiting to 4 bits, causing wrong output
  • Confusing bit order when reversing
  • Assuming reversed number equals original
4. Identify the error in the following C# code that attempts to reverse bits of an 8-bit number:
int ReverseBits(int n) {
  int reversed = 0;
  for (int i = 0; i < 8; i++) {
    reversed |= (n & 1) << i;
    n >>= 1;
  }
  return reversed;
}
medium
A. The variable reversed should be initialized to 1
B. The loop should run 16 times for 8-bit numbers
C. The bits are not reversed; they are copied in the same order
D. The shift operator << is used incorrectly

Solution

  1. Step 1: Analyze bit placement in loop

    The code places bits at increasing positions without reversing order.
  2. Step 2: Understand reversing bits logic

    Reversing requires shifting reversed left and OR with last bit, not shifting bits by i.
  3. Final Answer:

    The bits are not reversed; they are copied in the same order -> Option C
  4. Quick Check:

    Shifting bits by i copies order, not reverse [OK]
Hint: Reversing needs shifting reversed left each step, not shifting bits by i [OK]
Common Mistakes:
  • Using bit shift by loop index instead of shifting reversed
  • Incorrect loop count for bit size
  • Initializing reversed incorrectly
5. You want to reverse the bits of a 16-bit unsigned integer and then check if the reversed number is a palindrome in binary (reads the same forwards and backwards). Which approach correctly combines reversing bits and palindrome check?
hard
A. Invert bits using bitwise NOT, then check if inverted equals original
B. Reverse bits using a loop shifting reversed left and OR with last bit, then compare reversed with original
C. Shift original number left by 16 bits, then compare with original
D. Reverse bits by shifting bits by index, then check if reversed equals original

Solution

  1. Step 1: Reverse bits correctly

    Use loop to shift reversed left, OR with last bit of original, shift original right.
  2. Step 2: Check palindrome by comparing reversed and original

    If reversed equals original, bits form a palindrome.
  3. Final Answer:

    Reverse bits using a loop shifting reversed left and OR with last bit, then compare reversed with original -> Option B
  4. Quick Check:

    Reverse bits + compare = palindrome check [OK]
Hint: Reverse bits properly, then compare to original for palindrome [OK]
Common Mistakes:
  • Using bitwise NOT instead of reversing bits
  • Shifting bits incorrectly causing wrong reverse
  • Comparing inverted bits instead of reversed bits
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