SDE & MLE Prep

190. Reverse Bits

Problem

Reverse the bits of a 32-bit unsigned integer.

Example: 43261596 (00000010100101000001111010011100) → 964176192 (00111001011110000010100101000000)

Constraints: follow-up: the function is called many times.

Shift the result left and append the lowest bit of n, 32 times. Java: use >>> (unsigned shift).

Complexity: O(32).

class Solution:
    def reverseBits(self, n: int) -> int:
        res = 0
        for _ in range(32):
            res = (res << 1) | (n & 1)
            n >>= 1
        return res