mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-05-15 17:58:44 +00:00
17 lines
484 B
Rust
17 lines
484 B
Rust
pub fn egg_count(mut display_value: u32) -> usize {
|
|
let mut count = 0;
|
|
|
|
// Loop continues until all set bits have been cleared
|
|
while display_value > 0 {
|
|
// Increment the count for the bit we are about to clear
|
|
count += 1;
|
|
|
|
// This is Brian Kernighan's trick:
|
|
// display_value & (display_value - 1) clears the
|
|
// rightmost (least significant) '1' bit in display_value.
|
|
display_value &= display_value - 1;
|
|
}
|
|
|
|
count
|
|
}
|