190. Reverse Bits

Practice Reverse Bits for free on NudgeCode: Easy, Bit Manipulation pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

Reverse the order of all 32 bits of n (bit 0 becomes bit 31 and so on), and return the resulting integer. The current public LC190 contract uses a non-negative, even signed 32-bit input; reversing an even input keeps the result non-negative.

Example

n = 43261596  ->  964176192
n = 2         ->  1073741824

Current public problem constraints

Local practice extension

This practice retains tests from the older unsigned-bit-pattern version, accepting any n from 0 to 2^31 - 1 and interpreting the reversed pattern as unsigned rather than converting its sign bit back. Odd inputs in those extra tests are not requirements of the current public LC190 contract. The same fixed 32-iteration helper handles both versions.