Merge pull request #3 from Weltenbummler397/exercism-sync/3334531c851a77e5

[Sync Iteration] rust/eliuds-eggs/2
This commit is contained in:
Weltenbummler397 2026-05-04 09:53:18 +02:00 committed by GitHub
commit c738617d4d
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
}