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.