Simple java solution


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

Log in to reply
 

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