Reverse Bits
Given an unsigned integer, reverse its binary representation (i.e., reading it from right to left).
Function Signature:
def reverse_bits(n: int) -> int:
"""
Reverse the binary representation of the given unsigned integer.
Parameters:
- n: An unsigned integer.
Returns:
- int: The integer after reversing its binary representation.
"""
# Your code here
Example:
n = 43261596
print(f'{n:032b}') # 00000010100101000001111010011100
result = reverse_bits(n)
print(result) # Output should be 964176192
print(f'{result:032b}') # 00111001011110000010100101000000
# Explanation: The binary representation of 43261596 is 00000010100101000001111010011100.
# Reversing the bits, from right to left, gives 00111001011110000010100101000000, which is 964176192 in decimal.
Note:
- The input integer
n
is guaranteed to fit within the range of a 32-bit unsigned integer.
Instructions:
- Implement the
reverse_bits
function to solve the problem. - Use Python for your implementation.
- Provide clear comments in your code to explain the logic.
- Discuss the time and space complexity of your solution.
As always, we’ll share our solution to this problem tomorrow. Stay tuned 😊