LeetCode/rs/bitwise-and-of-numbers-range.rs
Matej Focko 56b6eee734
rs: add «201. Bitwise AND of Numbers Range»
Signed-off-by: Matej Focko <mfocko@redhat.com>
2024-02-21 14:54:47 +01:00

21 lines
445 B
Rust

impl Solution {
fn discarded(mut left: i32, mut right: i32) -> i32 {
let mut counter = 0;
while left != right {
left >>= 1;
right >>= 1;
counter += 1;
}
counter
}
pub fn range_bitwise_and(left: i32, right: i32) -> i32 {
let discarded_bits = Solution::discarded(left, right);
let mask = !((1 << discarded_bits) - 1);
left & mask
}
}