blob: 11a8ee1158119e10f23b599a91ad6fe6db2fc006 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# 201. Bitwise AND of Numbers Range
import math
"""
given two integers 'm' and 'n' that represent the range '[m,
n]', return the bitwise and of all numbers in this range, inclusive.
"""
class Solution(object):
def rangeBitwiseAnd(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
if m == n:
return m
diff = n - m
msb = 1 << math.floor(math.log2(diff))
bit_rem = (msb << 1) - 1
return m & m & ~bit_rem
if __name__ == "__main__":
obj = Solution()
print(obj.rangeBitwiseAnd(5, 7))
print(obj.rangeBitwiseAnd(0, 0))
print(obj.rangeBitwiseAnd(1, 82747883))
|