Reverse Bits
Reverse Bits is LeetCode 190 (Easy). You are given an integer n. Write it as 32 bits, reverse their order, and return the number the reversed bits form: bit 0 moves to bit 31, bit 1 to bit 30, and so on.
- All 32 bits count, including the zeros above the highest 1. They become zeros at the bottom of the answer.
nis even and below 2³¹, so its lowest bit is 0 and the answer always fits in a signed 32-bit integer.
The width is fixed at 32, so any correct method runs in constant time.
- 0 <= n <= 2³¹ - 2
- n is even.
Intuition
Move the bits from n to result one at a time, always taking the lowest bit of n and putting it at the lowest place of result, after moving everything already in result one place up.
The first bit taken gets pushed up by every later move, so it ends up highest, and the last bit taken stays lowest. That is the order reversed. After 32 moves, one per bit, bit i of the input sits at position 31 − i.
This is the base-2 version of reversing an integer's digits: read the lowest digit with n & 1 (instead of n % 10), drop it with n >> 1 (instead of n // 10), and push it with result << 1 (instead of result * 10).
Approach
Before reading on, reverse the 8-bit pattern 00000110 by hand, one bit per round. Count the rounds you need, even though the last 1 is read in round 3.
Move one bit per round
Each round does four things in order:
- Read:
n & 1is the lowest bit ofn. - Make room:
result << 1moves every bit already placed one position up. - Place:
| bitputs the new bit in the free lowest position. - Drop:
n >>= 1throws away the bit just read, so the next one becomes lowest.
Python, C++ and Java all write the first three as one line: result = (result << 1) | (n & 1).
Run exactly 32 rounds
Do not stop when n becomes 0. A bit placed in round i is pushed up once in each later round, so it reaches its reversed position only if all 32 rounds run.
Code in Python, C++ and Java
n and push them onto the low end of result. Every later push moves the earlier bits one place up, so the first bit read ends up highest: the order comes out reversed.n & 1 is 0. Shift result left to free its lowest place, put the bit there, then shift n right so the next bit becomes its lowest.n & 1 is 1. Shift result left to free its lowest place, put the bit there, then shift n right so the next bit becomes its lowest.n & 1 is 0. Shift result left to free its lowest place, put the bit there, then shift n right so the next bit becomes its lowest.n & 1 is 1. Shift result left to free its lowest place, put the bit there, then shift n right so the next bit becomes its lowest.n & 1 is 1, and after the shift n is 0. A while n loop would stop here and return 11, which is wrong: the bits are still at the bottom of result and need 27 more shifts to reach their reversed places.n is 0, but the loop still runs the remaining 27 rounds. Each pushes a 0 and moves the bits already in result one place up, so the 5 bits read earlier climb to the top of the 32.n and push them onto the low end of result. Every later push moves the earlier bits one place up, so the first bit read ends up highest: the order comes out reversed.n are 1100; read from the right they come out as 0011, and that is the order they land in result. Each round still moves one bit, exactly as in the code.n are 1001; read from the right they come out as 1001, and that is the order they land in result.n are 1110; read from the right they come out as 0111, and that is the order they land in result.n are 0001; read from the right they come out as 1000, and that is the order they land in result.n are 0100; read from the right they come out as 0010, and that is the order they land in result.n are 1001; read from the right they come out as 1001, and that is the order they land in result.n are 0010; read from the right they come out as 0100, and that is the order they land in result.n are 0000; read from the right they come out as 0000, and that is the order they land in result.Common pitfalls
Stopping when n becomes 0
while n:
result = (result << 1) | (n & 1)
n >>= 1for _ in range(32):
result = (result << 1) | (n & 1)
n >>= 1The zeros above the highest 1 still have to be pushed in: each one moves the bits already placed one step closer to their reversed position. Without them the right bits land in the wrong places: n = 2 returns 1 instead of 2³⁰.
Complexity
Reverse Bits FAQ
Is there a Python one-liner?
Yes: format n as 32 binary digits, reverse the string, and read it back: int(format(n, "032b")[::-1], 2). The 032b pads with leading zeros, which is what keeps all 32 bits; bin(n)[2:][::-1] drops them and gives the wrong answer.
How can it be optimized if the function is called many times?
This is the problem's follow-up. Two standard answers:
- Lookup table: precompute the reversal of all 256 bytes once, then reverse a number with four table lookups and shifts.
- Mask swaps: swap the two 16-bit halves, then neighbouring bytes, 4-bit groups, 2-bit pairs and single bits. Each swap is one line such as
n = ((n & 0xFF00FF00) >> 8) | ((n & 0x00FF00FF) << 8), so the whole reversal is five lines with no loop.