Struct ONE

Source
pub struct ONE { /* private fields */ }

Methods from Deref<Target = Integer>§

Source

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);
Source

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]
    );
}
Source

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 BigIntegers 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]
    );
}
Source

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]
    );
}
Source

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);
}
Source

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));
Source

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)
);
Source

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§

Source§

impl Deref for ONE

Source§

type Target = Integer

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Integer

Dereferences the value.
Source§

impl LazyStatic for ONE

Auto Trait Implementations§

§

impl Freeze for ONE

§

impl RefUnwindSafe for ONE

§

impl Send for ONE

§

impl Sync for ONE

§

impl Unpin for ONE

§

impl UnwindSafe for ONE

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T, U> ExactFrom<T> for U
where U: TryFrom<T>,

Source§

fn exact_from(value: T) -> U

Source§

impl<T, U> ExactInto<U> for T
where U: ExactFrom<T>,

Source§

fn exact_into(self) -> U

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T, U> OverflowingInto<U> for T
where U: OverflowingFrom<T>,

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> RoundingInto<U> for T
where U: RoundingFrom<T>,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> SaturatingInto<U> for T
where U: SaturatingFrom<T>,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T, U> WrappingInto<U> for T
where U: WrappingFrom<T>,

Source§

fn wrapping_into(self) -> U

Source§

impl<T> ErasedDestructor for T
where T: 'static,