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]之間的數(shù)AND之后最低位一定是0(奇數(shù)和偶數(shù))灸异。然后m,n右移一位棕所,繼續(xù)計算最低位(倒數(shù)第二位)笛匙。重復上述步驟运嗜,知道范圍為0延赌,或1贰盗。用moveFactor來記錄左移次數(shù)
public class Solution {
public int rangeBitwiseAnd(int m, int n) {
if(m == 0) return 0;
int moveFactor = 1;
while(m!=n){
m >>= 1;
n >>= 1;
moveFactor <<= 1;
}
return m*moveFactor;
}
}