Math & Bits · 127Easy
Reverse Bits
Reverse the bits of a given 32-bit unsigned integer and return the result.
Constraints
- The input is a 32-bit unsigned integer.
Examples
reverseBits(43261596) -> 964176192 reverseBits(0) -> 0 reverseBits(4294967293) -> 3221225471 reverseBits(1) -> 2147483648 // the lowest bit moves to the top reverseBits(2147483648) -> 1 // and back again reverseBits(4294967295) -> 4294967295 // all ones is its own reverse
Edge cases
- Leading zeros matter — always process exactly 32 bits.
- In JS, bitwise operations are signed.
- FOLLOW-UP
- Follow-up: how would you optimize for many repeated calls? (byte-level lookup table)
Show the target complexity (spoiler)
- Naive
- Converting to a binary string, reversing it, and parsing back works but allocates on
- Target
- O(32) time, O(1) space — shift the result left and push in the input's lowest bit, 32 times.
Solve it
By hand, in your editor. The runner checks answers and timing.
git clone https://github.com/sarmadkung/optimal-round cd optimal-round # open problems/12-math-bits/127-reverse-bits.js ./practice c 127