malachite_nz/integer/logic/significant_bits.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;
10use malachite_base::num::logic::traits::SignificantBits;
11
12impl SignificantBits for &Integer {
13 /// Returns the number of significant bits of an [`Integer`]'s absolute value.
14 ///
15 /// $$
16 /// f(n) = \\begin{cases}
17 /// 0 & \text{if} \\quad n = 0, \\\\
18 /// \lfloor \log_2 |n| \rfloor + 1 & \\text{otherwise}.
19 /// \\end{cases}
20 /// $$
21 ///
22 /// # Worst-case complexity
23 /// Constant time and additional memory.
24 ///
25 /// # Examples
26 /// ```
27 /// use malachite_base::num::basic::traits::Zero;
28 /// use malachite_base::num::logic::traits::SignificantBits;
29 /// use malachite_nz::integer::Integer;
30 ///
31 /// assert_eq!(Integer::ZERO.significant_bits(), 0);
32 /// assert_eq!(Integer::from(100).significant_bits(), 7);
33 /// assert_eq!(Integer::from(-100).significant_bits(), 7);
34 /// ```
35 fn significant_bits(self) -> u64 {
36 self.abs.significant_bits()
37 }
38}