Java solution - Easy to understand


  • 0
    X
    public class Solution {
        public int rangeBitwiseAnd(int m, int n) {
            int ret = 0;
            int index = 1 << 30;
    
            while (index > 0)
            {
                if ((m & index) > 0 && (n & index) > 0)
                {
                    ret = ret | index;
                }
                else if ((m & index) > 0 || (n & index) > 0)
                {
                    return ret;
                }
                
                index = index >> 1;
            }
            
            return ret;
        }
    }

Log in to reply
 

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