Skip to content

Latest commit

 

History

History
37 lines (26 loc) · 620 Bytes

201.md

File metadata and controls

37 lines (26 loc) · 620 Bytes

201. Bitwise AND of Numbers Range

Description

link


Solution

See Code

There is a conclution:

When we do & (and) manipulation on a range(m, n) inclusively, we just find the common bits in m and n from left to right and pad zeros for the rest


Code

O(n)

class Solution:
    def rangeBitwiseAnd(self, m, n):
        """
        :type m: int
        :type n: int
        :rtype: int
        """
        i = 0
        while m != n:
            m >>= 1
            n >>= 1
            i += 1
        return n << i