aboutsummaryrefslogtreecommitdiff
path: root/bitwise_and_num_range.py
diff options
context:
space:
mode:
Diffstat (limited to 'bitwise_and_num_range.py')
-rw-r--r--bitwise_and_num_range.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/bitwise_and_num_range.py b/bitwise_and_num_range.py
new file mode 100644
index 0000000..11a8ee1
--- /dev/null
+++ b/bitwise_and_num_range.py
@@ -0,0 +1,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))