malachite_nz/integer/logic/
trailing_zeros.rs

1// Copyright © 2025 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::integer::Integer;
10
11impl Integer {
12    /// Returns the number of trailing zeros in the binary expansion of an [`Integer`]
13    /// (equivalently, the multiplicity of 2 in its prime factorization), or `None` is the
14    /// [`Integer`] is 0.
15    ///
16    /// # Worst-case complexity
17    /// $T(n) = O(n)$
18    ///
19    /// $M(n) = O(1)$
20    ///
21    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
22    ///
23    /// # Examples
24    /// ```
25    /// use malachite_base::num::arithmetic::traits::Pow;
26    /// use malachite_base::num::basic::traits::Zero;
27    /// use malachite_nz::integer::Integer;
28    ///
29    /// assert_eq!(Integer::ZERO.trailing_zeros(), None);
30    /// assert_eq!(Integer::from(3).trailing_zeros(), Some(0));
31    /// assert_eq!(Integer::from(-72).trailing_zeros(), Some(3));
32    /// assert_eq!(Integer::from(100).trailing_zeros(), Some(2));
33    /// assert_eq!((-Integer::from(10u32).pow(12)).trailing_zeros(), Some(12));
34    /// ```
35    pub fn trailing_zeros(&self) -> Option<u64> {
36        self.abs.trailing_zeros()
37    }
38}