← DSA Atlas
Dedicated problem page · #190

Reverse Bits

EasyBit ManipulationBit-by-bit reversal accumulationShift-and-append over a fixed 32-bit width
Solve on LeetCode ↗
190
EasyBit ManipulationShift-and-append over a fixed 32-bit widthBit-by-bit reversal accumulation

Reverse Bits

Reverse the bits of a given 32-bit unsigned integer. Read the bits from least significant to most significant and emit them in the opposite order, returning the resulting 32-bit unsigned integer.

Open official problem prompt ↗
In plain English

Produce the integer whose 32-bit binary representation is the exact reverse of the input's.

Picture it like this

Moving books off a shelf one at a time onto a new shelf; the first book you remove ends up at the far end, so the whole row comes out reversed.

Example
Input
n = 43261596 (binary 00000010100101000001111010011100)
Output
964176192 (binary 00111001011110000010100101000000)
Why
The 32-bit pattern read backwards is the reversed bit string, whose value is 964176192.
Constraints
The input is a 32-bit unsigned integerBits are treated over exactly 32 positions
Pattern lesson

See the pattern, then code

Bit-by-bit reversal accumulation
Recognition clue

You must mirror a fixed-width bit pattern end-to-end — a classic shift-and-collect over 32 iterations.

Bit Manipulation

XOR cancellation, powers of two, compact subset state, or per-bit counting.. Pull the input's lowest bit off and push it onto the low end of the result while shifting the result left. After 32 steps the first bit taken lands in the highest position, reversing the order.

New words, made simpleKnow these before the algorithm
Least significant bit (LSB)
The rightmost bit, value 2^0, extracted with n & 1.
Fixed width
The number is treated as exactly 32 bits, so exactly 32 iterations are performed.
Approaches from first idea to best ideaCompare the trade-offs
ApproachHow it thinksCost
String reversal

Works but leans on string conversion and padding; less clean than pure bit ops.

Format n as a 32-char binary string, reverse it, and parse back to int.

Time O(1)Space O(1)
The rule we keep true

Invariant

After k iterations, result holds the first k bits taken from n (its k lowest original bits) in reversed order, occupying result's lowest k positions.

Why this is correct

Reasoning

The i-th bit removed from n is the original bit at position i. Because result is shifted left once per iteration, that bit gets pushed left by 31 - i more times, landing at position 31 - i — precisely its mirrored location.

The algorithm in three movesSay these aloud before coding
1Initialize result = 0

take LSB of n, res = (res<<1)|bit

2Repeat 32 times

n >>= 1 exposes next bit

3Shift result left by one, then OR in the lowest bit of n

after 32 rounds bit0 sits at position 31

4Shift n right by one to expose the next bit

5Return result after 32 iterations

Visual trace

Follow the data, one decision at a time

Input snapshotHighlighted cells are involved in this example
...11000
res<<1|bit1
...2
final3
1 · Readresult=0
2 · Askbegin 32 rounds
3 · Update stateresult=0
4 · Resultready
Key takeaway

Each round peels n's lowest bit and stacks it into result, reversing the 32-bit order.

Code walkthrough

Read the solution in small chunks

Python 3

Do not memorize the whole program. Connect each group of lines to one job in the algorithm.

  1. 1
    Lines 3Accumulator

    result starts empty and will collect bits in reverse.

  2. 2
    Lines 4-632 shift-and-append rounds

    Each round makes room in result, drops in n's current lowest bit, and advances n.

  3. 3
    Lines 7Return

    After exactly 32 rounds every bit has been mirrored.

Make it stick

Edge cases, mistakes, and one self-check

E

Edge cases to test

  • n = 0 returns 0
  • All 32 bits set returns the same all-ones value
  • A single low bit becomes a single high bit (1 -> 2147483648)
!

Common beginner mistakes

  • Looping fewer or more than 32 times and misaligning bits
  • In fixed-width languages, letting the result be interpreted as signed
  • Reversing only the significant bits and dropping the leading zeros of the 32-bit field
Check your understanding

Why exactly 32 iterations rather than looping while n is nonzero?