rs: add «201. Bitwise AND of Numbers Range»

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2024-02-21 14:54:47 +01:00
parent 5d27f631e0
commit 56b6eee734
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,21 @@
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
}
}