diff options
| author | Brendan Zabarauskas <bjzaba@yahoo.com.au> | 2014-02-17 10:12:10 +1100 |
|---|---|---|
| committer | Brendan Zabarauskas <bjzaba@yahoo.com.au> | 2014-02-17 13:55:06 +1100 |
| commit | 79f52cf9ba20c36860bee8f775498a0880189ac1 (patch) | |
| tree | 34d366700d281c19ec5b2b2231818b4ad720165e /src/libstd/num/mod.rs | |
| parent | 0ba6d4885fc71ca7156e1fe689edb939e1d9d418 (diff) | |
| download | rust-79f52cf9ba20c36860bee8f775498a0880189ac1.tar.gz rust-79f52cf9ba20c36860bee8f775498a0880189ac1.zip | |
Rename Bitwise::population_count to Bitwise::count_ones and add Bitwise::count_zeros
These are inspired by the [functions in the Julia standard library](http://docs.julialang.org/en/release-0.2/stdlib/base/#Base.count_ones).
Diffstat (limited to 'src/libstd/num/mod.rs')
| -rw-r--r-- | src/libstd/num/mod.rs | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/src/libstd/num/mod.rs b/src/libstd/num/mod.rs index 493069139ef..33690a5fddb 100644 --- a/src/libstd/num/mod.rs +++ b/src/libstd/num/mod.rs @@ -375,18 +375,35 @@ pub trait Bitwise: Bounded + BitXor<Self,Self> + Shl<Self,Self> + Shr<Self,Self> { - /// Returns the number of bits set in the number. + /// Returns the number of ones in the binary representation of the number. /// /// # Example /// /// ```rust /// use std::num::Bitwise; /// - /// let n = 0b0101000u16; - /// assert_eq!(n.population_count(), 2); + /// let n = 0b01001100u8; + /// assert_eq!(n.count_ones(), 3); /// ``` - fn population_count(&self) -> Self; - /// Returns the number of leading zeros in the number. + fn count_ones(&self) -> Self; + + /// Returns the number of zeros in the binary representation of the number. + /// + /// # Example + /// + /// ```rust + /// use std::num::Bitwise; + /// + /// let n = 0b01001100u8; + /// assert_eq!(n.count_zeros(), 5); + /// ``` + #[inline] + fn count_zeros(&self) -> Self { + (!*self).count_ones() + } + + /// Returns the number of leading zeros in the in the binary representation + /// of the number. /// /// # Example /// @@ -397,7 +414,9 @@ pub trait Bitwise: Bounded /// assert_eq!(n.leading_zeros(), 10); /// ``` fn leading_zeros(&self) -> Self; - /// Returns the number of trailing zeros in the number. + + /// Returns the number of trailing zeros in the in the binary representation + /// of the number. /// /// # Example /// |
