LeetCode 190 Reverse Bits - Solution

The core idea

Process one bit at a time. Extract the least significant bit from input, append it to the result.

For 3232 iterations: 1.1. Shift result left by 1 to make room. 2.2. Add the last bit of n to result (result |= n & 1). 3.3. Shift n right by 1 to move to the next bit.

This effectively reads bits right-to-left and writes them left-to-right.