# Middle-题目59：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.

public class Solution {
private static int[] pows = new int[]{1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824};
public static int rangeBitwiseAnd(int m, int n) {
if(m>pows[30])
return pows[30] + rangeBitwiseAnd(m-pows[30],n-pows[30]);
for(int i = 1;i < 31; i++) {
if(m<pows[i] && n>pows[i])
return 0;
else if(m>=pows[i] && n<pows[i+1])
return pows[i] + rangeBitwiseAnd(m-pows[i],n-pows[i]);
}
return m&n;
}
}

public class Solution {
public int rangeBitwiseAnd(int m, int n) {
int bit = 0;
while(m!=n) {
m>>=1;
n>>=1;
bit++;
}
return m<<bit;
}
}  

Cmershen的碎碎念：

05-22 4429

02-05 583

07-18 545

04-23 170

05-08 2982

03-21 127

02-26 139

07-04 3

11-12 245

06-11 68