Challenge - 5 Problems
Bit Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat 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);
Attempts:
2 left
💡 Hint
Think about how the bits move from right to left when reversed.
✗ Incorrect
The binary of 13 is 00001101. Reversing bits gives 10110000 which is 176 in decimal.
❓ Predict Output
intermediateWhat 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);
Attempts:
2 left
💡 Hint
Reverse the bits of 0001 in 4 bits.
✗ Incorrect
0001 reversed is 1000 which is 8 in decimal.
🔧 Debug
advancedWhy 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);Attempts:
2 left
💡 Hint
Check how unsigned integers behave with bit shifts.
✗ Incorrect
The function correctly reverses bits for 32-bit unsigned integers. Input with all bits set returns same value.
🧠 Conceptual
advancedHow 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)?
Attempts:
2 left
💡 Hint
Consider a number with bits mostly 1s except one bit.
✗ Incorrect
Maximum flips occur when all bits except one are 1 and that one bit is 0. Reversing flips all but the middle bit in 8 bits, so 7 bits flip.
🚀 Application
expertWhat 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);Attempts:
2 left
💡 Hint
Reverse the bits carefully or use the function to verify.
✗ Incorrect
Reversing the bits results in 0b11110000111100001111000111011011 which is 4026531823 decimal.
