C 32ms solution using mask


  • 0
    R
    int rangeBitwiseAnd(int m, int n) 
    {
        int msb=0;
        
        int t = m ^ n;
        while (t >>= 1)
            msb++;
        unsigned int mask = ~((1 << msb) -1);
        
        
        return m & mask;
    }

  • 0
    M

    the code I wrote here is not correct, so I removed it in case someone might be misled...


Log in to reply
 

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