1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
use integer::Integer;
use malachite_base::num::logic::traits::CountOnes;
impl Integer {
/// Counts the number of ones in the binary expansion of an [`Integer`]. If the [`Integer`] is
/// negative, then the number of ones is infinite, so `None` is returned.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(1)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
///
/// # Examples
/// ```
/// extern crate malachite_base;
///
/// use malachite_base::num::arithmetic::traits::Pow;
/// use malachite_base::num::basic::traits::Zero;
/// use malachite_nz::integer::Integer;
///
/// assert_eq!(Integer::ZERO.checked_count_ones(), Some(0));
/// // 105 = 1101001b
/// assert_eq!(Integer::from(105).checked_count_ones(), Some(4));
/// assert_eq!(Integer::from(-105).checked_count_ones(), None);
/// // 10^12 = 1110100011010100101001010001000000000000b
/// assert_eq!(Integer::from(10u32).pow(12).checked_count_ones(), Some(13));
/// ```
pub fn checked_count_ones(&self) -> Option<u64> {
if self.sign {
Some(self.abs.count_ones())
} else {
None
}
}
}