Skip to main content

malachite_base/num/basic/
integers.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::comparison::traits::{Max, Min};
10use crate::named::Named;
11use crate::num::arithmetic::traits::{
12    AbsDiff, AddMul, AddMulAssign, ArithmeticCheckedShl, ArithmeticCheckedShr, BinomialCoefficient,
13    CeilingRoot, CeilingRootAssign, CeilingSqrt, CeilingSqrtAssign, CheckedAdd, CheckedAddMul,
14    CheckedBinomialCoefficient, CheckedDiv, CheckedMul, CheckedNeg, CheckedPow, CheckedRoot,
15    CheckedSqrt, CheckedSquare, CheckedSub, CheckedSubMul, DivAssignEuclidean, DivAssignMod,
16    DivAssignRem, DivEuclidean, DivExact, DivExactAssign, DivMod, DivRem, DivRound, DivRoundAssign,
17    DivisibleBy, DivisibleByPowerOf2, EqMod, EqModPowerOf2, ExtendedGcd, FloorRoot,
18    FloorRootAssign, FloorSqrt, FloorSqrtAssign, JacobiSymbol, KroneckerSymbol, LegendreSymbol,
19    Mod, ModAssign, ModPowerOf2, ModPowerOf2Assign, OverflowingAdd, OverflowingAddAssign,
20    OverflowingAddMul, OverflowingAddMulAssign, OverflowingDiv, OverflowingDivAssign,
21    OverflowingMul, OverflowingMulAssign, OverflowingNeg, OverflowingNegAssign, OverflowingPow,
22    OverflowingPowAssign, OverflowingSquare, OverflowingSquareAssign, OverflowingSub,
23    OverflowingSubAssign, OverflowingSubMul, OverflowingSubMulAssign, Parity, Pow, PowAssign,
24    PowerOf2, RemPowerOf2, RemPowerOf2Assign, RotateLeft, RotateLeftAssign, RotateRight,
25    RotateRightAssign, RoundToMultiple, RoundToMultipleAssign, RoundToMultipleOfPowerOf2,
26    RoundToMultipleOfPowerOf2Assign, SaturatingAdd, SaturatingAddAssign, SaturatingAddMul,
27    SaturatingAddMulAssign, SaturatingMul, SaturatingMulAssign, SaturatingPow, SaturatingPowAssign,
28    SaturatingSquare, SaturatingSquareAssign, SaturatingSub, SaturatingSubAssign, SaturatingSubMul,
29    SaturatingSubMulAssign, ShlRound, ShlRoundAssign, ShrRound, ShrRoundAssign, Sign, Square,
30    SquareAssign, SubMul, SubMulAssign, WrappingAdd, WrappingAddAssign, WrappingAddMul,
31    WrappingAddMulAssign, WrappingDiv, WrappingDivAssign, WrappingMul, WrappingMulAssign,
32    WrappingNeg, WrappingNegAssign, WrappingPow, WrappingPowAssign, WrappingSquare,
33    WrappingSquareAssign, WrappingSub, WrappingSubAssign, WrappingSubMul, WrappingSubMulAssign,
34};
35use crate::num::basic::traits::{One, Two, Zero};
36use crate::num::conversion::traits::{
37    ConvertibleFrom, ExactFrom, ExactInto, FromSciString, FromStringBase, IsInteger,
38    OverflowingFrom, OverflowingInto, RoundingFrom, RoundingInto, SaturatingFrom, SaturatingInto,
39    ToSci, ToStringBase, WrappingFrom, WrappingInto,
40};
41use crate::num::factorization::traits::{ExpressAsPower, IsPower, IsSquare};
42use crate::num::float::NiceFloat;
43use crate::num::logic::traits::{
44    BitAccess, BitBlockAccess, BitConvertible, BitIterable, BitScan, CountOnes, CountZeros,
45    LeadingZeros, LowMask, NotAssign, SignificantBits, TrailingZeros,
46};
47#[cfg(feature = "random")]
48use crate::num::random::HasRandomPrimitiveInts;
49use core::fmt::{Binary, Debug, Display, LowerHex, Octal, UpperHex};
50use core::hash::Hash;
51use core::iter::{Product, Sum};
52use core::ops::{
53    Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign,
54    Mul, MulAssign, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
55};
56use core::panic::{RefUnwindSafe, UnwindSafe};
57use core::str::FromStr;
58
59pub const USIZE_IS_U32: bool = usize::WIDTH == u32::WIDTH;
60pub const USIZE_IS_U64: bool = usize::WIDTH == u64::WIDTH;
61
62// Checks that usize is equivalent to u32 or u64, at compile time. The rest of Malachite can assume
63// that this condition is true.
64const _USIZE_ASSERTION: () = assert!(USIZE_IS_U32 || USIZE_IS_U64);
65
66// When the `random` feature is enabled, the HasRandomPrimitiveInts bound is included.
67
68#[cfg(feature = "random")]
69/// Defines functions on primitive integer types: uxx, ixx, usize, and isize.
70///
71/// The different types are distinguished by whether they are signed or unsigned, and by their
72/// widths. The width $W$ is the number of bits in the type. For example, the width of [`u32`] or
73/// [`i32`] is 32. Each type has $2^W$ distinct values.
74///
75/// Let $n$ be a value of type `Self`. If `Self` is unsigned, $0 \leq n < 2^W$. If `Self` is signed,
76/// $2^{W-1} \leq n < 2^{W-1}$.
77pub trait PrimitiveInt:
78    'static
79    + AbsDiff<Self>
80    + Add<Self, Output = Self>
81    + AddAssign<Self>
82    + AddMul<Self, Self, Output = Self>
83    + AddMulAssign<Self, Self>
84    + ArithmeticCheckedShl<i128, Output = Self>
85    + ArithmeticCheckedShl<i16, Output = Self>
86    + ArithmeticCheckedShl<i32, Output = Self>
87    + ArithmeticCheckedShl<i64, Output = Self>
88    + ArithmeticCheckedShl<i8, Output = Self>
89    + ArithmeticCheckedShl<isize, Output = Self>
90    + ArithmeticCheckedShl<u128, Output = Self>
91    + ArithmeticCheckedShl<u16, Output = Self>
92    + ArithmeticCheckedShl<u32, Output = Self>
93    + ArithmeticCheckedShl<u64, Output = Self>
94    + ArithmeticCheckedShl<u8, Output = Self>
95    + ArithmeticCheckedShl<usize, Output = Self>
96    + ArithmeticCheckedShr<i128, Output = Self>
97    + ArithmeticCheckedShr<i16, Output = Self>
98    + ArithmeticCheckedShr<i32, Output = Self>
99    + ArithmeticCheckedShr<i64, Output = Self>
100    + ArithmeticCheckedShr<i8, Output = Self>
101    + ArithmeticCheckedShr<isize, Output = Self>
102    + Binary
103    + BinomialCoefficient<Self>
104    + BitAccess
105    + BitAnd<Self, Output = Self>
106    + BitAndAssign<Self>
107    + BitBlockAccess
108    + BitConvertible
109    + BitIterable
110    + BitOr<Self, Output = Self>
111    + BitOrAssign<Self>
112    + BitScan
113    + BitXor<Self, Output = Self>
114    + BitXorAssign<Self>
115    + CeilingRoot<u64, Output = Self>
116    + CeilingRootAssign<u64>
117    + CeilingSqrt<Output = Self>
118    + CeilingSqrtAssign
119    + CheckedAdd<Self, Output = Self>
120    + CheckedAddMul<Self, Self, Output = Self>
121    + CheckedBinomialCoefficient<Self>
122    + CheckedDiv<Self, Output = Self>
123    + CheckedMul<Self, Output = Self>
124    + CheckedNeg<Output = Self>
125    + CheckedPow<u64, Output = Self>
126    + CheckedRoot<u64, Output = Self>
127    + CheckedSqrt<Output = Self>
128    + CheckedSquare<Output = Self>
129    + CheckedSub<Self, Output = Self>
130    + CheckedSubMul<Self, Self, Output = Self>
131    + Clone
132    + ConvertibleFrom<f32>
133    + ConvertibleFrom<f64>
134    + ConvertibleFrom<i128>
135    + ConvertibleFrom<i16>
136    + ConvertibleFrom<i32>
137    + ConvertibleFrom<i64>
138    + ConvertibleFrom<i8>
139    + ConvertibleFrom<isize>
140    + ConvertibleFrom<u128>
141    + ConvertibleFrom<u16>
142    + ConvertibleFrom<u32>
143    + ConvertibleFrom<u64>
144    + ConvertibleFrom<u8>
145    + ConvertibleFrom<usize>
146    + Copy
147    + CountOnes
148    + CountZeros
149    + Debug
150    + Default
151    + Display
152    + Div<Self, Output = Self>
153    + DivAssign<Self>
154    + DivAssignEuclidean<Self, ModOutput = Self>
155    + DivAssignMod<Self, ModOutput = Self>
156    + DivAssignRem<Self, RemOutput = Self>
157    + DivEuclidean<Self, DivOutput = Self, ModOutput = Self>
158    + DivExact<Self, Output = Self>
159    + DivExactAssign<Self>
160    + DivMod<Self, DivOutput = Self, ModOutput = Self>
161    + DivRem<Self, DivOutput = Self, RemOutput = Self>
162    + DivRound<Self, Output = Self>
163    + DivRoundAssign<Self>
164    + DivisibleBy<Self>
165    + DivisibleByPowerOf2
166    + Eq
167    + EqMod<Self, Self>
168    + EqModPowerOf2<Self>
169    + ExactFrom<i128>
170    + ExactFrom<i16>
171    + ExactFrom<i32>
172    + ExactFrom<i64>
173    + ExactFrom<i8>
174    + ExactFrom<isize>
175    + ExactFrom<u128>
176    + ExactFrom<u16>
177    + ExactFrom<u32>
178    + ExactFrom<u64>
179    + ExactFrom<u8>
180    + ExactFrom<usize>
181    + ExactInto<i128>
182    + ExactInto<i16>
183    + ExactInto<i32>
184    + ExactInto<i64>
185    + ExactInto<i8>
186    + ExactInto<isize>
187    + ExactInto<u128>
188    + ExactInto<u16>
189    + ExactInto<u32>
190    + ExactInto<u64>
191    + ExactInto<u8>
192    + ExactInto<usize>
193    + ExpressAsPower
194    + ExtendedGcd<Self>
195    + FloorRoot<u64, Output = Self>
196    + FloorRootAssign<u64>
197    + FloorSqrt<Output = Self>
198    + FloorSqrtAssign
199    + From<bool>
200    + FromSciString
201    + FromStr
202    + FromStringBase
203    + HasRandomPrimitiveInts
204    + Hash
205    + IsInteger
206    + IsPower
207    + IsSquare
208    + JacobiSymbol<Self>
209    + KroneckerSymbol<Self>
210    + LeadingZeros
211    + LegendreSymbol<Self>
212    + LowMask
213    + LowerHex
214    + Max
215    + Min
216    + Mod<Self, Output = Self>
217    + ModAssign<Self>
218    + ModPowerOf2
219    + ModPowerOf2Assign
220    + Mul<Self, Output = Self>
221    + MulAssign<Self>
222    + Named
223    + Not<Output = Self>
224    + NotAssign
225    + Octal
226    + One
227    + Ord
228    + OverflowingAdd<Self, Output = Self>
229    + OverflowingAddAssign<Self>
230    + OverflowingAddMul<Self, Self, Output = Self>
231    + OverflowingAddMulAssign<Self, Self>
232    + OverflowingDiv<Self, Output = Self>
233    + OverflowingDivAssign<Self>
234    + OverflowingFrom<i128>
235    + OverflowingFrom<i16>
236    + OverflowingFrom<i32>
237    + OverflowingFrom<i64>
238    + OverflowingFrom<i8>
239    + OverflowingFrom<isize>
240    + OverflowingFrom<u128>
241    + OverflowingFrom<u16>
242    + OverflowingFrom<u32>
243    + OverflowingFrom<u64>
244    + OverflowingFrom<u8>
245    + OverflowingFrom<usize>
246    + OverflowingInto<i128>
247    + OverflowingInto<i16>
248    + OverflowingInto<i32>
249    + OverflowingInto<i64>
250    + OverflowingInto<i8>
251    + OverflowingInto<isize>
252    + OverflowingInto<u128>
253    + OverflowingInto<u16>
254    + OverflowingInto<u32>
255    + OverflowingInto<u64>
256    + OverflowingInto<u8>
257    + OverflowingInto<usize>
258    + OverflowingMul<Self, Output = Self>
259    + OverflowingMulAssign<Self>
260    + OverflowingNeg<Output = Self>
261    + OverflowingNegAssign
262    + OverflowingPow<u64, Output = Self>
263    + OverflowingPowAssign<u64>
264    + OverflowingSquare<Output = Self>
265    + OverflowingSquareAssign
266    + OverflowingSub<Self, Output = Self>
267    + OverflowingSubAssign<Self>
268    + OverflowingSubMul<Self, Self, Output = Self>
269    + OverflowingSubMulAssign<Self, Self>
270    + Parity
271    + PartialEq<Self>
272    + PartialOrd<Self>
273    + Pow<u64, Output = Self>
274    + PowAssign<u64>
275    + PowerOf2<u64>
276    + Product
277    + RefUnwindSafe
278    + Rem<Self, Output = Self>
279    + RemAssign<Self>
280    + RemPowerOf2<Output = Self>
281    + RemPowerOf2Assign
282    + RotateLeft<Output = Self>
283    + RotateLeftAssign
284    + RotateRight<Output = Self>
285    + RotateRightAssign
286    + RoundToMultiple<Self, Output = Self>
287    + RoundToMultipleAssign<Self>
288    + RoundToMultipleOfPowerOf2<u64, Output = Self>
289    + RoundToMultipleOfPowerOf2Assign<u64>
290    + RoundingFrom<f32>
291    + RoundingFrom<f64>
292    + RoundingInto<f32>
293    + RoundingInto<f64>
294    + SaturatingAdd<Self, Output = Self>
295    + SaturatingAddAssign<Self>
296    + SaturatingAddMul<Self, Self, Output = Self>
297    + SaturatingAddMulAssign<Self, Self>
298    + SaturatingFrom<i128>
299    + SaturatingFrom<i16>
300    + SaturatingFrom<i32>
301    + SaturatingFrom<i64>
302    + SaturatingFrom<i8>
303    + SaturatingFrom<isize>
304    + SaturatingFrom<u128>
305    + SaturatingFrom<u16>
306    + SaturatingFrom<u32>
307    + SaturatingFrom<u64>
308    + SaturatingFrom<u8>
309    + SaturatingFrom<usize>
310    + SaturatingInto<i128>
311    + SaturatingInto<i16>
312    + SaturatingInto<i32>
313    + SaturatingInto<i64>
314    + SaturatingInto<i8>
315    + SaturatingInto<isize>
316    + SaturatingInto<u128>
317    + SaturatingInto<u16>
318    + SaturatingInto<u32>
319    + SaturatingInto<u64>
320    + SaturatingInto<u8>
321    + SaturatingInto<usize>
322    + SaturatingMul<Self, Output = Self>
323    + SaturatingMulAssign<Self>
324    + SaturatingPow<u64, Output = Self>
325    + SaturatingPowAssign<u64>
326    + SaturatingSquare<Output = Self>
327    + SaturatingSquareAssign
328    + SaturatingSub<Self, Output = Self>
329    + SaturatingSubAssign<Self>
330    + SaturatingSubMul<Self, Self, Output = Self>
331    + SaturatingSubMulAssign<Self, Self>
332    + Shl<i128, Output = Self>
333    + Shl<i16, Output = Self>
334    + Shl<i32, Output = Self>
335    + Shl<i64, Output = Self>
336    + Shl<i8, Output = Self>
337    + Shl<u128, Output = Self>
338    + Shl<u16, Output = Self>
339    + Shl<u32, Output = Self>
340    + Shl<u64, Output = Self>
341    + Shl<u8, Output = Self>
342    + ShlAssign<i128>
343    + ShlAssign<i16>
344    + ShlAssign<i32>
345    + ShlAssign<i64>
346    + ShlAssign<i8>
347    + ShlAssign<isize>
348    + ShlAssign<u128>
349    + ShlAssign<u16>
350    + ShlAssign<u32>
351    + ShlAssign<u64>
352    + ShlAssign<u8>
353    + ShlAssign<usize>
354    + ShlRound<i128, Output = Self>
355    + ShlRound<i16, Output = Self>
356    + ShlRound<i32, Output = Self>
357    + ShlRound<i64, Output = Self>
358    + ShlRound<i8, Output = Self>
359    + ShlRound<isize, Output = Self>
360    + ShlRoundAssign<i128>
361    + ShlRoundAssign<i16>
362    + ShlRoundAssign<i32>
363    + ShlRoundAssign<i64>
364    + ShlRoundAssign<i8>
365    + ShlRoundAssign<isize>
366    + Shr<i128, Output = Self>
367    + Shr<i16, Output = Self>
368    + Shr<i32, Output = Self>
369    + Shr<i64, Output = Self>
370    + Shr<i8, Output = Self>
371    + Shr<isize, Output = Self>
372    + Shr<u128, Output = Self>
373    + Shr<u16, Output = Self>
374    + Shr<u32, Output = Self>
375    + Shr<u64, Output = Self>
376    + Shr<u8, Output = Self>
377    + Shr<usize, Output = Self>
378    + ShrAssign<i128>
379    + ShrAssign<i16>
380    + ShrAssign<i32>
381    + ShrAssign<i64>
382    + ShrAssign<i8>
383    + ShrAssign<isize>
384    + ShrAssign<u128>
385    + ShrAssign<u16>
386    + ShrAssign<u32>
387    + ShrAssign<u64>
388    + ShrAssign<u8>
389    + ShrAssign<usize>
390    + ShrRound<i128, Output = Self>
391    + ShrRound<i16, Output = Self>
392    + ShrRound<i32, Output = Self>
393    + ShrRound<i64, Output = Self>
394    + ShrRound<i8, Output = Self>
395    + ShrRound<isize, Output = Self>
396    + ShrRound<u128, Output = Self>
397    + ShrRound<u16, Output = Self>
398    + ShrRound<u32, Output = Self>
399    + ShrRound<u64, Output = Self>
400    + ShrRound<u8, Output = Self>
401    + ShrRound<usize, Output = Self>
402    + ShrRoundAssign<i128>
403    + ShrRoundAssign<i16>
404    + ShrRoundAssign<i32>
405    + ShrRoundAssign<i64>
406    + ShrRoundAssign<i8>
407    + ShrRoundAssign<isize>
408    + ShrRoundAssign<u128>
409    + ShrRoundAssign<u16>
410    + ShrRoundAssign<u32>
411    + ShrRoundAssign<u64>
412    + ShrRoundAssign<u8>
413    + ShrRoundAssign<usize>
414    + Sign
415    + SignificantBits
416    + Sized
417    + Square<Output = Self>
418    + SquareAssign
419    + Sub<Self, Output = Self>
420    + SubAssign<Self>
421    + SubMul<Self, Self, Output = Self>
422    + SubMulAssign<Self, Self>
423    + Sum<Self>
424    + ToSci
425    + ToStringBase
426    + TrailingZeros
427    + TryFrom<NiceFloat<f32>>
428    + TryFrom<i128>
429    + TryFrom<i16>
430    + TryFrom<i32>
431    + TryFrom<i64>
432    + TryFrom<i8>
433    + TryFrom<isize>
434    + TryFrom<u128>
435    + TryFrom<u16>
436    + TryFrom<u32>
437    + TryFrom<u64>
438    + TryFrom<u8>
439    + TryFrom<usize>
440    + TryInto<NiceFloat<f32>>
441    + TryInto<i128>
442    + TryInto<i16>
443    + TryInto<i32>
444    + TryInto<i64>
445    + TryInto<i8>
446    + TryInto<isize>
447    + TryInto<u128>
448    + TryInto<u16>
449    + TryInto<u32>
450    + TryInto<u64>
451    + TryInto<u8>
452    + TryInto<usize>
453    + Two
454    + UnwindSafe
455    + UpperHex
456    + WrappingAdd<Self, Output = Self>
457    + WrappingAddAssign<Self>
458    + WrappingAddMul<Self, Self, Output = Self>
459    + WrappingAddMulAssign<Self, Self>
460    + WrappingDiv<Self, Output = Self>
461    + WrappingDivAssign<Self>
462    + WrappingFrom<i128>
463    + WrappingFrom<i16>
464    + WrappingFrom<i32>
465    + WrappingFrom<i64>
466    + WrappingFrom<i8>
467    + WrappingFrom<isize>
468    + WrappingFrom<u128>
469    + WrappingFrom<u16>
470    + WrappingFrom<u32>
471    + WrappingFrom<u64>
472    + WrappingFrom<u8>
473    + WrappingFrom<usize>
474    + WrappingInto<i128>
475    + WrappingInto<i16>
476    + WrappingInto<i32>
477    + WrappingInto<i64>
478    + WrappingInto<i8>
479    + WrappingInto<isize>
480    + WrappingInto<u128>
481    + WrappingInto<u16>
482    + WrappingInto<u32>
483    + WrappingInto<u64>
484    + WrappingInto<u8>
485    + WrappingInto<usize>
486    + WrappingMul<Self, Output = Self>
487    + WrappingMulAssign<Self>
488    + WrappingNeg<Output = Self>
489    + WrappingNegAssign
490    + WrappingPow<u64, Output = Self>
491    + WrappingPowAssign<u64>
492    + WrappingSquare<Output = Self>
493    + WrappingSquareAssign
494    + WrappingSub<Self, Output = Self>
495    + WrappingSubAssign<Self>
496    + WrappingSubMul<Self, Self, Output = Self>
497    + WrappingSubMulAssign<Self, Self>
498    + Zero
499{
500    /// The number of bits of `Self`.
501    const WIDTH: u64;
502
503    /// The base-2 logarithm of the number of bits of `Self`.
504    ///
505    /// Whenever you need to use `n / WIDTH`, you can use `n >> LOG_WIDTH` instead.
506    ///
507    /// This is $\log_2 W$.
508    ///
509    /// Note that this value is correct for all of the built-in primitive integer types, but it will
510    /// not be correct for custom types whose $W$ is not a power of 2. For such implementations,
511    /// `LOG_WIDTH` should not be used.
512    const LOG_WIDTH: u64 = Self::WIDTH.trailing_zeros() as u64;
513
514    /// A mask that consists of `LOG_WIDTH` bits.
515    ///
516    /// Whenever you need to use `n % WIDTH`, you can use `n & WIDTH_MASK` instead.
517    ///
518    /// This is $W - 1$.
519    ///
520    /// Note that this value is correct for all of the built-in primitive integer types, but it will
521    /// not be correct for custom types whose $W$ is not a power of 2. For such implementations,
522    /// `WIDTH_MASK` should not be used.
523    const WIDTH_MASK: u64 = Self::WIDTH - 1;
524
525    /// Gets the most-significant bit of `Self`. For signed integers, this is the sign bit.
526    ///
527    /// If `Self` is unsigned, $f(n) = (n \geq 2^{W-1})$. If `Self` is unsigned, $f(n) = (n < 0)$.
528    ///
529    /// # Worst-case complexity
530    /// Constant time and additional memory.
531    ///
532    /// # Examples
533    /// ```
534    /// use malachite_base::num::basic::integers::PrimitiveInt;
535    ///
536    /// assert_eq!(123u32.get_highest_bit(), false);
537    /// assert_eq!(4000000000u32.get_highest_bit(), true);
538    /// assert_eq!(2000000000i32.get_highest_bit(), false);
539    /// assert_eq!((-2000000000i32).get_highest_bit(), true);
540    /// ```
541    #[inline]
542    fn get_highest_bit(&self) -> bool {
543        self.get_bit(Self::WIDTH - 1)
544    }
545}
546
547#[cfg(not(feature = "random"))]
548/// Defines functions on primitive integer types: uxx, ixx, usize, and isize.
549///
550/// The different types are distinguished by whether they are signed or unsigned, and by their
551/// widths. The width $W$ is the number of bits in the type. For example, the width of [`u32`] or
552/// [`i32`] is 32. Each type has $2^W$ distinct values.
553///
554/// Let $n$ be a value of type `Self`. If `Self` is unsigned, $0 \leq n < 2^W$. If `Self` is signed,
555/// $2^{W-1} \leq n < 2^{W-1}$.
556pub trait PrimitiveInt:
557    'static
558    + AbsDiff<Self>
559    + Add<Self, Output = Self>
560    + AddAssign<Self>
561    + AddMul<Self, Self, Output = Self>
562    + AddMulAssign<Self, Self>
563    + ArithmeticCheckedShl<i128, Output = Self>
564    + ArithmeticCheckedShl<i16, Output = Self>
565    + ArithmeticCheckedShl<i32, Output = Self>
566    + ArithmeticCheckedShl<i64, Output = Self>
567    + ArithmeticCheckedShl<i8, Output = Self>
568    + ArithmeticCheckedShl<isize, Output = Self>
569    + ArithmeticCheckedShl<u128, Output = Self>
570    + ArithmeticCheckedShl<u16, Output = Self>
571    + ArithmeticCheckedShl<u32, Output = Self>
572    + ArithmeticCheckedShl<u64, Output = Self>
573    + ArithmeticCheckedShl<u8, Output = Self>
574    + ArithmeticCheckedShl<usize, Output = Self>
575    + ArithmeticCheckedShr<i128, Output = Self>
576    + ArithmeticCheckedShr<i16, Output = Self>
577    + ArithmeticCheckedShr<i32, Output = Self>
578    + ArithmeticCheckedShr<i64, Output = Self>
579    + ArithmeticCheckedShr<i8, Output = Self>
580    + ArithmeticCheckedShr<isize, Output = Self>
581    + Binary
582    + BinomialCoefficient<Self>
583    + BitAccess
584    + BitAnd<Self, Output = Self>
585    + BitAndAssign<Self>
586    + BitBlockAccess
587    + BitConvertible
588    + BitIterable
589    + BitOr<Self, Output = Self>
590    + BitOrAssign<Self>
591    + BitScan
592    + BitXor<Self, Output = Self>
593    + BitXorAssign<Self>
594    + CeilingRoot<u64, Output = Self>
595    + CeilingRootAssign<u64>
596    + CeilingSqrt<Output = Self>
597    + CeilingSqrtAssign
598    + CheckedAdd<Self, Output = Self>
599    + CheckedAddMul<Self, Self, Output = Self>
600    + CheckedBinomialCoefficient<Self>
601    + CheckedDiv<Self, Output = Self>
602    + CheckedMul<Self, Output = Self>
603    + CheckedNeg<Output = Self>
604    + CheckedPow<u64, Output = Self>
605    + CheckedRoot<u64, Output = Self>
606    + CheckedSqrt<Output = Self>
607    + CheckedSquare<Output = Self>
608    + CheckedSub<Self, Output = Self>
609    + CheckedSubMul<Self, Self, Output = Self>
610    + Clone
611    + ConvertibleFrom<f32>
612    + ConvertibleFrom<f64>
613    + ConvertibleFrom<i128>
614    + ConvertibleFrom<i16>
615    + ConvertibleFrom<i32>
616    + ConvertibleFrom<i64>
617    + ConvertibleFrom<i8>
618    + ConvertibleFrom<isize>
619    + ConvertibleFrom<u128>
620    + ConvertibleFrom<u16>
621    + ConvertibleFrom<u32>
622    + ConvertibleFrom<u64>
623    + ConvertibleFrom<u8>
624    + ConvertibleFrom<usize>
625    + Copy
626    + CountOnes
627    + CountZeros
628    + Debug
629    + Default
630    + Display
631    + Div<Self, Output = Self>
632    + DivAssign<Self>
633    + DivAssignEuclidean<Self, ModOutput = Self>
634    + DivAssignMod<Self, ModOutput = Self>
635    + DivAssignRem<Self, RemOutput = Self>
636    + DivEuclidean<Self, DivOutput = Self, ModOutput = Self>
637    + DivExact<Self, Output = Self>
638    + DivExactAssign<Self>
639    + DivMod<Self, DivOutput = Self, ModOutput = Self>
640    + DivRem<Self, DivOutput = Self, RemOutput = Self>
641    + DivRound<Self, Output = Self>
642    + DivRoundAssign<Self>
643    + DivisibleBy<Self>
644    + DivisibleByPowerOf2
645    + Eq
646    + EqMod<Self, Self>
647    + EqModPowerOf2<Self>
648    + ExactFrom<i128>
649    + ExactFrom<i16>
650    + ExactFrom<i32>
651    + ExactFrom<i64>
652    + ExactFrom<i8>
653    + ExactFrom<isize>
654    + ExactFrom<u128>
655    + ExactFrom<u16>
656    + ExactFrom<u32>
657    + ExactFrom<u64>
658    + ExactFrom<u8>
659    + ExactFrom<usize>
660    + ExactInto<i128>
661    + ExactInto<i16>
662    + ExactInto<i32>
663    + ExactInto<i64>
664    + ExactInto<i8>
665    + ExactInto<isize>
666    + ExactInto<u128>
667    + ExactInto<u16>
668    + ExactInto<u32>
669    + ExactInto<u64>
670    + ExactInto<u8>
671    + ExactInto<usize>
672    + ExpressAsPower
673    + ExtendedGcd<Self>
674    + FloorRoot<u64, Output = Self>
675    + FloorRootAssign<u64>
676    + FloorSqrt<Output = Self>
677    + FloorSqrtAssign
678    + From<bool>
679    + FromSciString
680    + FromStr
681    + FromStringBase
682    + Hash
683    + IsInteger
684    + IsPower
685    + IsSquare
686    + JacobiSymbol<Self>
687    + KroneckerSymbol<Self>
688    + LeadingZeros
689    + LegendreSymbol<Self>
690    + LowMask
691    + LowerHex
692    + Max
693    + Min
694    + Mod<Self, Output = Self>
695    + ModAssign<Self>
696    + ModPowerOf2
697    + ModPowerOf2Assign
698    + Mul<Self, Output = Self>
699    + MulAssign<Self>
700    + Named
701    + Not<Output = Self>
702    + NotAssign
703    + Octal
704    + One
705    + Ord
706    + OverflowingAdd<Self, Output = Self>
707    + OverflowingAddAssign<Self>
708    + OverflowingAddMul<Self, Self, Output = Self>
709    + OverflowingAddMulAssign<Self, Self>
710    + OverflowingDiv<Self, Output = Self>
711    + OverflowingDivAssign<Self>
712    + OverflowingFrom<i128>
713    + OverflowingFrom<i16>
714    + OverflowingFrom<i32>
715    + OverflowingFrom<i64>
716    + OverflowingFrom<i8>
717    + OverflowingFrom<isize>
718    + OverflowingFrom<u128>
719    + OverflowingFrom<u16>
720    + OverflowingFrom<u32>
721    + OverflowingFrom<u64>
722    + OverflowingFrom<u8>
723    + OverflowingFrom<usize>
724    + OverflowingInto<i128>
725    + OverflowingInto<i16>
726    + OverflowingInto<i32>
727    + OverflowingInto<i64>
728    + OverflowingInto<i8>
729    + OverflowingInto<isize>
730    + OverflowingInto<u128>
731    + OverflowingInto<u16>
732    + OverflowingInto<u32>
733    + OverflowingInto<u64>
734    + OverflowingInto<u8>
735    + OverflowingInto<usize>
736    + OverflowingMul<Self, Output = Self>
737    + OverflowingMulAssign<Self>
738    + OverflowingNeg<Output = Self>
739    + OverflowingNegAssign
740    + OverflowingPow<u64, Output = Self>
741    + OverflowingPowAssign<u64>
742    + OverflowingSquare<Output = Self>
743    + OverflowingSquareAssign
744    + OverflowingSub<Self, Output = Self>
745    + OverflowingSubAssign<Self>
746    + OverflowingSubMul<Self, Self, Output = Self>
747    + OverflowingSubMulAssign<Self, Self>
748    + Parity
749    + PartialEq<Self>
750    + PartialOrd<Self>
751    + Pow<u64, Output = Self>
752    + PowAssign<u64>
753    + PowerOf2<u64>
754    + Product
755    + RefUnwindSafe
756    + Rem<Self, Output = Self>
757    + RemAssign<Self>
758    + RemPowerOf2<Output = Self>
759    + RemPowerOf2Assign
760    + RotateLeft<Output = Self>
761    + RotateLeftAssign
762    + RotateRight<Output = Self>
763    + RotateRightAssign
764    + RoundToMultiple<Self, Output = Self>
765    + RoundToMultipleAssign<Self>
766    + RoundToMultipleOfPowerOf2<u64, Output = Self>
767    + RoundToMultipleOfPowerOf2Assign<u64>
768    + RoundingFrom<f32>
769    + RoundingFrom<f64>
770    + RoundingInto<f32>
771    + RoundingInto<f64>
772    + SaturatingAdd<Self, Output = Self>
773    + SaturatingAddAssign<Self>
774    + SaturatingAddMul<Self, Self, Output = Self>
775    + SaturatingAddMulAssign<Self, Self>
776    + SaturatingFrom<i128>
777    + SaturatingFrom<i16>
778    + SaturatingFrom<i32>
779    + SaturatingFrom<i64>
780    + SaturatingFrom<i8>
781    + SaturatingFrom<isize>
782    + SaturatingFrom<u128>
783    + SaturatingFrom<u16>
784    + SaturatingFrom<u32>
785    + SaturatingFrom<u64>
786    + SaturatingFrom<u8>
787    + SaturatingFrom<usize>
788    + SaturatingInto<i128>
789    + SaturatingInto<i16>
790    + SaturatingInto<i32>
791    + SaturatingInto<i64>
792    + SaturatingInto<i8>
793    + SaturatingInto<isize>
794    + SaturatingInto<u128>
795    + SaturatingInto<u16>
796    + SaturatingInto<u32>
797    + SaturatingInto<u64>
798    + SaturatingInto<u8>
799    + SaturatingInto<usize>
800    + SaturatingMul<Self, Output = Self>
801    + SaturatingMulAssign<Self>
802    + SaturatingPow<u64, Output = Self>
803    + SaturatingPowAssign<u64>
804    + SaturatingSquare<Output = Self>
805    + SaturatingSquareAssign
806    + SaturatingSub<Self, Output = Self>
807    + SaturatingSubAssign<Self>
808    + SaturatingSubMul<Self, Self, Output = Self>
809    + SaturatingSubMulAssign<Self, Self>
810    + Shl<i128, Output = Self>
811    + Shl<i16, Output = Self>
812    + Shl<i32, Output = Self>
813    + Shl<i64, Output = Self>
814    + Shl<i8, Output = Self>
815    + Shl<u128, Output = Self>
816    + Shl<u16, Output = Self>
817    + Shl<u32, Output = Self>
818    + Shl<u64, Output = Self>
819    + Shl<u8, Output = Self>
820    + ShlAssign<i128>
821    + ShlAssign<i16>
822    + ShlAssign<i32>
823    + ShlAssign<i64>
824    + ShlAssign<i8>
825    + ShlAssign<isize>
826    + ShlAssign<u128>
827    + ShlAssign<u16>
828    + ShlAssign<u32>
829    + ShlAssign<u64>
830    + ShlAssign<u8>
831    + ShlAssign<usize>
832    + ShlRound<i128, Output = Self>
833    + ShlRound<i16, Output = Self>
834    + ShlRound<i32, Output = Self>
835    + ShlRound<i64, Output = Self>
836    + ShlRound<i8, Output = Self>
837    + ShlRound<isize, Output = Self>
838    + ShlRoundAssign<i128>
839    + ShlRoundAssign<i16>
840    + ShlRoundAssign<i32>
841    + ShlRoundAssign<i64>
842    + ShlRoundAssign<i8>
843    + ShlRoundAssign<isize>
844    + Shr<i128, Output = Self>
845    + Shr<i16, Output = Self>
846    + Shr<i32, Output = Self>
847    + Shr<i64, Output = Self>
848    + Shr<i8, Output = Self>
849    + Shr<isize, Output = Self>
850    + Shr<u128, Output = Self>
851    + Shr<u16, Output = Self>
852    + Shr<u32, Output = Self>
853    + Shr<u64, Output = Self>
854    + Shr<u8, Output = Self>
855    + Shr<usize, Output = Self>
856    + ShrAssign<i128>
857    + ShrAssign<i16>
858    + ShrAssign<i32>
859    + ShrAssign<i64>
860    + ShrAssign<i8>
861    + ShrAssign<isize>
862    + ShrAssign<u128>
863    + ShrAssign<u16>
864    + ShrAssign<u32>
865    + ShrAssign<u64>
866    + ShrAssign<u8>
867    + ShrAssign<usize>
868    + ShrRound<i128, Output = Self>
869    + ShrRound<i16, Output = Self>
870    + ShrRound<i32, Output = Self>
871    + ShrRound<i64, Output = Self>
872    + ShrRound<i8, Output = Self>
873    + ShrRound<isize, Output = Self>
874    + ShrRound<u128, Output = Self>
875    + ShrRound<u16, Output = Self>
876    + ShrRound<u32, Output = Self>
877    + ShrRound<u64, Output = Self>
878    + ShrRound<u8, Output = Self>
879    + ShrRound<usize, Output = Self>
880    + ShrRoundAssign<i128>
881    + ShrRoundAssign<i16>
882    + ShrRoundAssign<i32>
883    + ShrRoundAssign<i64>
884    + ShrRoundAssign<i8>
885    + ShrRoundAssign<isize>
886    + ShrRoundAssign<u128>
887    + ShrRoundAssign<u16>
888    + ShrRoundAssign<u32>
889    + ShrRoundAssign<u64>
890    + ShrRoundAssign<u8>
891    + ShrRoundAssign<usize>
892    + Sign
893    + SignificantBits
894    + Sized
895    + Square<Output = Self>
896    + SquareAssign
897    + Sub<Self, Output = Self>
898    + SubAssign<Self>
899    + SubMul<Self, Self, Output = Self>
900    + SubMulAssign<Self, Self>
901    + Sum<Self>
902    + ToSci
903    + ToStringBase
904    + TrailingZeros
905    + TryFrom<NiceFloat<f32>>
906    + TryFrom<i128>
907    + TryFrom<i16>
908    + TryFrom<i32>
909    + TryFrom<i64>
910    + TryFrom<i8>
911    + TryFrom<isize>
912    + TryFrom<u128>
913    + TryFrom<u16>
914    + TryFrom<u32>
915    + TryFrom<u64>
916    + TryFrom<u8>
917    + TryFrom<usize>
918    + TryInto<NiceFloat<f32>>
919    + TryInto<i128>
920    + TryInto<i16>
921    + TryInto<i32>
922    + TryInto<i64>
923    + TryInto<i8>
924    + TryInto<isize>
925    + TryInto<u128>
926    + TryInto<u16>
927    + TryInto<u32>
928    + TryInto<u64>
929    + TryInto<u8>
930    + TryInto<usize>
931    + Two
932    + UnwindSafe
933    + UpperHex
934    + WrappingAdd<Self, Output = Self>
935    + WrappingAddAssign<Self>
936    + WrappingAddMul<Self, Self, Output = Self>
937    + WrappingAddMulAssign<Self, Self>
938    + WrappingDiv<Self, Output = Self>
939    + WrappingDivAssign<Self>
940    + WrappingFrom<i128>
941    + WrappingFrom<i16>
942    + WrappingFrom<i32>
943    + WrappingFrom<i64>
944    + WrappingFrom<i8>
945    + WrappingFrom<isize>
946    + WrappingFrom<u128>
947    + WrappingFrom<u16>
948    + WrappingFrom<u32>
949    + WrappingFrom<u64>
950    + WrappingFrom<u8>
951    + WrappingFrom<usize>
952    + WrappingInto<i128>
953    + WrappingInto<i16>
954    + WrappingInto<i32>
955    + WrappingInto<i64>
956    + WrappingInto<i8>
957    + WrappingInto<isize>
958    + WrappingInto<u128>
959    + WrappingInto<u16>
960    + WrappingInto<u32>
961    + WrappingInto<u64>
962    + WrappingInto<u8>
963    + WrappingInto<usize>
964    + WrappingMul<Self, Output = Self>
965    + WrappingMulAssign<Self>
966    + WrappingNeg<Output = Self>
967    + WrappingNegAssign
968    + WrappingPow<u64, Output = Self>
969    + WrappingPowAssign<u64>
970    + WrappingSquare<Output = Self>
971    + WrappingSquareAssign
972    + WrappingSub<Self, Output = Self>
973    + WrappingSubAssign<Self>
974    + WrappingSubMul<Self, Self, Output = Self>
975    + WrappingSubMulAssign<Self, Self>
976    + Zero
977{
978    /// The number of bits of `Self`.
979    const WIDTH: u64;
980
981    /// The base-2 logarithm of the number of bits of `Self`.
982    ///
983    /// Whenever you need to use `n / WIDTH`, you can use `n >> LOG_WIDTH` instead.
984    ///
985    /// This is $\log_2 W$.
986    ///
987    /// Note that this value is correct for all of the built-in primitive integer types, but it will
988    /// not be correct for custom types whose $W$ is not a power of 2. For such implementations,
989    /// `LOG_WIDTH` should not be used.
990    const LOG_WIDTH: u64 = Self::WIDTH.trailing_zeros() as u64;
991
992    /// A mask that consists of `LOG_WIDTH` bits.
993    ///
994    /// Whenever you need to use `n % WIDTH`, you can use `n & WIDTH_MASK` instead.
995    ///
996    /// This is $W - 1$.
997    ///
998    /// Note that this value is correct for all of the built-in primitive integer types, but it will
999    /// not be correct for custom types whose $W$ is not a power of 2. For such implementations,
1000    /// `WIDTH_MASK` should not be used.
1001    const WIDTH_MASK: u64 = Self::WIDTH - 1;
1002
1003    /// Gets the most-significant bit of `Self`. For signed integers, this is the sign bit.
1004    ///
1005    /// If `Self` is unsigned, $f(n) = (n \geq 2^{W-1})$. If `Self` is unsigned, $f(n) = (n < 0)$.
1006    ///
1007    /// # Worst-case complexity
1008    /// Constant time and additional memory.
1009    ///
1010    /// # Examples
1011    /// ```
1012    /// use malachite_base::num::basic::integers::PrimitiveInt;
1013    ///
1014    /// assert_eq!(123u32.get_highest_bit(), false);
1015    /// assert_eq!(4000000000u32.get_highest_bit(), true);
1016    /// assert_eq!(2000000000i32.get_highest_bit(), false);
1017    /// assert_eq!((-2000000000i32).get_highest_bit(), true);
1018    /// ```
1019    #[inline]
1020    fn get_highest_bit(&self) -> bool {
1021        self.get_bit(Self::WIDTH - 1)
1022    }
1023}
1024
1025/// Defines basic trait implementations that are the same for unsigned and signed types.
1026macro_rules! impl_basic_traits_primitive_int {
1027    ($t:ident, $width:expr) => {
1028        /// # Examples
1029        ///
1030        /// See [here](self).
1031        impl PrimitiveInt for $t {
1032            const WIDTH: u64 = $width;
1033        }
1034
1035        impl_named!($t);
1036
1037        /// The constant 0.
1038        ///
1039        /// # Examples
1040        /// See [here](self).
1041        impl Zero for $t {
1042            const ZERO: $t = 0;
1043        }
1044
1045        /// The constant 1.
1046        ///
1047        /// # Examples
1048        /// See [here](self).
1049        impl One for $t {
1050            const ONE: $t = 1;
1051        }
1052
1053        /// The constant 2.
1054        ///
1055        /// # Examples
1056        /// See [here](self).
1057        impl Two for $t {
1058            const TWO: $t = 2;
1059        }
1060
1061        /// The lowest value representable by this type.
1062        ///
1063        /// If `Self` is unsigned, `MIN` is 0. If `Self` is signed, `MIN` is $-2^{W-1}$.
1064        ///
1065        /// # Examples
1066        /// See [here](self).
1067        impl Min for $t {
1068            const MIN: $t = $t::MIN;
1069        }
1070
1071        /// The highest value representable by this type.
1072        ///
1073        /// If `Self` is unsigned, `MAX` is $2^W-1$. If `Self` is signed, `MAX` is $2^{W-1}-1$.
1074        ///
1075        /// # Examples
1076        /// See [here](self).
1077        impl Max for $t {
1078            const MAX: $t = $t::MAX;
1079        }
1080    };
1081}
1082impl_basic_traits_primitive_int!(u8, 8);
1083impl_basic_traits_primitive_int!(u16, 16);
1084impl_basic_traits_primitive_int!(u32, 32);
1085impl_basic_traits_primitive_int!(u64, 64);
1086impl_basic_traits_primitive_int!(u128, 128);
1087impl_basic_traits_primitive_int!(usize, 0usize.trailing_zeros() as u64);
1088impl_basic_traits_primitive_int!(i8, 8);
1089impl_basic_traits_primitive_int!(i16, 16);
1090impl_basic_traits_primitive_int!(i32, 32);
1091impl_basic_traits_primitive_int!(i64, 64);
1092impl_basic_traits_primitive_int!(i128, 128);
1093impl_basic_traits_primitive_int!(isize, 0usize.trailing_zeros() as u64);