Skip to content
← All posts

Reverse Bits: Bit-by-Bit Reversal

4 min read
leetcodeproblemeasybit-manipulation

LeetCode Reverse Bits (Problem 190) asks: given a 32-bit unsigned integer, reverse all of its bits and return the result.

For example, the input 00000010100101000001111010011100 (which is 43261596 in decimal) becomes 00111001011110000010100101000000 (which is 964176192). Every bit slides to its mirror position across the 32-bit boundary.

input (n)00000010100101000001111010011100output (result)00111001011110000010100101000000
Each bit in the input maps to the mirror position in the output. Bit 0 becomes bit 31, bit 1 becomes bit 30, and so on.

The idea is simple. Bit 0 of the input becomes bit 31 of the output. Bit 1 becomes bit 30. And so on for all 32 positions.

The approach

You can reverse the bits by processing one bit at a time from right to left. On each iteration:

  1. Extract the least significant bit of n using n & 1
  2. Shift result one position to the left to make room
  3. OR the extracted bit into result
  4. Shift n one position to the right to expose the next bit

Repeat exactly 32 times. After the loop, result holds the fully reversed 32-bit value.

Why does this work? On each iteration, you peel the rightmost bit off n and push it onto the left side of result. Since result is being built from left to right (via repeated left shifts), the first bit you extract (originally the LSB of the input) ends up in the most significant position. The last bit you extract (originally the MSB) ends up in the least significant position. That is exactly what reversal means.

Python solution

def reverse_bits(n: int) -> int:
    result = 0
    for _ in range(32):
        result = (result << 1) | (n & 1)
        n >>= 1
    return result

Walkthrough: n = 6

Let's trace through a small example. The input is 6, which in 32-bit binary is 00000000000000000000000000000110. The reversed output should be 01100000000000000000000000000000, which is 1610612736.

Iteration 0: Extract LSB of n. n & 1 = 0. Shift result left, OR in the bit. Shift n right.

n00000110LSB = 0result00000000i = 0

n = 6 (binary 110). The LSB is 0, so result stays 0. Then n shifts right to 3 (binary 11).

Iteration 1: Extract LSB of n. n & 1 = 1. Shift result left, OR in the bit. Shift n right.

n00000011LSB = 1result00000001i = 1

n = 3 (binary 11). The LSB is 1. result becomes 1. Then n shifts right to 1.

Iteration 2: Extract LSB of n. n & 1 = 1. Shift result left, OR in the bit. Shift n right.

n00000001LSB = 1result00000011i = 2

n = 1 (binary 1). The LSB is 1. result shifts left to 10, then OR with 1 gives 11. n shifts right to 0.

Remaining iterations: n is 0. Each step shifts result left and ORs in 0.

n00000000LSB = 0result01100000i = 31

After 32 total iterations, result is 01100000...0 (binary). The bits of 6 are now reversed in a 32-bit field.

The first iteration extracts 0 (the LSB of 6 is 0). The second extracts 1, and the third extracts 1. After that, n is 0 and the remaining 29 iterations just shift result left without adding any new bits. The final value has the bits of 6 reversed across all 32 positions.

Complexity analysis

MetricValue
TimeO(1), always exactly 32 iterations
SpaceO(1), only two integer variables

The loop runs a fixed 32 times regardless of the input value. There is no input-dependent branching or early termination. Both time and space are constant.

Building blocks

This problem decomposes into three reusable pieces that CodeBricks drills independently:

Bit extraction

Isolate the least significant bit of an integer using n & 1. This gives you either 0 or 1. It is the same primitive used in counting set bits, checking odd/even, and reading individual bit flags.

bit = n & 1

Bit shifting

Move bits left or right by one position. Left shift (result << 1) makes room for a new bit in the least significant position. Right shift (n >> 1) drops the current LSB and exposes the next bit.

result = result << 1
n = n >> 1

Bit accumulation

Build up a result one bit at a time using OR. After shifting result left, OR-ing in a bit sets that position without disturbing any bits already placed. This pattern appears whenever you need to construct a number bit by bit.

result = (result << 1) | bit

These three operations combine into a single line: result = (result << 1) | (n & 1). Once each building block is automatic, the full solution writes itself.

Edge cases

All zeros (n = 0). Every bit is 0. The loop extracts 0 on every iteration, and the result is 0. Reversing all zeros gives all zeros.

All ones (n = 2^32 - 1, or 4294967295). Every bit is 1. Reversing gives the same value. The result is identical to the input because the bit pattern is a palindrome.

Single bit set (n = 1). The binary is 00000000000000000000000000000001. Reversed, the 1 moves to position 31, giving 10000000000000000000000000000000, which is 2147483648 (2^31).

From understanding to recall

Reverse Bits is easy to understand once you see the loop. But in an interview, you need to write it without hesitation. Which direction do you shift result? Which direction do you shift n? Do you OR or AND? These small details trip people up when the pattern is not in muscle memory.

The fix is to practice the building blocks in isolation. You drill bit extraction, bit shifting, and bit accumulation as separate micro-patterns. Then you combine them. After a few spaced repetitions, the full loop flows out without conscious effort.

Related posts

  • Number of 1 Bits - Another bit manipulation problem on 32-bit integers. Uses the same n & 1 extraction primitive, but counts set bits instead of reversing their positions.
  • Single Number - Uses XOR to cancel duplicate elements. A different bit manipulation technique, but builds the same comfort with bitwise operators that makes Reverse Bits feel natural.