LeetCode #190: Reverse Bits
• 99 words • 1 min • updated
python
class Solution:
def reverseBits(self, n: int) -> int:
ans = 0
for i in range(32):
b = n & 1
n >>= 1
ans = (ans << 1) + b
return ansNote:
python
for i in range(32):…is needed to process all 32 bits of n.
Initially I tried it like this:
python
class Solution:
def reverseBits(self, n: int) -> int:
ans = 0
while n:
b = n & 1
n >>= 1
ans = (ans << 1) + b
return ans…but it stops at the most significant bit of n.