Bitwise AND of Numbers Range Given two integers left
and right
that represent the range [left, right]
, return the bitwise AND of all numbers in this range, inclusive.
Example 1:
Input: left = 5, right = 7 Output: 4
Example 2:
Input: left = 0, right = 0 Output: 0
Example 3:
Input: left = 1, right = 2147483647 Output: 0
Constraints:
0 <= left <= right <= 231 - 1
Bitwise AND of Numbers Range Solutions
✅Time:O(32)=O(1)
✅Space: O(1)
C++
class Solution {
public:
int rangeBitwiseAnd(int m, int n) {
int shiftBits = 0;
while (m != n) {
m >>= 1;
n >>= 1;
++shiftBits;
}
return m << shiftBits;
}
};
Java
class Solution {
public int rangeBitwiseAnd(int m, int n) {
int shiftBits = 0;
while (m != n) {
m >>= 1;
n >>= 1;
++shiftBits;
}
return m << shiftBits;
}
}
Python
class Solution:
def rangeBitwiseAnd(self, m: int, n: int) -> int:
return self.rangeBitwiseAnd(m >> 1, n >> 1) << 1 if m < n else m
Watch Tutorial
Checkout more Solutions here