easy +10 pts

Reverse Bits

Reverse the binary representation of an unsigned 32-bit integer.

Given a non-negative integer `n`, write a function `reverse_bits(n)` that returns the integer obtained by reversing the 32-bit binary representation of `n`. The input is always a non-negative integer less than 2^32. The output should also be an integer in the range [0, 2^32). Consider the binary representation padded to 32 bits, then reverse the bit order (the most significant bit becomes the least significant bit, etc.). Note: Python's integers are unbounded, but you must treat `n` as if it is a 32-bit unsigned integer. The function signature is `def reverse_bits(n: int) -> int:`. You may assume the input is valid according to the constraints.

Constraints

0 ≤ n < 2^32. The function should run in O(1) time and O(1) space. No external libraries.

Example

```python
>>> reverse_bits(0)
0
>>> reverse_bits(1)
2147483648
>>> reverse_bits(43261596)
964176192
```
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use a loop to iterate over each of the 32 positions, shifting the result left each time and extracting the rightmost bit of `n`.
Remember to shift `n` right after extracting its least significant bit.
Start with `result = 0` and build it by moving bits from the original number's LSB to the result's MSB.
Think about how many bits you need to process: exactly 32, regardless of the size of `n`.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.