An accepted three line solution in JAVA


  • 1
    J
    public class Solution {
        public int rangeBitwiseAnd(int m, int n) {
            int mask = Integer.MAX_VALUE;
            while ((m & mask) != (n & mask)) mask <<= 1;
            return mask & m;
        }
    }
    

    The only bits that will be 1 will be bits that are common to the upper bits of m and n. Everything else will have at least one instance of a 0 in that range.


  • 0
    K
    This post is deleted!

Log in to reply
 

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