malachite_base/num/arithmetic/
eq_mod_power_of_2.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::num::arithmetic::traits::{DivisibleByPowerOf2, EqModPowerOf2};
10
11macro_rules! impl_eq_mod_power_of_2 {
12    ($t:ident) => {
13        impl EqModPowerOf2<$t> for $t {
14            /// Returns whether one number is equal to another modulo $2^k$.
15            ///
16            /// $f(x, y, k) = (x \equiv y \mod 2^k)$.
17            ///
18            /// $f(x, y, k) = (\exists n \in \Z : x - y = n2^k)$.
19            ///
20            /// # Worst-case complexity
21            /// Constant time and additional memory.
22            ///
23            /// # Examples
24            /// See [here](super::eq_mod_power_of_2#eq_mod_power_of_2).
25            #[inline]
26            fn eq_mod_power_of_2(self, other: $t, pow: u64) -> bool {
27                (self ^ other).divisible_by_power_of_2(pow)
28            }
29        }
30    };
31}
32apply_to_primitive_ints!(impl_eq_mod_power_of_2);