My clear and short cpp code


  • 1
    C
    class Solution {
    public:
        int rangeBitwiseAnd(int m, int n) 
        {
            // it depends on the highest 0 bit in m
            
            int p = 0;
            while(m != n)
            {
                m >>= 1;
                n >>= 1;
                p++;
            }
            
            return m << p;
        }
    };

Log in to reply
 

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