n is a signed 32-bit integer, but its bits are treated as a fixed-width 32-bit two's-complement pattern. A two's-complement pattern uses bit 31 as the sign bit and bits 30 through 0 for the remaining value representation; leading zero bits are part of the pattern.
Reverse the order of all 32 bits in n: the original bit at position 0 becomes position 31, the original bit at position 1 becomes position 30, and so on. Interpret the resulting 32-bit pattern as a signed two's-complement integer.
Implement Solution.reverseBits(n) and return that signed integer. Both n and the returned value are signed 32-bit integers.
Example 1:
Input: n = 1
Output: -2147483648
Explanation: The 32-bit pattern for 1 has only its least significant bit set. After reversal, that set bit occupies position 31, which represents -2^31 as a signed 32-bit integer.
Example 2:
Input: n = 5
Output: -1610612736
Explanation: The set bits of 5 are at positions 0 and 2. They move to positions 31 and 29, producing the signed 32-bit value -1610612736.
Example 3:
Input: n = -2
Output: 2147483647
Explanation: The 32-bit pattern for -2 is all ones except for its least significant bit. Reversing it gives a pattern with bit 31 clear and every lower bit set, equal to 2147483647.
Constraints:
-2^31 <= n <= 2^31 - 1 is an integer is represented using exactly 32 bits, including leading zero bitsThe returned value is interpreted as a signed 32-bit two's-complement integer-2^31 <= returned value <= 2^31 - 1Need help or want more practice?