201. Bitwise AND of Numbers Range
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.
For example, given the range [5, 7], you should return 4.
Solution: If m < n, in the binary representations from m to n, they will share a common prefix(could be empty). In each of the bit in the right of the boarder of common prefix, it will have at least a '1' from [m, n]. So the task is transformed to find number of bits in the right of the boarder.
public int rangeBitwiseAnd(int m, int n) {
int steps = 0;
while (m < n) {
m >>= 1;
n >>= 1;
steps++;
}
return m<<steps;
}