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
What is the output of the following C++ code that reverses the bits of an 8-bit unsigned integer 13?
DSA C++
unsigned char reverseBits(unsigned char n) {
unsigned char result = 0;
for (int i = 0; i < 8; i++) {
result <<= 1;
result |= (n & 1);
n >>= 1;
}
return result;
}
#include <iostream>
int main() {
unsigned char x = 13; // binary 00001101
unsigned char y = reverseBits(x);
std::cout << (int)y << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Think about how bits shift and how the least significant bit moves to the most significant position.
✗ 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
What is the output of reversing bits of the 16-bit unsigned integer 4660 using the given function?
DSA C++
unsigned short reverseBits16(unsigned short n) {
unsigned short result = 0;
for (int i = 0; i < 16; i++) {
result <<= 1;
result |= (n & 1);
n >>= 1;
}
return result;
}
#include <iostream>
int main() {
unsigned short x = 4660; // binary 0001001000110100
unsigned short y = reverseBits16(x);
std::cout << y << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Convert 4660 to binary, reverse bits, then convert back to decimal.
✗ Incorrect
4660 in binary is 0001001000110100. Reversing bits gives 0010110001001000 which is 11336 decimal.
🔧 Debug
advancedIdentify the error in bit reversal code
What error does the following C++ code produce when trying to reverse bits of an 8-bit number?
DSA C++
unsigned char reverseBits(unsigned char n) {
unsigned char result = 0;
for (int i = 0; i <= 8; i++) {
result <<= 1;
result |= (n & 1);
n >>= 1;
}
return result;
}Attempts:
2 left
💡 Hint
Check the loop condition carefully for how many times it runs.
✗ Incorrect
The loop runs 9 times (i from 0 to 8 inclusive) instead of 8 times, causing an extra shift and wrong result.
❓ Predict Output
advancedOutput of reversing bits for 32-bit number
What is the output of reversing bits of the 32-bit unsigned integer 43261596 using the given function?
DSA C++
unsigned int reverseBits32(unsigned int n) { unsigned int result = 0; for (int i = 0; i < 32; i++) { result <<= 1; result |= (n & 1); n >>= 1; } return result; } #include <iostream> int main() { unsigned int x = 43261596; // binary 00000010100101000001111010011100 unsigned int y = reverseBits32(x); std::cout << y << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Reverse the bits of the 32-bit number carefully and convert to decimal.
✗ Incorrect
The reversed bits of 43261596 are 00111001011110000010100101000000 which equals 964176192 decimal.
🧠 Conceptual
expertNumber of set bits after reversing
If you reverse the bits of a 64-bit unsigned integer, what can you say about the number of set bits (bits with value 1) in the reversed number compared to the original?
Attempts:
2 left
💡 Hint
Reversing bits only changes their order, not their count.
✗ Incorrect
Reversing bits only changes their positions, so the count of set bits remains unchanged.
