-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathhighest_set_bit.rs
52 lines (45 loc) · 1.23 KB
/
highest_set_bit.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! This module provides a function to find the position of the most significant bit (MSB)
//! set to 1 in a given positive integer.
/// Finds the position of the highest (most significant) set bit in a positive integer.
///
/// # Arguments
///
/// * `num` - An integer value for which the highest set bit will be determined.
///
/// # Returns
///
/// * Returns `Some(position)` if a set bit exists or `None` if no bit is set.
pub fn find_highest_set_bit(num: usize) -> Option<usize> {
if num == 0 {
return None;
}
let mut position = 0;
let mut n = num;
while n > 0 {
n >>= 1;
position += 1;
}
Some(position - 1)
}
#[cfg(test)]
mod tests {
use super::*;
macro_rules! test_find_highest_set_bit {
($($name:ident: $test_case:expr,)*) => {
$(
#[test]
fn $name() {
let (input, expected) = $test_case;
assert_eq!(find_highest_set_bit(input), expected);
}
)*
};
}
test_find_highest_set_bit! {
test_positive_number: (18, Some(4)),
test_0: (0, None),
test_1: (1, Some(0)),
test_2: (2, Some(1)),
test_3: (3, Some(1)),
}
}