Reverse Bits

Easy
Concept
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Reverse Bits

Approach

Iterate through all 32 bit positions. For each position, extract the lowest bit of n with n & 1, shift the result left by 1 and OR in the extracted bit, then right-shift n. This builds the reversed number one bit at a time.

Complexity Analysis

Time
O(1)
Space
O(1)

Pattern

Bit Shifting

Why It Works

Each iteration moves one bit from the least significant end of the input to the most significant end of the result, effectively mirroring the entire 32-bit representation.

Updated Feb 2026