Java 8ms


  • 0
    W

    public int rangeBitwiseAnd(int m, int n) {
    if(m == n) return m;
    int mask = m & n;
    if (mask == 0) return 0;
    int res = 0;

        for(int i = 0; (mask >> i) > 0; i++) {
        	int curBit = 1 << i;
        	if((curBit & mask) != 0 && curBit > n - m) {
        		res |= curBit;
        	}
        }
        
        return res;
    }

Log in to reply
 

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