
Leetcode Problem 201: Bitwise AND of Numbers Range
Given two integers left and right representing an inclusive range [left, right], return the bitwise AND of all integers in that range.
Constraints:
left and right are non-negative integers up to 2,147,483,647 (2^31 − 1).left <= right.function rangeBitwiseAnd(left: number, right: number): number {
let shift = 0
while (left !== right) {
left = left >> 1
right = right >> 1
shift++
}
return left << shift
};
// Beautiful solution from ChatGPT notes => use xor to find the different bits and then use the mask to get the result (common bits between left and right)
function rangeBitwiseAnd2(left: number, right: number): number {
let diff = left ^ right;
let mask = ~diff + 1;
return left & mask;
};
console.log(rangeBitwiseAnd(5, 7))