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.
计算m到n之间所有数的按位与的值。本质是计算m与n高位1相同位置时的值,假设m = 1110010,n = 1101010,则值为1100000,程序如下所示:
class Solution {
public int rangeBitwiseAnd(int m, int n) {
int val = 0;
while (m != n){
m >>= 1;
n >>= 1;
val ++;
}
return (m<<val);
}
}