Reverse Bits - Problem
Reverse bits of a given 32-bit unsigned integer.
Note: Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned.
In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 2 below, the input represents the signed integer -3 and the output represents the signed integer -1073741825.
Input & Output
Example 1 — Basic Case
$
Input:
n = 43261596
›
Output:
964176192
💡 Note:
Binary of 43261596 is 00000010100101000001111010011100. Reversing gives 00111001011110000010100101000000 which is 964176192 in decimal.
Example 2 — Negative Number
$
Input:
n = -3
›
Output:
-1073741825
💡 Note:
Binary of -3 (in 32-bit) is 11111111111111111111111111111101. Reversing gives 10111111111111111111111111111111 which is -1073741825 in decimal.
Example 3 — Edge Case
$
Input:
n = 1
›
Output:
-2147483648
💡 Note:
Binary of 1 is 00000000000000000000000000000001. Reversing gives 10000000000000000000000000000000 which is -2147483648 in decimal.
Constraints
- The input must be a 32-bit unsigned integer
- -2³¹ ≤ n ≤ 2³¹ - 1
Visualization
Tap to expand
Understanding the Visualization
1
Input
32-bit integer representation
2
Process
Reverse the order of all 32 bits
3
Output
Reversed bit pattern as integer
Key Takeaway
🎯 Key Insight: Use bit manipulation to efficiently extract and reposition each bit without string conversions
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code