You are given a non-negative integer n that fits in 32 bits. Treat it as an unsigned 32‑bit value and reverse the order of every bit in that 32‑bit pattern. The bit that originally sat at the least significant position is moved to the most significant position, the next bit becomes the second most significant, and so on through all 32 positions. The reversal includes all leading zeros: for example, 1 is represented with 31 zero bits above its only set bit, and all those zeros shift to new locations after the reversal. Return the resulting unsigned integer.
Example 1:
Input: n = 1
Output: 2147483648
Explanation: 00000000000000000000000000000001 becomes 10000000000000000000000000000000.
n = 12147483648
The input n = 1 is stored in 32 bits as 00000000000000000000000000000001. The least significant bit (rightmost) is 1.
Example 2:
Input: n = 6
Output: 1610612736
Explanation: 00000000000000000000000000000110 becomes 01100000000000000000000000000000.
Example 3:
Input: n = 4294967293
Output: 3221225471
Explanation: 11111111111111111111111111111101 becomes 10111111111111111111111111111111.
Constraints:
0 <= n <= 2^32 - 1Follow‑up: If this function were called very frequently, how would you speed it up?
n = 12147483648
The input n = 1 is stored in 32 bits as 00000000000000000000000000000001. The least significant bit (rightmost) is 1.