201. Bitwise AND of Numbers Range

# Medium

Key idea: find the common bits from left to right, it's the result

So compare m and n from removing most right bit until they are equal.

class Solution:
    def rangeBitwiseAnd(self, m: int, n: int) -> int:
        for i in range(32):
            if m >> i == n >> i:
                res = m >> i
                return res << i

Last updated