mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-05-15 17:58:44 +00:00
[Sync Iteration] rust/eliuds-eggs/2
This commit is contained in:
parent
f80fbca12e
commit
b9143713e4
9
solutions/rust/eliuds-eggs/2/Cargo.toml
Normal file
9
solutions/rust/eliuds-eggs/2/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/2/src/lib.rs
Normal file
16
solutions/rust/eliuds-eggs/2/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