malachite_nz/integer/arithmetic/balanced_mod_power_of_2.rs
1// Copyright © 2026 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 crate::natural::arithmetic::balanced_mod_power_of_2::balanced_from_mod_power_of_2;
11use core::mem::take;
12use malachite_base::num::arithmetic::traits::{
13 BalancedModPowerOf2, BalancedModPowerOf2Assign, ModPowerOf2,
14};
15
16impl BalancedModPowerOf2 for Integer {
17 type Output = Self;
18
19 /// Divides an [`Integer`] by $2^k$, returning the balanced remainder: the representative of
20 /// `self` modulo $2^k$ that is closest to zero. The [`Integer`] is taken by value.
21 ///
22 /// The remainder $r$ satisfies $-2^{k-1} < r \leq 2^{k-1}$ and $r \equiv x \bmod 2^k$, which
23 /// determine it uniquely; for $k = 0$ it is 0. A remainder of exactly $2^{k-1}$ is positive.
24 /// This is [`balanced_mod`](malachite_base::num::arithmetic::traits::BalancedMod::balanced_mod)
25 /// with modulus $2^k$.
26 ///
27 /// # Worst-case complexity
28 /// $T(n) = O(n)$
29 ///
30 /// $M(n) = O(n)$
31 ///
32 /// where $T$ is time, $M$ is additional memory, and $n$ is `pow`.
33 ///
34 /// # Examples
35 /// ```
36 /// use malachite_base::num::arithmetic::traits::BalancedModPowerOf2;
37 /// use malachite_nz::integer::Integer;
38 ///
39 /// assert_eq!(Integer::from(23).balanced_mod_power_of_2(3), -1);
40 /// // a negative value is reduced into the same range
41 /// assert_eq!(Integer::from(-19).balanced_mod_power_of_2(3), -3);
42 /// // exactly half the modulus stays positive
43 /// assert_eq!(Integer::from(-20).balanced_mod_power_of_2(3), 4);
44 /// ```
45 #[inline]
46 fn balanced_mod_power_of_2(self, pow: u64) -> Self {
47 balanced_from_mod_power_of_2(self.mod_power_of_2(pow), pow)
48 }
49}
50
51impl BalancedModPowerOf2 for &Integer {
52 type Output = Integer;
53
54 /// Divides an [`Integer`] by $2^k$, returning the balanced remainder: the representative of
55 /// `self` modulo $2^k$ that is closest to zero. The [`Integer`] is taken by reference.
56 ///
57 /// See the [`BalancedModPowerOf2`] documentation for details.
58 ///
59 /// # Worst-case complexity
60 /// $T(n) = O(n)$
61 ///
62 /// $M(n) = O(n)$
63 ///
64 /// where $T$ is time, $M$ is additional memory, and $n$ is `pow`.
65 ///
66 /// # Examples
67 /// ```
68 /// use malachite_base::num::arithmetic::traits::BalancedModPowerOf2;
69 /// use malachite_nz::integer::Integer;
70 ///
71 /// assert_eq!((&Integer::from(23)).balanced_mod_power_of_2(3), -1);
72 /// assert_eq!((&Integer::from(-19)).balanced_mod_power_of_2(3), -3);
73 /// assert_eq!((&Integer::from(-20)).balanced_mod_power_of_2(3), 4);
74 /// ```
75 #[inline]
76 fn balanced_mod_power_of_2(self, pow: u64) -> Integer {
77 balanced_from_mod_power_of_2(self.mod_power_of_2(pow), pow)
78 }
79}
80
81impl BalancedModPowerOf2Assign for Integer {
82 /// Divides an [`Integer`] by $2^k$, replacing it by the balanced remainder: the representative
83 /// of `self` modulo $2^k$ that is closest to zero.
84 ///
85 /// See the [`BalancedModPowerOf2`] documentation for details.
86 ///
87 /// # Worst-case complexity
88 /// $T(n) = O(n)$
89 ///
90 /// $M(n) = O(n)$
91 ///
92 /// where $T$ is time, $M$ is additional memory, and $n$ is `pow`.
93 ///
94 /// # Examples
95 /// ```
96 /// use malachite_base::num::arithmetic::traits::BalancedModPowerOf2Assign;
97 /// use malachite_nz::integer::Integer;
98 ///
99 /// let mut x = Integer::from(23);
100 /// x.balanced_mod_power_of_2_assign(3);
101 /// assert_eq!(x, -1);
102 /// ```
103 #[inline]
104 fn balanced_mod_power_of_2_assign(&mut self, pow: u64) {
105 *self = take(self).balanced_mod_power_of_2(pow);
106 }
107}