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.
import math
class Solution(object):
def rangeBitwiseAnd(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
if m == 0:
return 0
# find end
v = int(math.log(m,2))
if 2**(v+1) < n:
n = 2**(v+1)
level = 0
ans = 0
left_log = v
while level <= v:
s = 0
zero = False
if (-m//2**level+ n//2**level+1) >=2: # because there must be one even integer among two more continuous integers
zero = True
else:
t1 = m//2**level
t2 = n//2**level
if t1 % 2 ==0 or t2%2==0:
zero = True
if not zero:
ans += 2**level
level += 1
return ans