[Sync Iteration] rust/eliuds-eggs/2

This commit is contained in:
exercism-solutions-syncer[bot] 2025-12-03 09:14:45 +00:00 committed by GitHub
parent f80fbca12e
commit b9143713e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 0 deletions

View File

@ -0,0 +1,9 @@
[package]
name = "eliuds_eggs"
version = "0.1.0"
edition = "2024"
# Not all libraries from crates.io are available in Exercism's test runner.
# The full list of available libraries is here:
# https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml
[dependencies]

View File

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