pub struct ZERO { /* private fields */ }
Methods from Deref<Target = Integer>§
Sourcepub fn unsigned_abs_ref(&self) -> &Natural
pub fn unsigned_abs_ref(&self) -> &Natural
Finds the absolute value of an Integer
, taking the Integer
by reference and
returning a reference to the internal Natural
absolute value.
$$ f(x) = |x|. $$
§Worst-case complexity
Constant time and additional memory.
§Examples
use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;
assert_eq!(*Integer::ZERO.unsigned_abs_ref(), 0);
assert_eq!(*Integer::from(123).unsigned_abs_ref(), 123);
assert_eq!(*Integer::from(-123).unsigned_abs_ref(), 123);
Sourcepub fn to_twos_complement_limbs_asc(&self) -> Vec<u64>
pub fn to_twos_complement_limbs_asc(&self) -> Vec<u64>
Returns the limbs of an Integer
, in ascending order, so that less
significant limbs have lower indices in the output vector.
The limbs are in two’s complement, and the most significant bit of the limbs indicates the
sign; if the bit is zero, the Integer
is positive, and if the bit is one it is negative.
There are no trailing zero limbs if the Integer
is positive or trailing Limb::MAX
limbs if the Integer
is negative, except as necessary to include the correct sign bit.
Zero is a special case: it contains no limbs.
This function borrows self
. If taking ownership of self
is possible,
into_twos_complement_limbs_asc
is more
efficient.
This function is more efficient than
to_twos_complement_limbs_desc
.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(n)$
where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits()
.
§Examples
use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::basic::integers::PrimitiveInt;
use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;
use malachite_nz::platform::Limb;
if Limb::WIDTH == u32::WIDTH {
assert!(Integer::ZERO.to_twos_complement_limbs_asc().is_empty());
assert_eq!(Integer::from(123).to_twos_complement_limbs_asc(), &[123]);
assert_eq!(
Integer::from(-123).to_twos_complement_limbs_asc(),
&[4294967173]
);
// 10^12 = 232 * 2^32 + 3567587328
assert_eq!(
Integer::from(10u32).pow(12).to_twos_complement_limbs_asc(),
&[3567587328, 232]
);
assert_eq!(
(-Integer::from(10u32).pow(12)).to_twos_complement_limbs_asc(),
&[727379968, 4294967063]
);
}
Sourcepub fn to_twos_complement_limbs_desc(&self) -> Vec<u64>
pub fn to_twos_complement_limbs_desc(&self) -> Vec<u64>
Returns the limbs of an Integer
, in descending order, so that less
significant limbs have higher indices in the output vector.
The limbs are in two’s complement, and the most significant bit of the limbs indicates the
sign; if the bit is zero, the Integer
is positive, and if the bit is one it is negative.
There are no leading zero limbs if the Integer
is non-negative or leading Limb::MAX
limbs if the Integer
is negative, except as necessary to include the correct sign bit.
Zero is a special case: it contains no limbs.
This is similar to how BigInteger
s in Java are represented.
This function borrows self
. If taking ownership of self
is possible,
into_twos_complement_limbs_desc
is more
efficient.
This function is less efficient than
to_twos_complement_limbs_asc
.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(n)$
where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits()
.
§Examples
use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::basic::integers::PrimitiveInt;
use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;
use malachite_nz::platform::Limb;
if Limb::WIDTH == u32::WIDTH {
assert!(Integer::ZERO.to_twos_complement_limbs_desc().is_empty());
assert_eq!(Integer::from(123).to_twos_complement_limbs_desc(), &[123]);
assert_eq!(
Integer::from(-123).to_twos_complement_limbs_desc(),
&[4294967173]
);
// 10^12 = 232 * 2^32 + 3567587328
assert_eq!(
Integer::from(10u32).pow(12).to_twos_complement_limbs_desc(),
&[232, 3567587328]
);
assert_eq!(
(-Integer::from(10u32).pow(12)).to_twos_complement_limbs_desc(),
&[4294967063, 727379968]
);
}
Sourcepub fn twos_complement_limbs(&self) -> TwosComplementLimbIterator<'_>
pub fn twos_complement_limbs(&self) -> TwosComplementLimbIterator<'_>
Returns a double-ended iterator over the twos-complement limbs of an
Integer
.
The forward order is ascending, so that less significant limbs appear first. There may be a most-significant sign-extension limb.
If it’s necessary to get a Vec
of all the twos_complement limbs, consider using
to_twos_complement_limbs_asc
,
to_twos_complement_limbs_desc
,
into_twos_complement_limbs_asc
, or
into_twos_complement_limbs_desc
instead.
§Worst-case complexity
Constant time and additional memory.
§Examples
use itertools::Itertools;
use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::basic::integers::PrimitiveInt;
use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;
use malachite_nz::platform::Limb;
if Limb::WIDTH == u32::WIDTH {
assert!(Integer::ZERO.twos_complement_limbs().next().is_none());
assert_eq!(
Integer::from(123).twos_complement_limbs().collect_vec(),
&[123]
);
assert_eq!(
Integer::from(-123).twos_complement_limbs().collect_vec(),
&[4294967173]
);
// 10^12 = 232 * 2^32 + 3567587328
assert_eq!(
Integer::from(10u32)
.pow(12)
.twos_complement_limbs()
.collect_vec(),
&[3567587328, 232]
);
// Sign-extension for a non-negative `Integer`
assert_eq!(
Integer::from(4294967295i64)
.twos_complement_limbs()
.collect_vec(),
&[4294967295, 0]
);
assert_eq!(
(-Integer::from(10u32).pow(12))
.twos_complement_limbs()
.collect_vec(),
&[727379968, 4294967063]
);
// Sign-extension for a negative `Integer`
assert_eq!(
(-Integer::from(4294967295i64))
.twos_complement_limbs()
.collect_vec(),
&[1, 4294967295]
);
assert!(Integer::ZERO.twos_complement_limbs().next_back().is_none());
assert_eq!(
Integer::from(123)
.twos_complement_limbs()
.rev()
.collect_vec(),
&[123]
);
assert_eq!(
Integer::from(-123)
.twos_complement_limbs()
.rev()
.collect_vec(),
&[4294967173]
);
// 10^12 = 232 * 2^32 + 3567587328
assert_eq!(
Integer::from(10u32)
.pow(12)
.twos_complement_limbs()
.rev()
.collect_vec(),
&[232, 3567587328]
);
// Sign-extension for a non-negative `Integer`
assert_eq!(
Integer::from(4294967295i64)
.twos_complement_limbs()
.rev()
.collect_vec(),
&[0, 4294967295]
);
assert_eq!(
(-Integer::from(10u32).pow(12))
.twos_complement_limbs()
.rev()
.collect_vec(),
&[4294967063, 727379968]
);
// Sign-extension for a negative `Integer`
assert_eq!(
(-Integer::from(4294967295i64))
.twos_complement_limbs()
.rev()
.collect_vec(),
&[4294967295, 1]
);
}
Sourcepub fn twos_complement_limb_count(&self) -> u64
pub fn twos_complement_limb_count(&self) -> u64
Returns the number of twos-complement limbs of an Integer
. There may be a
most-significant sign-extension limb, which is included in the count.
Zero has 0 limbs.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits()
.
§Examples
use malachite_base::num::arithmetic::traits::{Pow, PowerOf2};
use malachite_base::num::basic::integers::PrimitiveInt;
use malachite_base::num::basic::traits::{One, Zero};
use malachite_nz::integer::Integer;
use malachite_nz::platform::Limb;
if Limb::WIDTH == u32::WIDTH {
assert_eq!(Integer::ZERO.twos_complement_limb_count(), 0);
assert_eq!(Integer::from(123u32).twos_complement_limb_count(), 1);
assert_eq!(Integer::from(10u32).pow(12).twos_complement_limb_count(), 2);
let n = Integer::power_of_2(Limb::WIDTH - 1);
assert_eq!((&n - Integer::ONE).twos_complement_limb_count(), 1);
assert_eq!(n.twos_complement_limb_count(), 2);
assert_eq!((&n + Integer::ONE).twos_complement_limb_count(), 2);
assert_eq!((-(&n - Integer::ONE)).twos_complement_limb_count(), 1);
assert_eq!((-&n).twos_complement_limb_count(), 1);
assert_eq!((-(&n + Integer::ONE)).twos_complement_limb_count(), 2);
}
Sourcepub fn checked_count_ones(&self) -> Option<u64>
pub fn checked_count_ones(&self) -> Option<u64>
Counts the number of ones in the binary expansion of an Integer
. If the Integer
is
negative, then the number of ones is infinite, so None
is returned.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits()
.
§Examples
use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;
assert_eq!(Integer::ZERO.checked_count_ones(), Some(0));
// 105 = 1101001b
assert_eq!(Integer::from(105).checked_count_ones(), Some(4));
assert_eq!(Integer::from(-105).checked_count_ones(), None);
// 10^12 = 1110100011010100101001010001000000000000b
assert_eq!(Integer::from(10u32).pow(12).checked_count_ones(), Some(13));
Sourcepub fn checked_count_zeros(&self) -> Option<u64>
pub fn checked_count_zeros(&self) -> Option<u64>
Counts the number of zeros in the binary expansion of an Integer
. If the Integer
is
non-negative, then the number of zeros is infinite, so None
is returned.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits()
.
§Examples
use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;
assert_eq!(Integer::ZERO.checked_count_zeros(), None);
// -105 = 10010111 in two's complement
assert_eq!(Integer::from(-105).checked_count_zeros(), Some(3));
assert_eq!(Integer::from(105).checked_count_zeros(), None);
// -10^12 = 10001011100101011010110101111000000000000 in two's complement
assert_eq!(
(-Integer::from(10u32).pow(12)).checked_count_zeros(),
Some(24)
);
Sourcepub fn trailing_zeros(&self) -> Option<u64>
pub fn trailing_zeros(&self) -> Option<u64>
Returns the number of trailing zeros in the binary expansion of an Integer
(equivalently, the multiplicity of 2 in its prime factorization), or None
is the
Integer
is 0.
§Worst-case complexity
$T(n) = O(n)$
$M(n) = O(1)$
where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits()
.
§Examples
use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;
assert_eq!(Integer::ZERO.trailing_zeros(), None);
assert_eq!(Integer::from(3).trailing_zeros(), Some(0));
assert_eq!(Integer::from(-72).trailing_zeros(), Some(3));
assert_eq!(Integer::from(100).trailing_zeros(), Some(2));
assert_eq!((-Integer::from(10u32).pow(12)).trailing_zeros(), Some(12));
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ZERO
impl RefUnwindSafe for ZERO
impl Send for ZERO
impl Sync for ZERO
impl Unpin for ZERO
impl UnwindSafe for ZERO
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more