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 a small number
What is the output of reversing the bits of the number 13 (in 8-bit representation)?
DSA Javascript
function reverseBits(num) {
let result = 0;
for (let i = 0; i < 8; i++) {
result <<= 1;
result |= (num & 1);
num >>= 1;
}
return result;
}
console.log(reverseBits(13));Attempts:
2 left
💡 Hint
Think about the binary form of 13 and reversing its bits in 8-bit form.
✗ Incorrect
The number 13 in 8-bit binary is 00001101. Reversing the bits gives 10110000 which is 176 in decimal.
❓ Predict Output
intermediateOutput of reversing bits for a power of two
What is the output of reversing the bits of the number 16 (in 8-bit representation)?
DSA Javascript
function reverseBits(num) {
let result = 0;
for (let i = 0; i < 8; i++) {
result <<= 1;
result |= (num & 1);
num >>= 1;
}
return result;
}
console.log(reverseBits(16));Attempts:
2 left
💡 Hint
16 in binary is 00010000. Reverse the bits carefully.
✗ Incorrect
16 in 8-bit binary is 00010000. Reversing bits gives 00001000 which is 8 in decimal.
🔧 Debug
advancedIdentify the error in bit reversal code
What error does the following code produce when reversing bits of a number?
DSA Javascript
function reverseBits(num) {
let result = 0;
for (let i = 0; i < 8; i++) {
result = result << 1 | num & 1
num = num >> 1
}
return result
}
console.log(reverseBits(5));Attempts:
2 left
💡 Hint
JavaScript allows missing semicolons in many cases.
✗ Incorrect
The code runs fine and outputs 160, which is the reversed bits of 5 (00000101 reversed to 10100000).
❓ Predict Output
advancedOutput of reversing bits for a 16-bit number
What is the output of reversing the bits of the number 4660 (in 16-bit representation)?
DSA Javascript
function reverseBits16(num) {
let result = 0;
for (let i = 0; i < 16; i++) {
result <<= 1;
result |= (num & 1);
num >>= 1;
}
return result;
}
console.log(reverseBits16(4660));Attempts:
2 left
💡 Hint
4660 in hex is 0x1234. Reverse its bits carefully in 16-bit form.
✗ Incorrect
4660 decimal is 0001001000110100 in binary. Reversing bits gives 0010110001001000 which is 11336 decimal.
🧠 Conceptual
expertNumber of set bits after reversing bits
If a number has 5 bits set to 1 in its 8-bit binary form, how many bits will be set to 1 after reversing its bits?
Attempts:
2 left
💡 Hint
Reversing bits changes their positions but not their count.
✗ Incorrect
Reversing bits only changes the order of bits, not the number of bits set to 1.
