Another method for Bitwise AND of Numbers Range [java]


  • 0
    D
    public int rangeBitwiseAnd(int m, int n) {
        int result = m & n;
        if (result == 0) return 0;
    
        for (int i = 0; i < 32; i++) {
            if ((n - m) >= (1 << i)) {
                result &= ~(1<<i);
            } else {
                break;
            }
        }
        
        return result;
    }

Log in to reply
 

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