Given a 32-bit unsigned integer n, reverse the bits of the binary representation of n and return the result.
Example 1:
Input: n = 00000000000000000000000000010101
Output: 2818572288 (10101000000000000000000000000000)Explanation: Reversing 00000000000000000000000000010101, which represents the unsigned integer 21, gives us 10101000000000000000000000000000 which represents the unsigned integer 2818572288.
You should aim for a solution with O(1) time and O(1) space.
Given a 32-bit integer, what is the position of bit i after reversing the bits? Maybe you should observe the bit positions before and after reversal to find a pattern.
After reversing the bits, the bit at position i moves to position 31 - i. Can you use this observation to construct the reversed number efficiently?
We initialize res to 0 and iterate through the bits of the given integer n. We extract the bit at the i-th position using ((n >> i) & 1). If it is 1, we set the corresponding bit in res at position (31 - i) using (res |= (1 << (31 - i))).
Before attempting this problem, you should be comfortable with:
We are given a 32-bit unsigned integer, and we need to reverse its bits.
The most straightforward way to think about this problem is:
In simpler terms:
This brute force approach closely follows how humans would solve the problem manually, making it easy to understand, though not the most optimal.
binary to store bits.i from 0 to 31 (since the number is 32-bit):1 or 0res as 0.1, set the corresponding bit in res using bit shifting.We are given a 32-bit unsigned integer and need to reverse all its bits.
Instead of storing bits in a string or array, we can do this directly using bit manipulation:
This approach avoids extra memory and works directly at the bit level, making it both clean and efficient.
res = 0 to store the reversed number.i from 0 to 31:i-th bit of nbit to position (31 - i)resres.We are given a 32-bit unsigned integer and need to reverse its bits.
Instead of reversing bits one-by-one, we can do this much faster by using a classic bit-manipulation trick called bitwise divide and conquer.
The key idea is:
This works because reversing bits is equivalent to:
Each step rearranges bits closer to their final reversed positions.
n stored in res.class Solution:
def reverseBits(self, n: int) -> int:
res = n
res = (res >> 16) | (res << 16) & 0xFFFFFFFF
res = ((res & 0xff00ff00) >> 8) | ((res & 0x00ff00ff) << 8)
res = ((res & 0xf0f0f0f0) >> 4) | ((res & 0x0f0f0f0f) << 4)
res = ((res & 0xcccccccc) >> 2) | ((res & 0x33333333) << 2)
res = ((res & 0xaaaaaaaa) >> 1) | ((res & 0x55555555) << 1)
return res & 0xFFFFFFFFIn languages like Java and JavaScript, using >> (signed right shift) instead of >>> (unsigned right shift) can cause incorrect results. The signed shift preserves the sign bit, which leads to unexpected behavior when the most significant bit is set. Always use unsigned right shift for bit reversal operations.
The problem specifies a 32-bit unsigned integer, so all operations must process exactly 32 bits. A common mistake is iterating fewer than 32 times or not accounting for leading zeros. Every bit position matters in reversal, so the loop must always run for all 32 bits regardless of the input value.