leetcode/src/problem/p201_bitwise_and_of_numbers_range.rs
2024-10-24 09:08:13 +08:00

32 lines
540 B
Rust

/**
* [201] Bitwise AND of Numbers Range
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn range_bitwise_and(left: i32, right: i32) -> i32 {
let mut right = right;
while left < right {
right = right & (right - 1);
}
right
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_201() {
assert_eq!(4, Solution::range_bitwise_and(5, 7));
assert_eq!(0, Solution::range_bitwise_and(0, 0));
}
}