#201
Medium Algorithms Bitwise and of numbers range
Bit Manipulation
48.7% acceptance
Jan 12, 2026
4264
318
Given two integers left and right that represent the range [left, right], return the bitwise AND of all numbers in this range, inclusive.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn range_bitwise_and(mut left: i32, mut right: i32) -> i32 {
let mut shift = 0;
while left != right {
left >>= 1;
right >>= 1;
shift += 1;
}
left << shift
}
}