Easy Bit Manipulation No Iterations from m to n


  • 0
    W
    class Solution(object):
    def rangeBitwiseAnd(self, m, n):
        count = 0
        while (m != n):
            m >>= 1; n >>= 1; count += 1
        return n << count
    

    We only need to find the common prefix of m and n. Keep a count of how many bits we shift and shift the prefix leftwards that many times.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.