leetcode -- Bitwise AND of Numbers Range 无聊数学题

https://leetcode.com/problems/bitwise-and-of-numbers-range/

[m, n]范围的按位与的结果为m与n的公共“左边首部(left header)”

class Solution:
    # @param m, an integer
    # @param n, an integer
    # @return an integer
    def rangeBitwiseAnd(self, m, n):
        p = 0
        while m != n:
            m >>= 1
            n >>= 1
            p += 1
        return m << p

参考http://bookshadow.com/weblog/2015/04/17/leetcode-bitwise-and-numbers-range/

你可能感兴趣的:(LeetCode)