Problem overview
The method reverseBits receives a number n written in binary with exactly 32 bits, leading zeros included. It reverses the order of those 32 bits, so the last bit becomes the first, the second to last becomes the second, and so on, and returns the number the reversed bits form.
For n = 1, the 32-bit form is 31 zeros followed by a 1. Reversed, the single 1 moves to the front and the result is 2147483648, which is 2 to the power of 31. For n = 43261596 the 32 bits are 00000010100101000001111010011100, and reversed they are 00111001011110000010100101000000, which is 964176192.
The lesson asks for O(1) time (one step for each of the 32 bits) and O(1) extra space.
Approach: peel bits off one end, push them onto the other
Take the bits of n one at a time from its right end. n & 1 is the last bit, and shifting n one place right drops it, so the next bit becomes the last. As you take bits, build the answer from the right: shift result one place left, which leaves a 0 at its right end, and copy the bit you took into that place with OR.
The first bit taken ends up moved left by every later round, so after 32 rounds it sits at the front. The last bit taken is never moved and stays at the right end. That is exactly the mirror image.
All 32 rounds must run, even after n has become 0. The leading zeros of n are bits too: they become the last bits of the answer, and the rounds that handle them are what push the earlier bits into their correct places.
Step by step
- Create
result, starting at 0. It collects the bits one at a time. - Loop 32 times, with
ioverrange(32). Each round moves one bit. - Shift and copy. Set
result = (result << 1) | (n & 1). The shift makes room,n & 1isolates the last bit ofn, and|puts it into the free place. This line does the reversing: bits taken earlier keep moving left while newer ones come in behind them. - Drop the bit you used, with
n >>= 1. The next bit ofnbecomes the last one. - Return
result.
The solution in Python
class Solution:
def reverseBits(self, n: int) -> int:
result = 0
for i in range(32):
result = (result << 1) | (n & 1)
n >>= 1
return result
Python details worth knowing
- No sign problems. Python integers have no fixed size, and
resultis built from non-negative values, so it simply grows to 32 bits. The top bit of the answer doesn't turn it negative, and values such as2147483648come out as ordinary positive numbers. - The loop variable
iis unused. It only counts the rounds. Many Python programmers write_for such a variable, but the lesson's name is fine. n >>= 1shiftsnright and assigns the result back ton. It rebinds the local name, so the caller's value isn't changed.- Bits above 32 are ignored. Only the lowest 32 bits of
nare looked at, which is why the loop count, not the value ofn, decides when to stop.
Running it
Printing the inputs and results as 32-bit binary strings shows the mirror image clearly:
n = 00000010100101000001111010011100 (43261596)
result = 00111001011110000010100101000000 (964176192)
n = 00000000000000000000000000000001 (1)
result = 10000000000000000000000000000000 (2147483648)
n = 11111111111111111111111111111101 (4294967293)
result = 10111111111111111111111111111111 (3221225471)
The third pair shows the same thing for a number with a single 0 bit: the 0 sits second from the right in n and second from the left in the result.
Complexity
Time: the loop always runs 32 times, whatever n is, so the time is O(1).
Space: two numbers, result and n, are kept, so the extra space is O(1).
Edge cases and common mistakes
- Stopping when
nbecomes 0. Writingwhile n:instead of the 32-round loop breaks every number with leading zeros. Withn = 1the loop would run once and return1, not2147483648. - Zero. All 32 bits are 0, so the result is 0.
- All bits set. The result is
4294967295, the same number. - Shifting in the wrong order. The shift of
resultmust come before the OR. Doing the OR first and shifting afterwards puts every bit one place too far left and leaves the last place empty. - Forgetting the parentheses around
n & 1. In(result << 1) | (n & 1)they keep the intent clear. The operators have a fixed priority (<<, then&, then|), but explicit parentheses spare the reader from remembering it.
Testing
These results come from running the solution:
| n | Result |
|---|---|
| 0 | 0 |
| 1 | 2147483648 |
| 43261596 | 964176192 |
| 4294967293 | 3221225471 |
