201. Bitwise AND of Numbers Range
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
Method: Bit Manipulation (Finding the Common Prefix)
1. Problem Analysis
Solution
Bitwise AND of Numbers Range in Python
class Solution:
def rangeBitwiseAnd(self, left: int, right: int) -> int:
shift = 0
# The goal is to find the common prefix of the binary representations
# of left and right. We can achieve this by repeatedly right-shifting
# both numbers until they become equal.
while left < right:
left >>= 1
right >>= 1
shift += 1
# At this point, `left` (or `right`) is the common prefix.
# We need to shift it back to its original position.
# The bits that were shifted off will become zeros, which is what
# happens when you AND a range of numbers.
return left << shift
Bitwise AND of Numbers Range in Java
class Solution {
public int rangeBitwiseAnd(int left, int right) {
int shift = 0;
// The goal is to find the common prefix of the binary representations
// of left and right. We can achieve this by repeatedly right-shifting
// both numbers until they become equal.
while (left < right) {
left >>= 1;
right >>= 1;
shift++;
}
// At this point, `left` (or `right`) is the common prefix.
// We need to shift it back to its original position.
// The bits that were shifted off will become zeros, which is what
// happens when you AND a range of numbers.
return left << shift;
}
}
Bitwise AND of Numbers Range in C++
class Solution {
public:
int rangeBitwiseAnd(int left, int right) {
int shift = 0;
// The goal is to find the common prefix of the binary representations
// of left and right. We can achieve this by repeatedly right-shifting
// both numbers until they become equal.
while (left < right) {
left >>= 1;
right >>= 1;
shift++;
}
// At this point, `left` (or `right`) is the common prefix.
// We need to shift it back to its original position.
// The bits that were shifted off will become zeros, which is what
// happens when you AND a range of numbers.
return left << shift;
}
};
Bitwise AND of Numbers Range in TypeScript
function rangeBitwiseAnd(left: number, right: number): number {
let shift = 0;
// The goal is to find the common prefix of the binary representations
// of left and right. We can achieve this by repeatedly right-shifting
// both numbers until they become equal.
while (left < right) {
left >>= 1;
right >>= 1;
shift++;
}
// At this point, `left` (or `right`) is the common prefix.
// We need to shift it back to its original position.
// The bits that were shifted off will become zeros, which is what
// happens when you AND a range of numbers.
return left << shift;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.