Skip to content

Commit a92ea47

Browse files
committed
Update highest set bit
1 parent a8491ae commit a92ea47

File tree

1 file changed

+2
-9
lines changed

1 file changed

+2
-9
lines changed

src/bit_manipulation/highest_set_bit.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,8 @@ pub fn find_highest_set_bit(num: usize) -> Option<usize> {
1515
return None;
1616
}
1717

18-
let mut position = 0;
19-
let mut n = num;
20-
21-
while n > 0 {
22-
n >>= 1;
23-
position += 1;
24-
}
25-
26-
Some(position - 1)
18+
let zc = num.leading_zeros();
19+
Some((usize::BITS - zc - 1) as usize)
2720
}
2821

2922
#[cfg(test)]

0 commit comments

Comments
 (0)