0 & 1 = 0, 1 & 1 = 1 -> xxxxxxy & 1 = y
Solution 1
class Solution:
# @param n, an integer
# @return an integer
def reverseBits(self, n):
ans = 0
for i in range(32):
ans = (ans << 1) + (n & 1)
n >>= 1
return ans
Time complexity is O(1). Space complexity is O(1).