Simplified solution for Java


  • 0
    2
    public int rangeBitwiseAnd(int m, int n)
    {
    	int location = -1;
    	int c = m & n;
    	for (int i = 30; i >= 0; i--)
    	{
    		if ((m & (1 << i)) != (n & (1 << i)))
    		{
    			location = i;
    			break;
    		}
    
    	}
    	for (int j = location; j >= 0; j--)
    	{
    		c&=~(1<<j);
    	}
    
    	return c;
    }

Log in to reply
 

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