# My C++ simple solution

• 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);
}
``````

};

• good job! can you explain it in detail to me,okay? than you very much,i will feel great appreciated for you if you can give me some help!

• 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.

• for the m=4,n=6, the temp = m^n=4^6=2, but 2 is not in the range of [4,6] ???

• it is all right cause temp is the XOR of m and n, which stands for the different bits of m and n. whether temp is in the range of [m, n] has nothing to do with the algorithm.

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