My simple c++ code.


  • 3
    Z
    int rangeBitwiseAnd(int m, int n) 
    {
        for(int k=0; m>0&&n>0; k++)
        {
            if(m==n)
            {
                while(k>0)
                {
                    m=m<<1;
                    k--;
                }
                return m;
            }
            m=m>>1;
            n=n>>1;
        }
        return 0;
    }

Log in to reply
 

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