Bitwise AND of Numbers Range

07/16/2016 Bit Manipulation

Question

Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.

For example, given the range [5, 7], you should return 4.


Solution

Result: Accepted Time: 44 ms

Here should be some explanations.

int rangeBitwiseAnd(int m, int n) {
    int ret = 0, tmp = 1, seg = n - m,nm = n&m;
    while(m)
    {
       if(seg < tmp)
            ret |= (nm&tmp);
        tmp <<= 1;
        m >>= 1;
    }
    return ret;
}

Complexity Analytics

  • Time Complexity:
  • Space Complexity: