Challenge - 5 Problems
Reverse Bits 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 the following Java code that reverses the bits of the number 13 (in 8-bit representation)?
DSA Java
public class ReverseBits { public static int reverseBits(int n) { int result = 0; for (int i = 0; i < 8; i++) { result <<= 1; result |= (n & 1); n >>= 1; } return result; } public static void main(String[] args) { int n = 13; // binary 00001101 int reversed = reverseBits(n); System.out.println(reversed); } }
Attempts:
2 left
💡 Hint
Think about reversing the bits of 00001101 in 8 bits.
✗ Incorrect
The binary of 13 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 the following Java code that reverses the bits of the number 16 (in 8-bit representation)?
DSA Java
public class ReverseBits { public static int reverseBits(int n) { int result = 0; for (int i = 0; i < 8; i++) { result <<= 1; result |= (n & 1); n >>= 1; } return result; } public static void main(String[] args) { int n = 16; // binary 00010000 int reversed = reverseBits(n); System.out.println(reversed); } }
Attempts:
2 left
💡 Hint
Reverse the bits of 00010000 in 8 bits.
✗ Incorrect
The binary of 16 is 00010000. Reversing the bits gives 00001000 which is 8 in decimal.
🧠 Conceptual
advancedUnderstanding bit shifting in reverse bits algorithm
In the reverse bits algorithm, why do we shift the result to the left before OR-ing with the last bit of the input number?
Attempts:
2 left
💡 Hint
Think about how bits are added one by one from right to left.
✗ Incorrect
Shifting the result left moves all bits one position to the left, making space on the right to add the next bit from the input number.
🔧 Debug
advancedIdentify the error in this reverse bits code
What error will this Java code produce when trying to reverse bits of an 8-bit number?
DSA Java
public class ReverseBits { public static int reverseBits(int n) { int result = 0; for (int i = 0; i <= 8; i++) { result <<= 1; result |= (n & 1); n >>= 1; } return result; } public static void main(String[] args) { int n = 5; System.out.println(reverseBits(n)); } }
Attempts:
2 left
💡 Hint
Check the loop condition carefully.
✗ Incorrect
The loop runs 9 times (i from 0 to 8 inclusive) instead of 8 times, causing an extra shift and bit addition, resulting in wrong output.
🚀 Application
expertReverse bits of a 32-bit integer and output as unsigned
Given the following Java code to reverse bits of a 32-bit integer, what is the output when input is 43261596? (Print as unsigned integer)
DSA Java
public class ReverseBits { public static int reverseBits(int n) { int result = 0; for (int i = 0; i < 32; i++) { result <<= 1; result |= (n & 1); n >>= 1; } return result; } public static void main(String[] args) { int n = 43261596; int reversed = reverseBits(n); System.out.println(Integer.toUnsignedString(reversed)); } }
Attempts:
2 left
💡 Hint
Use 32-bit reversal and print unsigned integer.
✗ Incorrect
Reversing bits of 43261596 (binary 00000010100101000001111010011100) results in 964176192 (binary 00111001011110000010100101000000).
