Reverse the bits of a 32-bit unsigned integer and return the resulting value.
Reverse Bits
Given a 32-bit unsigned integer, reverse its bits and return the integer you get after reversing.
Treat the input as a fixed-width 32-bit value. The leftmost bit becomes the rightmost bit, the second leftmost becomes the second rightmost, and so on.
For example, if the input bits are:
clike
00000010100101000001111010011100then the reversed bits are:
clike
00111001011110000010100101000000Return the decimal value of the reversed bit pattern.
Input Format
- A single integer
nrepresenting a 32-bit unsigned value. - You should consider only its lowest 32 bits.
Output Format
- Return one integer: the value obtained after reversing the 32 bits of
n.
Constraints
- Interpret the input as a 32-bit unsigned integer.
- The solution should work for all 32 bit positions.
- Constant extra space is expected.
Example 1
Input
n = 43261596
Output
964176192
Explanation
The binary form of 43261596 is 00000010100101000001111010011100. Reversing the 32 bits gives 00111001011110000010100101000000, which is 964176192.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.