Easy · Bit Shifts
Understand → Visualise → Practice.
Treat n as an unsigned 32-bit value and return the integer whose bits appear in reverse order.
Read the lowest bit of n and append it to the result, 32 times. The bit read first ends up most significant.
Set out = 0.
32 times: out = (out << 1) | (n & 1), then n >>= 1.
After 32 steps out holds the bits in reverse order.
O(32) = O(1) time and O(1) space.
In JavaScript, << produces signed 32-bit numbers; use >>> 0 or arithmetic to keep the result unsigned.
Read the full guide first: Bit Manipulation and Bitmask Patterns.
Guest mode lets you understand, visualise and run the sample tests. Create a free account to submit and save your solutions.