C# simple.


  • 0
    public int RangeBitwiseAnd(int m, int n) {
        int x = 0;
        int len = n - m + 1;
        int y = 0;
        for (int i = 0; i < 31; i++)
        {
            y = y == 0 ? 1 : y << 1;
            if (y < len || y > n)
            {
                continue;
            }
            int next = y << 1;
            if (m % next >= y && n % next >= y) x |= y; 
        }
        return x;
    }

Log in to reply
 

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