All Blind 75 questions

Reverse Bits

FreeBit manipulationEasy73 of 75

The problem

Reverse the order of all 32 bits in an unsigned 32-bit integer and return the resulting unsigned value. Leading zeros participate.

Example

1 → 2147483648 because the lowest bit moves to the highest position.

Need a hint?

Build the answer from the input’s least significant bit.

Write pseudocode, trace the example, or note an edge case. This scratchpad does not run code.

Notes stay in this browser when storage is available.

Read the solution approach

Repeat exactly 32 times: shift the result left, append the input’s lowest bit, then shift the input right using unsigned semantics. Mask to 32 bits if the language uses unbounded integers. Convert a signed bitwise result to unsigned before returning.

Complexity

O(32), hence O(1), time and O(1) space.

Before moving on, explain why the algorithm is correct and trace a boundary case without looking at the approach.