Reverse Bits
Reverse all 32 bits of an unsigned integer.
Why does this pattern fit?
Restate the exact job
Reverse all 32 bits of an unsigned integer.
Every step transfers the current low bit into the next result position.
O(32) time · O(1) space
Stopping when n becomes zero forgets leading zeros that become trailing positions.
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.