class Solution {
public:
int rangeBitwiseAnd(int m, int n) {
int temp = m ^ n;
int i = 0;
while(temp) {
i++;
temp >>= 1;
}
return ((m>>i)<<i);
}
};
I like this solution very much, too. In "temp=m^n", we can see that the leftmost 1 of temp imples that from this bit to the lowest bit, all the bits will vanish in the Add operation. For example, if temp = 0b 100, this means n = 0b 01**, m = 0b 00**. Obviously, 0b 100 absolutely lies in range [m,n], so in the And operation, the lowest 3 bits will vanish, but other bits will stay unchanged.