thiagowfx's avatar

Β¬ just serendipity πŸ€ (not just serendipity)

LeetCode #190: Reverse Bits

β€’ 99 words β€’ 1 min β€’ updated

LeetCode #190: Reverse Bits:

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 ans

Note:

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.