201 - Bitwise AND of Numbers Range
Written on November 4, 2015
Tweet
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.</em></strong></p>
public class Solution {
public int rangeBitwiseAnd(int m, int n) {
int diffDigits = 0;
while (m != n) {
m >>= 1;
n >>= 1;
diffDigits ++;
}
return n << diffDigits;
}//Find the same prefix of the numbers in this range.
}
class Solution(object):
def rangeBitwiseAnd(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
diff = 0
while m != n:
m >>= 1
n >>= 1
diff += 1
return m << diff