Reverse Bits
EasyBit Manipulation
Treat n as a 32-bit unsigned integer, writing out all 32 bits including leading zeros. Reverse the order of those 32 bits (bit 0 swaps with bit 31, bit 1 with bit 30, and so on) and return the resulting value as a non-negative integer.
For example, n = 8 is ...00001000; reversed, the single 1 lands at bit 28, giving 2^28 = 268435456.
Example 1
- Input:
- n = 6
- Output:
- 1610612736
- Explanation
- As 32 bits, 6 is
00000000000000000000000000000110. Reversed it is01100000000000000000000000000000, which is 1610612736. The leading zeros of the input become trailing zeros of the output.
Example 2
- Input:
- n = 1
- Output:
- 2147483648
- Explanation
- The single 1 in the lowest bit moves to the highest bit, giving 231 = 2147483648.
Example 3
- Input:
- n = 4294967294
- Output:
- 2147483647
- Explanation
- Every bit is 1 except the lowest. After reversing, every bit is 1 except the highest, which is 2147483647.
Constraints
0 <= n <= 2^32 - 1nis always treated as exactly 32 bits