malachite_base/num/arithmetic/
parity.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::Parity;
10
11macro_rules! impl_parity {
12    ($t:ident) => {
13        impl Parity for $t {
14            /// Tests whether a number is even.
15            ///
16            /// $f(x) = (2|x)$.
17            ///
18            /// $f(x) = (\exists k \in \N \ x = 2k)$.
19            ///
20            /// # Worst-case complexity
21            /// Constant time and additional memory.
22            ///
23            /// # Examples
24            /// See [here](super::parity#even).
25            #[inline]
26            fn even(self) -> bool {
27                (self & 1) == 0
28            }
29
30            /// Tests whether a number is odd.
31            ///
32            /// $f(x) = (2\nmid x)$.
33            ///
34            /// $f(x) = (\exists k \in \N \ x = 2k+1)$.
35            ///
36            /// # Worst-case complexity
37            /// Constant time and additional memory.
38            ///
39            /// # Examples
40            /// See [here](super::parity#odd).
41            #[inline]
42            fn odd(self) -> bool {
43                (self & 1) != 0
44            }
45        }
46    };
47}
48apply_to_primitive_ints!(impl_parity);