We need to reverse the bits of a 32-bit unsigned integer n.
👉 The trick is to process each bit from the least significant to the most significant, and construct the reversed number by shifting res left and adding the current bit of n.
- Iterate 32 times (for each bit in a 32-bit integer).
res <<= 1→ make space for the next bit.res += n & 1→ add the least significant bit ofntores.n >>= 1→ move to the next bit ofn.
Think of it as building the reversed number bit by bit:
- Take the rightmost bit of
nand append it to the left ofres. - Repeat until all bits are processed.
- Time: O(32) → constant, since we always process 32 bits.
- Space: O(1), constant auxiliary space.