PROBLEM 73 OF 75

Reverse Bits

Reverse all 32 bits of an unsigned integer.

PATTERNBit manipulationWrite the bit truth table before the code.
SPOT THE SIGNAL

Why does this pattern fit?

FRAME · 1 OF 4

Restate the exact job

Reverse all 32 bits of an unsigned integer.

WHY THIS FITS

Every step transfers the current low bit into the next result position.

COMPLEXITY

O(32) time · O(1) space

COMMON FAILURE

Stopping when n becomes zero forgets leading zeros that become trailing positions.

THE REASONING, IN ONE PLACE

How to solve Reverse Bits

The goal is to solve this problem from the pattern, not to memorize a finished answer. Use this as a check after your own attempt.

What the question asks

Reverse all 32 bits of an unsigned integer.

Why Bit manipulation fits

Every step transfers the current low bit into the next result position.

State to maintain

Input value and a 32-bit result.

Transition

Repeat 32 times: shift result, add n&1, then unsigned-shift n.

Time and space

O(32) time · O(1) space

Counterexample to the tempting mistake

Stopping when n becomes zero forgets leading zeros that become trailing positions.

Prove it again tomorrow

Close this page. Rebuild the state and transition from memory, write a test that exposes the mistake above, then solve a fresh input without looking back. A same-day reread is practice, not proof of retention.

Open blank recall ↗