201. Bitwise AND of Numbers Range - cocoder39/coco39_LC GitHub Wiki

201. Bitwise AND of Numbers Range

[26, 30]

11 010, 11 011, 11 100, 11 101, 11 110

same part is 11 000 = 24

time complexity is O(1) because at most 32 bits

int rangeBitwiseAnd(int m, int n) {
        int shift = 0;
        while (m != n) {
            m >>= 1;
            n >>= 1;
            shift++;
        }
        
        return m << shift;
    }