Reverse bits of a given 32 bits unsigned integer.
given input 43261596 (represented in binary as 00000010100101000001111010011100),
return 964176192 (represented in binary as 00111001011110000010100101000000).
If this function is called many times, how would you optimize it?
Related problem: Reverse Integer
参考题目: Q7 Reverse Integer
此题是将整数对应的32位二进制数反转,然后再将反转后的二进制数转化为整数。
class Solution:
# @param n, an integer
# @return an integer
def reverseBits(self, n):
return int(bin(n)[:1:-1].ljust(32, '0'), base=2)
# 或者 return eval('0b' + bin(n)[:1:-1].ljust(32, '0'))
a = 43261596
b = Solution()
print(b.reverseBits(a)) # 964176192