mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-05-15 17:58:44 +00:00
Merge pull request #2 from Weltenbummler397/exercism-sync/22d900627d8d0b0a
[Sync Iteration] rust/eliuds-eggs/1
This commit is contained in:
commit
3fdc9cfe3b
9
solutions/rust/eliuds-eggs/1/Cargo.toml
Normal file
9
solutions/rust/eliuds-eggs/1/Cargo.toml
Normal 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]
|
||||
16
solutions/rust/eliuds-eggs/1/src/lib.rs
Normal file
16
solutions/rust/eliuds-eggs/1/src/lib.rs
Normal 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
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user