pub struct Integer { /* private fields */ }
Expand description

An integer.

Any Integer whose absolute value is small enough to fit into a Limb is represented inline. Only integers outside this range incur the costs of heap-allocation.

Implementations

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

Mutates the absolute value of an Integer using a provided closure, and then returns whatever the closure returns.

This function is similar to the unsigned_abs_ref function, which returns a reference to the absolute value. A function that returns a mutable reference would be too dangerous, as it could leave the Integer in an invalid state (specifically, with a negative sign but a zero absolute value). So rather than returning a mutable reference, this function allows mutation of the absolute value using a closure. After the closure executes, this function ensures that the Integer remains valid.

There is only constant time and memory overhead on top of the time and memory used by the closure.

Examples
use malachite_base::num::arithmetic::traits::DivAssignMod;
use malachite_base::num::basic::traits::Two;
use malachite_nz::integer::Integer;
use malachite_nz::natural::Natural;

let mut n = Integer::from(-123);
let remainder = n.mutate_unsigned_abs(|x| x.div_assign_mod(Natural::TWO));
assert_eq!(n, -61);
assert_eq!(remainder, 1);

let mut n = Integer::from(-123);
n.mutate_unsigned_abs(|x| *x >>= 10);
assert_eq!(n, 0);

Converts a sign and a Natural to an Integer, taking the Natural by value. The Natural becomes the Integer’s absolute value, and the sign indicates whether the Integer should be non-negative. If the Natural is zero, then the Integer will be non-negative regardless of the sign.

Worst-case complexity

Constant time and additional memory.

Examples
use malachite_nz::integer::Integer;
use malachite_nz::natural::Natural;

assert_eq!(Integer::from_sign_and_abs(true, Natural::from(123u32)), 123);
assert_eq!(Integer::from_sign_and_abs(false, Natural::from(123u32)), -123);

Converts a sign and an Natural to an Integer, taking the Natural by reference. The Natural becomes the Integer’s absolute value, and the sign indicates whether the Integer should be non-negative. If the Natural is zero, then the Integer will be non-negative regardless of the sign.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, $n$ is abs.significant_bits().

Examples
use malachite_nz::integer::Integer;
use malachite_nz::natural::Natural;

assert_eq!(Integer::from_sign_and_abs_ref(true, &Natural::from(123u32)), 123);
assert_eq!(Integer::from_sign_and_abs_ref(false, &Natural::from(123u32)), -123);

Converts a slice of limbs to an Integer, in ascending order, so that less significant limbs have lower indices in the input slice.

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 non-negative, and if the bit is one it is negative. If the slice is empty, zero is returned.

This function borrows a slice. If taking ownership of a Vec is possible instead, from_owned_twos_complement_limbs_asc is more efficient.

This function is more efficient than from_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 xs.len().

Examples
extern crate malachite_base;

use malachite_base::num::basic::integers::PrimitiveInt;
use malachite_nz::integer::Integer;
use malachite_nz::platform::Limb;

if Limb::WIDTH == u32::WIDTH {
    assert_eq!(Integer::from_twos_complement_limbs_asc(&[]), 0);
    assert_eq!(Integer::from_twos_complement_limbs_asc(&[123]), 123);
    assert_eq!(Integer::from_twos_complement_limbs_asc(&[4294967173]), -123);
    // 10^12 = 232 * 2^32 + 3567587328
    assert_eq!(
        Integer::from_twos_complement_limbs_asc(&[3567587328, 232]),
        1000000000000u64
    );
    assert_eq!(
        Integer::from_twos_complement_limbs_asc(&[727379968, 4294967063]),
        -1000000000000i64
    );
}

Converts a slice of limbs to an Integer, in descending order, so that less significant limbs have higher indices in the input slice.

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 non-negative, and if the bit is one it is negative. If the slice is empty, zero is returned.

This function borrows a slice. If taking ownership of a Vec is possible instead, from_owned_twos_complement_limbs_desc is more efficient.

This function is less efficient than from_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 xs.len().

Examples
extern crate malachite_base;

use malachite_base::num::basic::integers::PrimitiveInt;
use malachite_nz::integer::Integer;
use malachite_nz::platform::Limb;

if Limb::WIDTH == u32::WIDTH {
    assert_eq!(Integer::from_twos_complement_limbs_desc(&[]), 0);
    assert_eq!(Integer::from_twos_complement_limbs_desc(&[123]), 123);
    assert_eq!(Integer::from_twos_complement_limbs_desc(&[4294967173]), -123);
    // 10^12 = 232 * 2^32 + 3567587328
    assert_eq!(
        Integer::from_twos_complement_limbs_desc(&[232, 3567587328]),
        1000000000000u64
    );
    assert_eq!(
        Integer::from_twos_complement_limbs_desc(&[4294967063, 727379968]),
        -1000000000000i64
    );
}

Converts a slice of limbs to an Integer, in ascending order, so that less significant limbs have lower indices in the input slice.

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 non-negative, and if the bit is one it is negative. If the slice is empty, zero is returned.

This function takes ownership of a Vec. If it’s necessary to borrow a slice instead, use from_twos_complement_limbs_asc

This function is more efficient than from_owned_twos_complement_limbs_desc.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is xs.len().

Examples
extern crate malachite_base;

use malachite_base::num::basic::integers::PrimitiveInt;
use malachite_nz::integer::Integer;
use malachite_nz::platform::Limb;

if Limb::WIDTH == u32::WIDTH {
    assert_eq!(Integer::from_owned_twos_complement_limbs_asc(vec![]), 0);
    assert_eq!(Integer::from_owned_twos_complement_limbs_asc(vec![123]), 123);
    assert_eq!(Integer::from_owned_twos_complement_limbs_asc(vec![4294967173]), -123);
    // 10^12 = 232 * 2^32 + 3567587328
    assert_eq!(
        Integer::from_owned_twos_complement_limbs_asc(vec![3567587328, 232]),
        1000000000000i64
    );
    assert_eq!(
        Integer::from_owned_twos_complement_limbs_asc(vec![727379968, 4294967063]),
        -1000000000000i64
    );
}

Converts a slice of limbs to an Integer, in descending order, so that less significant limbs have higher indices in the input slice.

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 non-negative, and if the bit is one it is negative. If the slice is empty, zero is returned.

This function takes ownership of a Vec. If it’s necessary to borrow a slice instead, use from_twos_complement_limbs_desc.

This function is less efficient than from_owned_twos_complement_limbs_asc.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is xs.len().

Examples
extern crate malachite_base;

use malachite_base::num::basic::integers::PrimitiveInt;
use malachite_nz::integer::Integer;
use malachite_nz::platform::Limb;

if Limb::WIDTH == u32::WIDTH {
    assert_eq!(Integer::from_owned_twos_complement_limbs_desc(vec![]), 0);
    assert_eq!(Integer::from_owned_twos_complement_limbs_desc(vec![123]), 123);
    assert_eq!(Integer::from_owned_twos_complement_limbs_desc(vec![4294967173]), -123);
    // 10^12 = 232 * 2^32 + 3567587328
    assert_eq!(
        Integer::from_owned_twos_complement_limbs_desc(vec![232, 3567587328]),
        1000000000000i64
    );
    assert_eq!(
        Integer::from_owned_twos_complement_limbs_desc(vec![4294967063, 727379968]),
        -1000000000000i64
    );
}

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
extern crate malachite_base;

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

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
extern crate malachite_base;

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

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 takes ownership of self. If it’s necessary to borrow self instead, use to_twos_complement_limbs_asc.

This function is more efficient than into_twos_complement_limbs_desc.

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
extern crate malachite_base;

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.into_twos_complement_limbs_asc().is_empty());
    assert_eq!(Integer::from(123).into_twos_complement_limbs_asc(), &[123]);
    assert_eq!(Integer::from(-123).into_twos_complement_limbs_asc(), &[4294967173]);
    // 10^12 = 232 * 2^32 + 3567587328
    assert_eq!(
        Integer::from(10u32).pow(12).into_twos_complement_limbs_asc(),
        &[3567587328, 232]
    );
    assert_eq!(
        (-Integer::from(10u32).pow(12)).into_twos_complement_limbs_asc(),
        &[727379968, 4294967063]
    );
}

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 takes ownership of self. If it’s necessary to borrow self instead, use to_twos_complement_limbs_desc.

This function is less efficient than into_twos_complement_limbs_asc.

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
extern crate malachite_base;

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.into_twos_complement_limbs_desc().is_empty());
    assert_eq!(Integer::from(123).into_twos_complement_limbs_desc(), &[123]);
    assert_eq!(Integer::from(-123).into_twos_complement_limbs_desc(), &[4294967173]);
    // 10^12 = 232 * 2^32 + 3567587328
    assert_eq!(
        Integer::from(10u32).pow(12).into_twos_complement_limbs_desc(),
        &[232, 3567587328]
    );
    assert_eq!(
        (-Integer::from(10u32).pow(12)).into_twos_complement_limbs_desc(),
        &[4294967063, 727379968]
    );
}

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
extern crate itertools;
extern crate malachite_base;

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().rev().next().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]
    );
}

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
extern crate malachite_base;

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

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
extern crate malachite_base;

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

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
extern crate malachite_base;

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

Takes the absolute value of an Integer, taking the Integer by reference.

$$ f(x) = |x|. $$

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::Abs;
use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;

assert_eq!((&Integer::ZERO).abs(), 0);
assert_eq!((&Integer::from(123)).abs(), 123);
assert_eq!((&Integer::from(-123)).abs(), 123);

Takes the absolute value of an Integer, taking the Integer by value.

$$ f(x) = |x|. $$

Worst-case complexity

Constant time and additional memory.

Examples
use malachite_base::num::arithmetic::traits::Abs;
use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;

assert_eq!(Integer::ZERO.abs(), 0);
assert_eq!(Integer::from(123).abs(), 123);
assert_eq!(Integer::from(-123).abs(), 123);

Replaces an Integer with its absolute value.

$$ x \gets |x|. $$

Worst-case complexity

Constant time and additional memory.

Examples
use malachite_base::num::arithmetic::traits::AbsAssign;
use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;

let mut x = Integer::ZERO;
x.abs_assign();
assert_eq!(x, 0);

let mut x = Integer::from(123);
x.abs_assign();
assert_eq!(x, 123);

let mut x = Integer::from(-123);
x.abs_assign();
assert_eq!(x, 123);

Adds two Integers, taking both by reference.

$$ f(x, y) = x + y. $$

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.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 + &Integer::from(123), 123);
assert_eq!(&Integer::from(-123) + &Integer::ZERO, -123);
assert_eq!(&Integer::from(-123) + &Integer::from(456), 333);
assert_eq!(
    &-Integer::from(10u32).pow(12) + &(Integer::from(10u32).pow(12) << 1),
    1000000000000u64
);
The resulting type after applying the + operator.

Adds two Integers, taking the first by reference and the second by value.

$$ f(x, y) = x + y. $$

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.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 + &Integer::from(123), 123);
assert_eq!(Integer::from(-123) + &Integer::ZERO, -123);
assert_eq!(Integer::from(-123) + &Integer::from(456), 333);
assert_eq!(
    -Integer::from(10u32).pow(12) + &(Integer::from(10u32).pow(12) << 1),
    1000000000000u64
);
The resulting type after applying the + operator.

Adds two Integers, taking the first by value and the second by reference.

$$ f(x, y) = x + y. $$

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.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 + Integer::from(123), 123);
assert_eq!(&Integer::from(-123) + Integer::ZERO, -123);
assert_eq!(&Integer::from(-123) + Integer::from(456), 333);
assert_eq!(
    &-Integer::from(10u32).pow(12) + (Integer::from(10u32).pow(12) << 1),
    1000000000000u64
);
The resulting type after applying the + operator.

Adds two Integers, taking both by value.

$$ f(x, y) = x + y. $$

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$ (only if the underlying Vec needs to reallocate)

where $T$ is time, $M$ is additional memory, and $n$ is min(self.significant_bits(), other.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 + Integer::from(123), 123);
assert_eq!(Integer::from(-123) + Integer::ZERO, -123);
assert_eq!(Integer::from(-123) + Integer::from(456), 333);
assert_eq!(
    -Integer::from(10u32).pow(12) + (Integer::from(10u32).pow(12) << 1),
    1000000000000u64
);
The resulting type after applying the + operator.

Adds an Integer to an Integer in place, taking the Integer on the right-hand side by reference.

$$ x \gets x + y. $$

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;

let mut x = Integer::ZERO;
x += &(-Integer::from(10u32).pow(12));
x += &(Integer::from(10u32).pow(12) * Integer::from(2u32));
x += &(-Integer::from(10u32).pow(12) * Integer::from(3u32));
x += &(Integer::from(10u32).pow(12) * Integer::from(4u32));
assert_eq!(x, 2000000000000u64);

Adds an Integer to an Integer in place, taking the Integer on the right-hand side by value.

$$ x \gets x + y. $$

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$ (only if the underlying Vec needs to reallocate)

where $T$ is time, $M$ is additional memory, and $n$ is min(self.significant_bits(), other.significant_bits()).

Examples
use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;

let mut x = Integer::ZERO;
x += -Integer::from(10u32).pow(12);
x += Integer::from(10u32).pow(12) * Integer::from(2u32);
x += -Integer::from(10u32).pow(12) * Integer::from(3u32);
x += Integer::from(10u32).pow(12) * Integer::from(4u32);
assert_eq!(x, 2000000000000u64);

Adds an Integer and the product of two other Integers, taking all three by reference.

$f(x, y, z) = x + yz$.

Worst-case complexity

$T(n, m) = O(m + n \log n \log\log n)$

$M(n, m) = O(m + n \log n)$

where $T$ is time, $M$ is additional memory, $n$ is max(y.significant_bits(), z.significant_bits()), and $m$ is self.significant_bits().

Examples
use malachite_base::num::arithmetic::traits::{AddMul, Pow};
use malachite_nz::integer::Integer;

assert_eq!(
    (&Integer::from(10u32)).add_mul(&Integer::from(3u32), &Integer::from(4u32)),
    22
);
assert_eq!(
    (&-Integer::from(10u32).pow(12))
            .add_mul(&Integer::from(0x10000), &-Integer::from(10u32).pow(12)),
    -65537000000000000i64
);

Adds an Integer and the product of two other Integers, taking the first by value and the second and third by reference.

$f(x, y, z) = x + yz$.

Worst-case complexity

$T(n, m) = O(m + n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, $n$ is max(y.significant_bits(), z.significant_bits()), and $m$ is self.significant_bits().

Examples
use malachite_base::num::arithmetic::traits::{AddMul, Pow};
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(10u32).add_mul(&Integer::from(3u32), &Integer::from(4u32)), 22);
assert_eq!(
    (-Integer::from(10u32).pow(12))
            .add_mul(&Integer::from(0x10000), &-Integer::from(10u32).pow(12)),
    -65537000000000000i64
);

Adds an Integer and the product of two other Integers, taking the first and third by value and the second by reference.

$f(x, y, z) = x + yz$.

Worst-case complexity

$T(n, m) = O(m + n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, $n$ is max(y.significant_bits(), z.significant_bits()), and $m$ is self.significant_bits().

Examples
use malachite_base::num::arithmetic::traits::{AddMul, Pow};
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(10u32).add_mul(&Integer::from(3u32), Integer::from(4u32)), 22);
assert_eq!(
    (-Integer::from(10u32).pow(12))
            .add_mul(&Integer::from(0x10000), -Integer::from(10u32).pow(12)),
    -65537000000000000i64
);

Adds an Integer and the product of two other Integers, taking the first two by value and the third by reference.

$f(x, y, z) = x + yz$.

Worst-case complexity

$T(n, m) = O(m + n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, $n$ is max(y.significant_bits(), z.significant_bits()), and $m$ is self.significant_bits().

Examples
use malachite_base::num::arithmetic::traits::{AddMul, Pow};
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(10u32).add_mul(Integer::from(3u32), &Integer::from(4u32)), 22);
assert_eq!(
    (-Integer::from(10u32).pow(12))
            .add_mul(Integer::from(0x10000), &-Integer::from(10u32).pow(12)),
    -65537000000000000i64
);

Adds an Integer and the product of two other Integers, taking all three by value.

$f(x, y, z) = x + yz$.

Worst-case complexity

$T(n, m) = O(m + n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, $n$ is max(y.significant_bits(), z.significant_bits()), and $m$ is self.significant_bits().

Examples
use malachite_base::num::arithmetic::traits::{AddMul, Pow};
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(10u32).add_mul(Integer::from(3u32), Integer::from(4u32)), 22);
assert_eq!(
    (-Integer::from(10u32).pow(12))
            .add_mul(Integer::from(0x10000), -Integer::from(10u32).pow(12)),
    -65537000000000000i64
);

Adds the product of two other Integers to an Integer in place, taking both Integers on the right-hand side by reference.

$x \gets x + yz$.

Worst-case complexity

$T(n, m) = O(m + n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, $n$ is max(y.significant_bits(), z.significant_bits()), and $m$ is self.significant_bits().

Examples
use malachite_base::num::arithmetic::traits::{AddMulAssign, Pow};
use malachite_nz::integer::Integer;

let mut x = Integer::from(10u32);
x.add_mul_assign(&Integer::from(3u32), &Integer::from(4u32));
assert_eq!(x, 22);

let mut x = -Integer::from(10u32).pow(12);
x.add_mul_assign(&Integer::from(0x10000), &-Integer::from(10u32).pow(12));
assert_eq!(x, -65537000000000000i64);

Adds the product of two other Integers to an Integer in place, taking the first Integer on the right-hand side by reference and the second by value.

$x \gets x + yz$.

Worst-case complexity

$T(n, m) = O(m + n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, $n$ is max(y.significant_bits(), z.significant_bits()), and $m$ is self.significant_bits().

Examples
use malachite_base::num::arithmetic::traits::{AddMulAssign, Pow};
use malachite_nz::integer::Integer;

let mut x = Integer::from(10u32);
x.add_mul_assign(&Integer::from(3u32), Integer::from(4u32));
assert_eq!(x, 22);

let mut x = -Integer::from(10u32).pow(12);
x.add_mul_assign(&Integer::from(0x10000), -Integer::from(10u32).pow(12));
assert_eq!(x, -65537000000000000i64);

Adds the product of two other Integers to an Integer in place, taking the first Integer on the right-hand side by value and the second by reference.

$x \gets x + yz$.

Worst-case complexity

$T(n, m) = O(m + n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, $n$ is max(y.significant_bits(), z.significant_bits()), and $m$ is self.significant_bits().

Examples
use malachite_base::num::arithmetic::traits::{AddMulAssign, Pow};
use malachite_nz::integer::Integer;

let mut x = Integer::from(10u32);
x.add_mul_assign(Integer::from(3u32), &Integer::from(4u32));
assert_eq!(x, 22);

let mut x = -Integer::from(10u32).pow(12);
x.add_mul_assign(Integer::from(0x10000), &-Integer::from(10u32).pow(12));
assert_eq!(x, -65537000000000000i64);

Adds the product of two other Integers to an Integer in place, taking both Integers on the right-hand side by value.

$x \gets x + yz$.

Worst-case complexity

$T(n, m) = O(m + n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, $n$ is max(y.significant_bits(), z.significant_bits()), and $m$ is self.significant_bits().

Examples
use malachite_base::num::arithmetic::traits::{AddMulAssign, Pow};
use malachite_nz::integer::Integer;

let mut x = Integer::from(10u32);
x.add_mul_assign(Integer::from(3u32), Integer::from(4u32));
assert_eq!(x, 22);

let mut x = -Integer::from(10u32).pow(12);
x.add_mul_assign(Integer::from(0x10000), -Integer::from(10u32).pow(12));
assert_eq!(x, -65537000000000000i64);

Converts an Integer to a binary String.

Using the # format flag prepends "0b" to the string.

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
extern crate malachite_base;

use malachite_base::num::basic::traits::Zero;
use malachite_base::strings::ToBinaryString;
use malachite_nz::integer::Integer;
use std::str::FromStr;

assert_eq!(Integer::ZERO.to_binary_string(), "0");
assert_eq!(Integer::from(123).to_binary_string(), "1111011");
assert_eq!(
    Integer::from_str("1000000000000")
        .unwrap()
        .to_binary_string(),
    "1110100011010100101001010001000000000000"
);
assert_eq!(format!("{:011b}", Integer::from(123)), "00001111011");
assert_eq!(Integer::from(-123).to_binary_string(), "-1111011");
assert_eq!(
    Integer::from_str("-1000000000000")
        .unwrap()
        .to_binary_string(),
    "-1110100011010100101001010001000000000000"
);
assert_eq!(format!("{:011b}", Integer::from(-123)), "-0001111011");

assert_eq!(format!("{:#b}", Integer::ZERO), "0b0");
assert_eq!(format!("{:#b}", Integer::from(123)), "0b1111011");
assert_eq!(
    format!("{:#b}", Integer::from_str("1000000000000").unwrap()),
    "0b1110100011010100101001010001000000000000"
);
assert_eq!(format!("{:#011b}", Integer::from(123)), "0b001111011");
assert_eq!(format!("{:#b}", Integer::from(-123)), "-0b1111011");
assert_eq!(
    format!("{:#b}", Integer::from_str("-1000000000000").unwrap()),
    "-0b1110100011010100101001010001000000000000"
);
assert_eq!(format!("{:#011b}", Integer::from(-123)), "-0b01111011");

Provides functions for accessing and modifying the $i$th bit of a Integer, or the coefficient of $2^i$ in its two’s complement binary expansion.

Examples

extern crate malachite_base;

use malachite_base::num::logic::traits::BitAccess;
use malachite_base::num::basic::traits::{NegativeOne, Zero};
use malachite_nz::integer::Integer;

let mut x = Integer::ZERO;
x.assign_bit(2, true);
x.assign_bit(5, true);
x.assign_bit(6, true);
assert_eq!(x, 100);
x.assign_bit(2, false);
x.assign_bit(5, false);
x.assign_bit(6, false);
assert_eq!(x, 0);

let mut x = Integer::from(-0x100);
x.assign_bit(2, true);
x.assign_bit(5, true);
x.assign_bit(6, true);
assert_eq!(x, -156);
x.assign_bit(2, false);
x.assign_bit(5, false);
x.assign_bit(6, false);
assert_eq!(x, -256);

let mut x = Integer::ZERO;
x.flip_bit(10);
assert_eq!(x, 1024);
x.flip_bit(10);
assert_eq!(x, 0);

let mut x = Integer::NEGATIVE_ONE;
x.flip_bit(10);
assert_eq!(x, -1025);
x.flip_bit(10);
assert_eq!(x, -1);

Determines whether the $i$th bit of an Integer, or the coefficient of $2^i$ in its two’s complement binary expansion, is 0 or 1.

false means 0 and true means 1. Getting bits beyond the Integer’s width is allowed; those bits are false if the Integer is non-negative and true if it is negative.

If $n \geq 0$, let $$ n = \sum_{i=0}^\infty 2^{b_i}; $$ but if $n < 0$, let $$ -n - 1 = \sum_{i=0}^\infty 2^{1 - b_i}, $$ where for all $i$, $b_i\in \{0, 1\}$.

$f(n, i) = (b_i = 1)$.

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
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::logic::traits::BitAccess;
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(123).get_bit(2), false);
assert_eq!(Integer::from(123).get_bit(3), true);
assert_eq!(Integer::from(123).get_bit(100), false);
assert_eq!(Integer::from(-123).get_bit(0), true);
assert_eq!(Integer::from(-123).get_bit(1), false);
assert_eq!(Integer::from(-123).get_bit(100), true);
assert_eq!(Integer::from(10u32).pow(12).get_bit(12), true);
assert_eq!(Integer::from(10u32).pow(12).get_bit(100), false);
assert_eq!((-Integer::from(10u32).pow(12)).get_bit(12), true);
assert_eq!((-Integer::from(10u32).pow(12)).get_bit(100), true);

Sets the $i$th bit of an Integer, or the coefficient of $2^i$ in its two’s complement binary expansion, to 1.

If $n \geq 0$, let $$ n = \sum_{i=0}^\infty 2^{b_i}; $$ but if $n < 0$, let $$ -n - 1 = \sum_{i=0}^\infty 2^{1 - b_i}, $$ where for all $i$, $b_i\in \{0, 1\}$. $$ n \gets \begin{cases} n + 2^j & \text{if} \quad b_j = 0, \\ n & \text{otherwise}. \end{cases} $$

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is index.

Examples
extern crate malachite_base;

use malachite_base::num::logic::traits::BitAccess;
use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;

let mut x = Integer::ZERO;
x.set_bit(2);
x.set_bit(5);
x.set_bit(6);
assert_eq!(x, 100);

let mut x = Integer::from(-0x100);
x.set_bit(2);
x.set_bit(5);
x.set_bit(6);
assert_eq!(x, -156);

Sets the $i$th bit of an Integer, or the coefficient of $2^i$ in its binary expansion, to 0.

If $n \geq 0$, let $$ n = \sum_{i=0}^\infty 2^{b_i}; $$ but if $n < 0$, let $$ -n - 1 = \sum_{i=0}^\infty 2^{1 - b_i}, $$ where for all $i$, $b_i\in \{0, 1\}$. $$ n \gets \begin{cases} n - 2^j & \text{if} \quad b_j = 1, \\ n & \text{otherwise}. \end{cases} $$

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is index.

Examples
extern crate malachite_base;

use malachite_base::num::logic::traits::BitAccess;
use malachite_nz::integer::Integer;

let mut x = Integer::from(0x7f);
x.clear_bit(0);
x.clear_bit(1);
x.clear_bit(3);
x.clear_bit(4);
assert_eq!(x, 100);

let mut x = Integer::from(-156);
x.clear_bit(2);
x.clear_bit(5);
x.clear_bit(6);
assert_eq!(x, -256);
Sets the bit at index to whichever value bit is. Read more
Sets the bit at index to the opposite of its original value. Read more

Takes the bitwise and of two Integers, taking both by reference.

$$ f(x, y) = x \wedge y. $$

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::basic::traits::One;
use malachite_nz::integer::Integer;
use std::str::FromStr;

assert_eq!(&Integer::from(-123) & &Integer::from(-456), -512);
assert_eq!(
    &-Integer::from(10u32).pow(12) & &-(Integer::from(10u32).pow(12) + Integer::ONE),
    -1000000004096i64
);
The resulting type after applying the & operator.

Takes the bitwise and of two Integers, taking the first by value and the second by reference.

$$ f(x, y) = x \wedge y. $$

Worst-case complexity

$T(n) = O(n)$

$M(m) = O(m)$

where $T$ is time, $M$ is additional memory, $n$ is max(self.significant_bits(), other.significant_bits()), and $m$ is other.significant_bits().

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::basic::traits::One;
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(-123) & &Integer::from(-456), -512);
assert_eq!(
    -Integer::from(10u32).pow(12) & &-(Integer::from(10u32).pow(12) + Integer::ONE),
    -1000000004096i64
);
The resulting type after applying the & operator.

Takes the bitwise and of two Integers, taking the first by reference and the seocnd by value.

$$ f(x, y) = x \wedge y. $$

Worst-case complexity

$T(n) = O(n)$

$M(m) = O(m)$

where $T$ is time, $M$ is additional memory, $n$ is max(self.significant_bits(), other.significant_bits()), and $m$ is self.significant_bits().

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::basic::traits::One;
use malachite_nz::integer::Integer;
use std::str::FromStr;

assert_eq!(&Integer::from(-123) & Integer::from(-456), -512);
assert_eq!(
    &-Integer::from(10u32).pow(12) & -(Integer::from(10u32).pow(12) + Integer::ONE),
    -1000000004096i64
);
The resulting type after applying the & operator.

Takes the bitwise and of two Integers, taking both by value.

$$ f(x, y) = x \wedge y. $$

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::basic::traits::One;
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(-123) & Integer::from(-456), -512);
assert_eq!(
    -Integer::from(10u32).pow(12) & -(Integer::from(10u32).pow(12) + Integer::ONE),
    -1000000004096i64
);
The resulting type after applying the & operator.

Bitwise-ands an Integer with another Integer in place, taking the Integer on the right-hand side by reference.

$$ x \gets x \wedge y. $$

Worst-case complexity

$T(n) = O(n)$

$M(m) = O(m)$

where $T$ is time, $M$ is additional memory, $n$ is max(self.significant_bits(), other.significant_bits()), and $m$ is other.significant_bits().

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::NegativeOne;
use malachite_nz::integer::Integer;

let mut x = Integer::NEGATIVE_ONE;
x &= &Integer::from(0x70ffffff);
x &= &Integer::from(0x7ff0_ffff);
x &= &Integer::from(0x7ffff0ff);
x &= &Integer::from(0x7ffffff0);
assert_eq!(x, 0x70f0f0f0);

Bitwise-ands an Integer with another Integer in place, taking the Integer on the right-hand side by value.

$$ x \gets x \wedge y. $$

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::NegativeOne;
use malachite_nz::integer::Integer;

let mut x = Integer::NEGATIVE_ONE;
x &= Integer::from(0x70ffffff);
x &= Integer::from(0x7ff0_ffff);
x &= Integer::from(0x7ffff0ff);
x &= Integer::from(0x7ffffff0);
assert_eq!(x, 0x70f0f0f0);

Extracts a block of adjacent two’s complement bits from an Integer, taking the Integer by reference.

The first index is start and last index is end - 1.

Let $n$ be self, and let $p$ and $q$ be start and end, respectively.

If $n \geq 0$, let $$ n = \sum_{i=0}^\infty 2^{b_i}; $$ but if $n < 0$, let $$ -n - 1 = \sum_{i=0}^\infty 2^{1 - b_i}, $$ where for all $i$, $b_i\in \{0, 1\}$. Then $$ f(n, p, q) = \sum_{i=p}^{q-1} 2^{b_{i-p}}. $$

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), end).

Panics

Panics if start > end.

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::Zero;
use malachite_base::num::logic::traits::BitBlockAccess;
use malachite_nz::integer::Integer;
use malachite_nz::natural::Natural;
use std::str::FromStr;

assert_eq!(
    (-Natural::from(0xabcdef0112345678u64)).get_bits(16, 48),
    Natural::from(0x10feedcbu32)
);
assert_eq!(
    Integer::from(0xabcdef0112345678u64).get_bits(4, 16),
    Natural::from(0x567u32)
);
assert_eq!(
    (-Natural::from(0xabcdef0112345678u64)).get_bits(0, 100),
    Natural::from_str("1267650600215849587758112418184").unwrap()
);
assert_eq!(Integer::from(0xabcdef0112345678u64).get_bits(10, 10), Natural::ZERO);

Extracts a block of adjacent two’s complement bits from an Integer, taking the Integer by value.

The first index is start and last index is end - 1.

Let $n$ be self, and let $p$ and $q$ be start and end, respectively.

If $n \geq 0$, let $$ n = \sum_{i=0}^\infty 2^{b_i}; $$ but if $n < 0$, let $$ -n - 1 = \sum_{i=0}^\infty 2^{1 - b_i}, $$ where for all $i$, $b_i\in \{0, 1\}$. Then $$ f(n, p, q) = \sum_{i=p}^{q-1} 2^{b_{i-p}}. $$

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), end).

Panics

Panics if start > end.

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::Zero;
use malachite_base::num::logic::traits::BitBlockAccess;
use malachite_nz::integer::Integer;
use malachite_nz::natural::Natural;
use std::str::FromStr;

assert_eq!(
    (-Natural::from(0xabcdef0112345678u64)).get_bits_owned(16, 48),
    Natural::from(0x10feedcbu32)
);
assert_eq!(
    Integer::from(0xabcdef0112345678u64).get_bits_owned(4, 16),
    Natural::from(0x567u32)
);
assert_eq!(
    (-Natural::from(0xabcdef0112345678u64)).get_bits_owned(0, 100),
    Natural::from_str("1267650600215849587758112418184").unwrap()
);
assert_eq!(Integer::from(0xabcdef0112345678u64).get_bits_owned(10, 10), Natural::ZERO);

Replaces a block of adjacent two’s complement bits in an Integer with other bits.

The least-significant end - start bits of bits are assigned to bits start through end - 1, inclusive, of self.

Let $n$ be self and let $m$ be bits, and let $p$ and $q$ be start and end, respectively.

Let $$ m = \sum_{i=0}^k 2^{d_i}, $$ where for all $i$, $d_i\in \{0, 1\}$.

If $n \geq 0$, let $$ n = \sum_{i=0}^\infty 2^{b_i}; $$ but if $n < 0$, let $$ -n - 1 = \sum_{i=0}^\infty 2^{1 - b_i}, $$ where for all $i$, $b_i\in \{0, 1\}$. Then $$ n \gets \sum_{i=0}^\infty 2^{c_i}, $$ where $$ \{c_0, c_1, c_2, \ldots \} = \{b_0, b_1, b_2, \ldots, b_{p-1}, d_0, d_1, \ldots, d_{p-q-1}, b_q, b_{q+1}, \ldots \}. $$

Worst-case complexity

$T(n) = O(n)$

$M(m) = O(m)$

where $T$ is time, $M$ is additional memory, $n$ is max(self.significant_bits(), end), and $m$ is self.significant_bits().

Panics

Panics if start > end.

Examples
extern crate malachite_base;

use malachite_base::num::logic::traits::BitBlockAccess;
use malachite_nz::integer::Integer;
use malachite_nz::natural::Natural;

let mut n = Integer::from(123);
n.assign_bits(5, 7, &Natural::from(456u32));
assert_eq!(n.to_string(), "27");

let mut n = Integer::from(-123);
n.assign_bits(64, 128, &Natural::from(456u32));
assert_eq!(n.to_string(), "-340282366920938455033212565746503123067");

let mut n = Integer::from(-123);
n.assign_bits(80, 100, &Natural::from(456u32));
assert_eq!(n.to_string(), "-1267098121128665515963862483067");

Returns a Vec containing the twos-complement bits of an Integer in ascending order: least- to most-significant.

The most significant bit indicates the sign; if the bit is false, the Integer is positive, and if the bit is true it is negative. There are no trailing false bits if the Integer is positive or trailing true bits if the Integer is negative, except as necessary to include the correct sign bit. Zero is a special case: it contains no bits.

This function is more efficient than to_bits_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
extern crate malachite_base;

use malachite_base::num::logic::traits::BitConvertible;
use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;

assert!(Integer::ZERO.to_bits_asc().is_empty());
// 105 = 01101001b, with a leading false bit to indicate sign
assert_eq!(
    Integer::from(105).to_bits_asc(),
    &[true, false, false, true, false, true, true, false]
);
// -105 = 10010111 in two's complement, with a leading true bit to indicate sign
assert_eq!(
    Integer::from(-105).to_bits_asc(),
    &[true, true, true, false, true, false, false, true]
);

Returns a Vec containing the twos-complement bits of an Integer in descending order: most- to least-significant.

The most significant bit indicates the sign; if the bit is false, the Integer is positive, and if the bit is true it is negative. There are no leading false bits if the Integer is positive or leading true bits if the Integer is negative, except as necessary to include the correct sign bit. Zero is a special case: it contains no bits.

This is similar to how BigIntegers in Java are represented.

This function is less efficient than to_bits_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
extern crate malachite_base;

use malachite_base::num::logic::traits::BitConvertible;
use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;

assert!(Integer::ZERO.to_bits_desc().is_empty());
// 105 = 01101001b, with a leading false bit to indicate sign
assert_eq!(
    Integer::from(105).to_bits_desc(),
    &[false, true, true, false, true, false, false, true]
);
// -105 = 10010111 in two's complement, with a leading true bit to indicate sign
assert_eq!(
    Integer::from(-105).to_bits_desc(),
    &[true, false, false, true, false, true, true, true]
);

Converts an iterator of twos-complement bits into an Integer. The bits should be in ascending order (least- to most-significant).

Let $k$ be bits.count(). If $k = 0$ or $b_{k-1}$ is false, then $$ f((b_i)_ {i=0}^{k-1}) = \sum_{i=0}^{k-1}2^i [b_i], $$ where braces denote the Iverson bracket, which converts a bit to 0 or 1.

If $b_{k-1}$ is true, then $$ f((b_i)_ {i=0}^{k-1}) = \left ( \sum_{i=0}^{k-1}2^i [b_i] \right ) - 2^k. $$

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is xs.count().

Examples
extern crate malachite_base;

use malachite_base::num::logic::traits::BitConvertible;
use malachite_nz::integer::Integer;
use std::iter::empty;

assert_eq!(Integer::from_bits_asc(empty()), 0);
// 105 = 1101001b
assert_eq!(
    Integer::from_bits_asc(
        [true, false, false, true, false, true, true, false].iter().cloned()
    ),
    105
);
// -105 = 10010111 in two's complement, with a leading true bit to indicate sign
assert_eq!(
    Integer::from_bits_asc(
        [true, true, true, false, true, false, false, true].iter().cloned()
    ),
    -105
);

Converts an iterator of twos-complement bits into an Integer. The bits should be in descending order (most- to least-significant).

If bits is empty or $b_0$ is false, then $$ f((b_i)_ {i=0}^{k-1}) = \sum_{i=0}^{k-1}2^{k-i-1} [b_i], $$ where braces denote the Iverson bracket, which converts a bit to 0 or 1.

If $b_0$ is true, then $$ f((b_i)_ {i=0}^{k-1}) = \left ( \sum_{i=0}^{k-1}2^{k-i-1} [b_i] \right ) - 2^k. $$

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is xs.count().

Examples
extern crate malachite_base;

use malachite_base::num::logic::traits::BitConvertible;
use malachite_nz::integer::Integer;
use std::iter::empty;

assert_eq!(Integer::from_bits_desc(empty()), 0);
// 105 = 1101001b
assert_eq!(
    Integer::from_bits_desc(
        [false, true, true, false, true, false, false, true].iter().cloned()
    ),
    105
);
// -105 = 10010111 in two's complement, with a leading true bit to indicate sign
assert_eq!(
    Integer::from_bits_desc(
        [true, false, false, true, false, true, true, true].iter().cloned()
    ),
    -105
);

Returns a double-ended iterator over the bits of an Integer.

The forward order is ascending, so that less significant bits appear first. There are no trailing false bits going forward, or leading falses going backward, except for possibly a most-significant sign-extension bit.

If it’s necessary to get a Vec of all the bits, consider using to_bits_asc or to_bits_desc instead.

Worst-case complexity

Constant time and additional memory.

Examples
extern crate itertools;
extern crate malachite_base;

use itertools::Itertools;
use malachite_base::num::basic::traits::Zero;
use malachite_base::num::logic::traits::BitIterable;
use malachite_nz::integer::Integer;

assert_eq!(Integer::ZERO.bits().next(), None);
// 105 = 01101001b, with a leading false bit to indicate sign
assert_eq!(
    Integer::from(105).bits().collect_vec(),
    &[true, false, false, true, false, true, true, false]
);
// -105 = 10010111 in two's complement, with a leading true bit to indicate sign
assert_eq!(
    Integer::from(-105).bits().collect_vec(),
    &[true, true, true, false, true, false, false, true]
);

assert_eq!(Integer::ZERO.bits().next_back(), None);
// 105 = 01101001b, with a leading false bit to indicate sign
assert_eq!(
    Integer::from(105).bits().rev().collect_vec(),
    &[false, true, true, false, true, false, false, true]
);
// -105 = 10010111 in two's complement, with a leading true bit to indicate sign
assert_eq!(
    Integer::from(-105).bits().rev().collect_vec(),
    &[true, false, false, true, false, true, true, true]
);

Takes the bitwise or of two Integers, taking both by reference.

$$ f(x, y) = x \vee y. $$

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::basic::traits::One;
use malachite_nz::integer::Integer;

assert_eq!(&Integer::from(-123) | &Integer::from(-456), -67);
assert_eq!(
    &-Integer::from(10u32).pow(12) | &-(Integer::from(10u32).pow(12) + Integer::ONE),
    -999999995905i64
);
The resulting type after applying the | operator.

Takes the bitwise or of two Integers, taking the first by value and the second by reference.

$$ f(x, y) = x \vee y. $$

Worst-case complexity

$T(n) = O(n)$

$M(m) = O(m)$

where $T$ is time, $M$ is additional memory, $n$ is max(self.significant_bits(), other.significant_bits()), and $m$ is other.significant_bits().

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::basic::traits::One;
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(-123) | &Integer::from(-456), -67);
assert_eq!(
    -Integer::from(10u32).pow(12) | &-(Integer::from(10u32).pow(12) + Integer::ONE),
    -999999995905i64
);
The resulting type after applying the | operator.

Takes the bitwise or of two Integers, taking the first by reference and the second by value.

$$ f(x, y) = x \vee y. $$

Worst-case complexity

$T(n) = O(n)$

$M(m) = O(m)$

where $T$ is time, $M$ is additional memory, $n$ is max(self.significant_bits(), other.significant_bits()), and $m$ is self.significant_bits().

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::basic::traits::One;
use malachite_nz::integer::Integer;

assert_eq!(&Integer::from(-123) | Integer::from(-456), -67);
assert_eq!(
    &-Integer::from(10u32).pow(12) | -(Integer::from(10u32).pow(12) + Integer::ONE),
    -999999995905i64
);
The resulting type after applying the | operator.

Takes the bitwise or of two Integers, taking both by value.

$$ f(x, y) = x \vee y. $$

Worst-case complexity

$T(n) = O(n)$

$M(m) = O(m)$

where $T$ is time, $M$ is additional memory, $n$ is max(self.significant_bits(), other.significant_bits()), and $m$ is min(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::basic::traits::One;
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(-123) | Integer::from(-456), -67);
assert_eq!(
    -Integer::from(10u32).pow(12) | -(Integer::from(10u32).pow(12) + Integer::ONE),
    -999999995905i64
);
The resulting type after applying the | operator.

Bitwise-ors an Integer with another Integer in place, taking the Integer on the right-hand side by reference.

Worst-case complexity

$T(n) = O(n)$

$M(m) = O(m)$

where $T$ is time, $M$ is additional memory, $n$ is max(self.significant_bits(), other.significant_bits()), and $m$ is other.significant_bits().

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;

let mut x = Integer::ZERO;
x |= &Integer::from(0x0000000f);
x |= &Integer::from(0x00000f00);
x |= &Integer::from(0x000f_0000);
x |= &Integer::from(0x0f000000);
assert_eq!(x, 0x0f0f_0f0f);

Bitwise-ors an Integer with another Integer in place, taking the Integer on the right-hand side by value.

Worst-case complexity

$T(n) = O(n)$

$M(m) = O(m)$

where $T$ is time, $M$ is additional memory, $n$ is max(self.significant_bits(), other.significant_bits()), and $m$ is min(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;

let mut x = Integer::ZERO;
x |= Integer::from(0x0000000f);
x |= Integer::from(0x00000f00);
x |= Integer::from(0x000f_0000);
x |= Integer::from(0x0f000000);
assert_eq!(x, 0x0f0f_0f0f);

Given an Integer and a starting index, searches the Integer for the smallest index of a false bit that is greater than or equal to the starting index.

If the [Integer] is negative, and the starting index is too large and there are no more false bits above it, 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
extern crate malachite_base;

use malachite_base::num::logic::traits::BitScan;
use malachite_nz::integer::Integer;

assert_eq!((-Integer::from(0x500000000u64)).index_of_next_false_bit(0), Some(0));
assert_eq!((-Integer::from(0x500000000u64)).index_of_next_false_bit(20), Some(20));
assert_eq!((-Integer::from(0x500000000u64)).index_of_next_false_bit(31), Some(31));
assert_eq!((-Integer::from(0x500000000u64)).index_of_next_false_bit(32), Some(34));
assert_eq!((-Integer::from(0x500000000u64)).index_of_next_false_bit(33), Some(34));
assert_eq!((-Integer::from(0x500000000u64)).index_of_next_false_bit(34), Some(34));
assert_eq!((-Integer::from(0x500000000u64)).index_of_next_false_bit(35), None);
assert_eq!((-Integer::from(0x500000000u64)).index_of_next_false_bit(100), None);

Given an Integer and a starting index, searches the Integer for the smallest index of a true bit that is greater than or equal to the starting index.

If the Integer is non-negative, and the starting index is too large and there are no more true bits above it, 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
extern crate malachite_base;

use malachite_base::num::logic::traits::BitScan;
use malachite_nz::integer::Integer;

assert_eq!((-Integer::from(0x500000000u64)).index_of_next_true_bit(0), Some(32));
assert_eq!((-Integer::from(0x500000000u64)).index_of_next_true_bit(20), Some(32));
assert_eq!((-Integer::from(0x500000000u64)).index_of_next_true_bit(31), Some(32));
assert_eq!((-Integer::from(0x500000000u64)).index_of_next_true_bit(32), Some(32));
assert_eq!((-Integer::from(0x500000000u64)).index_of_next_true_bit(33), Some(33));
assert_eq!((-Integer::from(0x500000000u64)).index_of_next_true_bit(34), Some(35));
assert_eq!((-Integer::from(0x500000000u64)).index_of_next_true_bit(35), Some(35));
assert_eq!((-Integer::from(0x500000000u64)).index_of_next_true_bit(36), Some(36));
assert_eq!((-Integer::from(0x500000000u64)).index_of_next_true_bit(100), Some(100));

Takes the bitwise xor of two Integers, taking both by reference.

$$ f(x, y) = x \oplus y. $$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::basic::traits::One;
use malachite_nz::integer::Integer;
use std::str::FromStr;

assert_eq!(&Integer::from(-123) ^ &Integer::from(-456), 445);
assert_eq!(
    &-Integer::from(10u32).pow(12) ^ &-(Integer::from(10u32).pow(12) + Integer::ONE),
    8191
);
The resulting type after applying the ^ operator.

Takes the bitwise xor of two Integers, taking the first by value and the second by reference.

$$ f(x, y) = x \oplus y. $$

Worst-case complexity

$T(n) = O(n)$

$M(m) = O(m)$

where $T$ is time, $M$ is additional memory, $n$ is max(self.significant_bits(), other.significant_bits()), and $m$ is other.significant_bits().

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::basic::traits::One;
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(-123) ^ &Integer::from(-456), 445);
assert_eq!(
    -Integer::from(10u32).pow(12) ^ &-(Integer::from(10u32).pow(12) + Integer::ONE),
    8191
);
The resulting type after applying the ^ operator.

Takes the bitwise xor of two Integers, taking the first by reference and the second by value.

$$ f(x, y) = x \oplus y. $$

Worst-case complexity

$T(n) = O(n)$

$M(m) = O(m)$

where $T$ is time, $M$ is additional memory, $n$ is max(self.significant_bits(), other.significant_bits()), and $m$ is self.significant_bits().

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::basic::traits::One;
use malachite_nz::integer::Integer;
use std::str::FromStr;

assert_eq!(&Integer::from(-123) ^ Integer::from(-456), 445);
assert_eq!(
    &-Integer::from(10u32).pow(12) ^ -(Integer::from(10u32).pow(12) + Integer::ONE),
    8191
);
The resulting type after applying the ^ operator.

Takes the bitwise xor of two Integers, taking both by value.

$$ f(x, y) = x \oplus y. $$

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::basic::traits::One;
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(-123) ^ Integer::from(-456), 445);
assert_eq!(
    -Integer::from(10u32).pow(12) ^ -(Integer::from(10u32).pow(12) + Integer::ONE),
    8191
);
The resulting type after applying the ^ operator.

Bitwise-xors an Integer with another Integer in place, taking the Integer on the right-hand side by reference.

$$ x \gets x \oplus y. $$

Worst-case complexity

$T(n) = O(n)$

$M(m) = O(m)$

where $T$ is time, $M$ is additional memory, $n$ is max(self.significant_bits(), other.significant_bits()), and $m$ is other.significant_bits().

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::NegativeOne;
use malachite_nz::integer::Integer;

let mut x = Integer::from(u32::MAX);
x ^= &Integer::from(0x0000000f);
x ^= &Integer::from(0x00000f00);
x ^= &Integer::from(0x000f_0000);
x ^= &Integer::from(0x0f000000);
assert_eq!(x, 0xf0f0_f0f0u32);

Bitwise-xors an Integer with another Integer in place, taking the Integer on the right-hand side by value.

$$ x \gets x \oplus y. $$

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::NegativeOne;
use malachite_nz::integer::Integer;

let mut x = Integer::from(u32::MAX);
x ^= Integer::from(0x0000000f);
x ^= Integer::from(0x00000f00);
x ^= Integer::from(0x000f_0000);
x ^= Integer::from(0x0f000000);
assert_eq!(x, 0xf0f0_f0f0u32);

Divides an Integer by another Integer in place, taking the Integer on the right-hand side by reference and returning the remainder. The quotient is rounded towards positive infinity and the remainder has the opposite sign as the second Integer.

The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = x - y\left \lceil\frac{x}{y} \right \rceil, $$ $$ x \gets \left \lceil \frac{x}{y} \right \rceil. $$

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
use malachite_base::num::arithmetic::traits::CeilingDivAssignMod;
use malachite_nz::integer::Integer;

// 3 * 10 + -7 = 23
let mut x = Integer::from(23);
assert_eq!(x.ceiling_div_assign_mod(&Integer::from(10)), -7);
assert_eq!(x, 3);

// -2 * -10 + 3 = 23
let mut x = Integer::from(23);
assert_eq!(x.ceiling_div_assign_mod(&Integer::from(-10)), 3);
assert_eq!(x, -2);

// -2 * 10 + -3 = -23
let mut x = Integer::from(-23);
assert_eq!(x.ceiling_div_assign_mod(&Integer::from(10)), -3);
assert_eq!(x, -2);

// 3 * -10 + 7 = -23
let mut x = Integer::from(-23);
assert_eq!(x.ceiling_div_assign_mod(&Integer::from(-10)), 7);
assert_eq!(x, 3);

Divides an Integer by another Integer in place, taking the Integer on the right-hand side by value and returning the remainder. The quotient is rounded towards positive infinity and the remainder has the opposite sign as the second Integer.

The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = x - y\left \lceil\frac{x}{y} \right \rceil, $$ $$ x \gets \left \lceil \frac{x}{y} \right \rceil. $$

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
use malachite_base::num::arithmetic::traits::CeilingDivAssignMod;
use malachite_nz::integer::Integer;

// 3 * 10 + -7 = 23
let mut x = Integer::from(23);
assert_eq!(x.ceiling_div_assign_mod(Integer::from(10)), -7);
assert_eq!(x, 3);

// -2 * -10 + 3 = 23
let mut x = Integer::from(23);
assert_eq!(x.ceiling_div_assign_mod(Integer::from(-10)), 3);
assert_eq!(x, -2);

// -2 * 10 + -3 = -23
let mut x = Integer::from(-23);
assert_eq!(x.ceiling_div_assign_mod(Integer::from(10)), -3);
assert_eq!(x, -2);

// 3 * -10 + 7 = -23
let mut x = Integer::from(-23);
assert_eq!(x.ceiling_div_assign_mod(Integer::from(-10)), 7);
assert_eq!(x, 3);

Divides an Integer by another Integer, taking both the first by value and the second by reference and returning the quotient and remainder. The quotient is rounded towards positive infinity and the remainder has the opposite sign as the second Integer.

The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = \left ( \left \lceil \frac{x}{y} \right \rceil, \space x - y\left \lceil \frac{x}{y} \right \rceil \right ). $$

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
use malachite_base::num::arithmetic::traits::CeilingDivMod;
use malachite_base::strings::ToDebugString;
use malachite_nz::integer::Integer;

// 3 * 10 + -7 = 23
assert_eq!(
    Integer::from(23).ceiling_div_mod(&Integer::from(10)).to_debug_string(),
    "(3, -7)"
);

// -2 * -10 + 3 = 23
assert_eq!(
    Integer::from(23).ceiling_div_mod(&Integer::from(-10)).to_debug_string(),
    "(-2, 3)"
);

// -2 * 10 + -3 = -23
assert_eq!(
    Integer::from(-23).ceiling_div_mod(&Integer::from(10)).to_debug_string(),
    "(-2, -3)"
);

// 3 * -10 + 7 = -23
assert_eq!(
    Integer::from(-23).ceiling_div_mod(&Integer::from(-10)).to_debug_string(),
    "(3, 7)"
);

Divides an Integer by another Integer, taking both by reference and returning the quotient and remainder. The quotient is rounded towards positive infinity and the remainder has the opposite sign as the second Integer.

The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = \left ( \left \lceil \frac{x}{y} \right \rceil, \space x - y\left \lceil \frac{x}{y} \right \rceil \right ). $$

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
use malachite_base::num::arithmetic::traits::CeilingDivMod;
use malachite_base::strings::ToDebugString;
use malachite_nz::integer::Integer;

// 3 * 10 + -7 = 23
assert_eq!(
    (&Integer::from(23)).ceiling_div_mod(&Integer::from(10)).to_debug_string(),
    "(3, -7)"
);

// -2 * -10 + 3 = 23
assert_eq!(
    (&Integer::from(23)).ceiling_div_mod(&Integer::from(-10)).to_debug_string(),
    "(-2, 3)"
);

// -2 * 10 + -3 = -23
assert_eq!(
    (&Integer::from(-23)).ceiling_div_mod(&Integer::from(10)).to_debug_string(),
    "(-2, -3)"
);

// 3 * -10 + 7 = -23
assert_eq!(
    (&Integer::from(-23)).ceiling_div_mod(&Integer::from(-10)).to_debug_string(),
    "(3, 7)"
);

Divides an Integer by another Integer, taking the first by reference and the second by value and returning the quotient and remainder. The quotient is rounded towards positive infinity and the remainder has the opposite sign as the second Integer.

The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = \left ( \left \lceil \frac{x}{y} \right \rceil, \space x - y\left \lceil \frac{x}{y} \right \rceil \right ). $$

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
use malachite_base::num::arithmetic::traits::CeilingDivMod;
use malachite_base::strings::ToDebugString;
use malachite_nz::integer::Integer;

// 3 * 10 + -7 = 23
assert_eq!(
    (&Integer::from(23)).ceiling_div_mod(Integer::from(10)).to_debug_string(),
    "(3, -7)"
);

// -2 * -10 + 3 = 23
assert_eq!(
    (&Integer::from(23)).ceiling_div_mod(Integer::from(-10)).to_debug_string(),
    "(-2, 3)"
);

// -2 * 10 + -3 = -23
assert_eq!(
    (&Integer::from(-23)).ceiling_div_mod(Integer::from(10)).to_debug_string(),
    "(-2, -3)"
);

// 3 * -10 + 7 = -23
assert_eq!(
    (&Integer::from(-23)).ceiling_div_mod(Integer::from(-10)).to_debug_string(),
    "(3, 7)"
);

Divides an Integer by another Integer, taking both by value and returning the quotient and remainder. The quotient is rounded towards positive infinity and the remainder has the opposite sign as the second Integer.

The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = \left ( \left \lceil \frac{x}{y} \right \rceil, \space x - y\left \lceil \frac{x}{y} \right \rceil \right ). $$

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
use malachite_base::num::arithmetic::traits::CeilingDivMod;
use malachite_base::strings::ToDebugString;
use malachite_nz::integer::Integer;

// 3 * 10 + -7 = 23
assert_eq!(
    Integer::from(23).ceiling_div_mod(Integer::from(10)).to_debug_string(),
    "(3, -7)"
);

// -2 * -10 + 3 = 23
assert_eq!(
    Integer::from(23).ceiling_div_mod(Integer::from(-10)).to_debug_string(),
    "(-2, 3)"
);

// -2 * 10 + -3 = -23
assert_eq!(
    Integer::from(-23).ceiling_div_mod(Integer::from(10)).to_debug_string(),
    "(-2, -3)"
);

// 3 * -10 + 7 = -23
assert_eq!(
    Integer::from(-23).ceiling_div_mod(Integer::from(-10)).to_debug_string(),
    "(3, 7)"
);

Divides an Integer by another Integer, taking the first by value and the second by reference and returning just the remainder. The remainder has the opposite sign as the second Integer.

If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = x - y\left \lceil \frac{x}{y} \right \rceil. $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CeilingMod;
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
assert_eq!(Integer::from(23).ceiling_mod(&Integer::from(10)), -7);

// -3 * -10 + -7 = 23
assert_eq!(Integer::from(23).ceiling_mod(&Integer::from(-10)), 3);

// -3 * 10 + 7 = -23
assert_eq!(Integer::from(-23).ceiling_mod(&Integer::from(10)), -3);

// 2 * -10 + -3 = -23
assert_eq!(Integer::from(-23).ceiling_mod(&Integer::from(-10)), 7);

Divides an Integer by another Integer, taking both by reference and returning just the remainder. The remainder has the opposite sign as the second Integer.

If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = x - y\left \lceil \frac{x}{y} \right \rceil. $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CeilingMod;
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
assert_eq!((&Integer::from(23)).ceiling_mod(&Integer::from(10)), -7);

// -3 * -10 + -7 = 23
assert_eq!((&Integer::from(23)).ceiling_mod(&Integer::from(-10)), 3);

// -3 * 10 + 7 = -23
assert_eq!((&Integer::from(-23)).ceiling_mod(&Integer::from(10)), -3);

// 2 * -10 + -3 = -23
assert_eq!((&Integer::from(-23)).ceiling_mod(&Integer::from(-10)), 7);

Divides an Integer by another Integer, taking the first by reference and the second by value and returning just the remainder. The remainder has the opposite sign as the second Integer.

If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = x - y\left \lceil \frac{x}{y} \right \rceil. $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CeilingMod;
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
assert_eq!((&Integer::from(23)).ceiling_mod(Integer::from(10)), -7);

// -3 * -10 + -7 = 23
assert_eq!((&Integer::from(23)).ceiling_mod(Integer::from(-10)), 3);

// -3 * 10 + 7 = -23
assert_eq!((&Integer::from(-23)).ceiling_mod(Integer::from(10)), -3);

// 2 * -10 + -3 = -23
assert_eq!((&Integer::from(-23)).ceiling_mod(Integer::from(-10)), 7);

Divides an Integer by another Integer, taking both by value and returning just the remainder. The remainder has the opposite sign as the second Integer.

If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = x - y\left \lceil \frac{x}{y} \right \rceil. $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CeilingMod;
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
assert_eq!(Integer::from(23).ceiling_mod(Integer::from(10)), -7);

// -3 * -10 + -7 = 23
assert_eq!(Integer::from(23).ceiling_mod(Integer::from(-10)), 3);

// -3 * 10 + 7 = -23
assert_eq!(Integer::from(-23).ceiling_mod(Integer::from(10)), -3);

// 2 * -10 + -3 = -23
assert_eq!(Integer::from(-23).ceiling_mod(Integer::from(-10)), 7);

Divides an Integer by another Integer, taking the Integer on the right-hand side by reference and replacing the first number by the remainder. The remainder has the opposite sign as the second number.

If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ x \gets x - y\left \lceil\frac{x}{y} \right \rceil. $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CeilingModAssign;
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
let mut x = Integer::from(23);
x.ceiling_mod_assign(&Integer::from(10));
assert_eq!(x, -7);

// -3 * -10 + -7 = 23
let mut x = Integer::from(23);
x.ceiling_mod_assign(&Integer::from(-10));
assert_eq!(x, 3);

// -3 * 10 + 7 = -23
let mut x = Integer::from(-23);
x.ceiling_mod_assign(&Integer::from(10));
assert_eq!(x, -3);

// 2 * -10 + -3 = -23
let mut x = Integer::from(-23);
x.ceiling_mod_assign(&Integer::from(-10));
assert_eq!(x, 7);

Divides an Integer by another Integer, taking the Integer on the right-hand side by value and replacing the first number by the remainder. The remainder has the opposite sign as the second number.

If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ x \gets x - y\left \lceil\frac{x}{y} \right \rceil. $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CeilingModAssign;
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
let mut x = Integer::from(23);
x.ceiling_mod_assign(Integer::from(10));
assert_eq!(x, -7);

// -3 * -10 + -7 = 23
let mut x = Integer::from(23);
x.ceiling_mod_assign(Integer::from(-10));
assert_eq!(x, 3);

// -3 * 10 + 7 = -23
let mut x = Integer::from(-23);
x.ceiling_mod_assign(Integer::from(10));
assert_eq!(x, -3);

// 2 * -10 + -3 = -23
let mut x = Integer::from(-23);
x.ceiling_mod_assign(Integer::from(-10));
assert_eq!(x, 7);

Divides an Integer by $2^k$, taking it by reference and returning just the remainder. The remainder is non-positive.

If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0 \leq -r < 2^k$.

$$ f(x, y) = x - 2^k\left \lceil \frac{x}{2^k} \right \rceil. $$

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is pow.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CeilingModPowerOf2;
use malachite_nz::integer::Integer;

// 2 * 2^8 + -252 = 260
assert_eq!((&Integer::from(260)).ceiling_mod_power_of_2(8), -252);
// -100 * 2^4 + -11 = -1611
assert_eq!((&Integer::from(-1611)).ceiling_mod_power_of_2(4), -11);

Divides an Integer by $2^k$, taking it by value and returning just the remainder. The remainder is non-positive.

If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0 \leq -r < 2^k$.

$$ f(x, y) = x - 2^k\left \lceil \frac{x}{2^k} \right \rceil. $$

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is pow.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CeilingModPowerOf2;
use malachite_nz::integer::Integer;

// 2 * 2^8 + -252 = 260
assert_eq!(Integer::from(260).ceiling_mod_power_of_2(8), -252);

// -100 * 2^4 + -11 = -1611
assert_eq!(Integer::from(-1611).ceiling_mod_power_of_2(4), -11);

Divides an Integer by $2^k$, replacing the Integer by the remainder. The remainder is non-positive.

If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0 \leq -r < 2^k$.

$$ x \gets x - 2^k\left \lceil\frac{x}{2^k} \right \rceil. $$

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is pow.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CeilingModPowerOf2Assign;
use malachite_nz::integer::Integer;

// 2 * 2^8 + -252 = 260
let mut x = Integer::from(260);
x.ceiling_mod_power_of_2_assign(8);
assert_eq!(x, -252);

// -100 * 2^4 + -11 = -1611
let mut x = Integer::from(-1611);
x.ceiling_mod_power_of_2_assign(4);
assert_eq!(x, -11);

Returns the ceiling of the $n$th root of an Integer, taking the Integer by reference.

$f(x, n) = \lceil\sqrt[n]{x}\rceil$.

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if exp is zero, or if exp is even and self is negative.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CeilingRoot;
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(999).ceiling_root(3), 10);
assert_eq!(Integer::from(1000).ceiling_root(3), 10);
assert_eq!(Integer::from(1001).ceiling_root(3), 11);
assert_eq!(Integer::from(100000000000i64).ceiling_root(5), 159);
assert_eq!(Integer::from(-100000000000i64).ceiling_root(5), -158);

Returns the ceiling of the $n$th root of an Integer, taking the Integer by value.

$f(x, n) = \lceil\sqrt[n]{x}\rceil$.

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if exp is zero, or if exp is even and self is negative.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CeilingRoot;
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(999).ceiling_root(3), 10);
assert_eq!(Integer::from(1000).ceiling_root(3), 10);
assert_eq!(Integer::from(1001).ceiling_root(3), 11);
assert_eq!(Integer::from(100000000000i64).ceiling_root(5), 159);
assert_eq!(Integer::from(-100000000000i64).ceiling_root(5), -158);

Replaces an Integer with the ceiling of its $n$th root.

$x \gets \lceil\sqrt[n]{x}\rceil$.

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if exp is zero, or if exp is even and self is negative.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CeilingRootAssign;
use malachite_nz::integer::Integer;

let mut x = Integer::from(999);
x.ceiling_root_assign(3);
assert_eq!(x, 10);

let mut x = Integer::from(1000);
x.ceiling_root_assign(3);
assert_eq!(x, 10);

let mut x = Integer::from(1001);
x.ceiling_root_assign(3);
assert_eq!(x, 11);

let mut x = Integer::from(100000000000i64);
x.ceiling_root_assign(5);
assert_eq!(x, 159);

let mut x = Integer::from(-100000000000i64);
x.ceiling_root_assign(5);
assert_eq!(x, -158);

Returns the ceiling of the square root of an Integer, taking it by reference.

$f(x) = \lceil\sqrt{x}\rceil$.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if self is negative.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CeilingSqrt;
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(99).ceiling_sqrt(), 10);
assert_eq!(Integer::from(100).ceiling_sqrt(), 10);
assert_eq!(Integer::from(101).ceiling_sqrt(), 11);
assert_eq!(Integer::from(1000000000).ceiling_sqrt(), 31623);
assert_eq!(Integer::from(10000000000u64).ceiling_sqrt(), 100000);

Returns the ceiling of the square root of an Integer, taking it by value.

$f(x) = \lceil\sqrt{x}\rceil$.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if self is negative.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CeilingSqrt;
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(99).ceiling_sqrt(), 10);
assert_eq!(Integer::from(100).ceiling_sqrt(), 10);
assert_eq!(Integer::from(101).ceiling_sqrt(), 11);
assert_eq!(Integer::from(1000000000).ceiling_sqrt(), 31623);
assert_eq!(Integer::from(10000000000u64).ceiling_sqrt(), 100000);

Replaces an Integer with the ceiling of its square root.

$x \gets \lceil\sqrt{x}\rceil$.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if self is negative.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CeilingSqrtAssign;
use malachite_nz::integer::Integer;

let mut x = Integer::from(99u8);
x.ceiling_sqrt_assign();
assert_eq!(x, 10);

let mut x = Integer::from(100);
x.ceiling_sqrt_assign();
assert_eq!(x, 10);

let mut x = Integer::from(101);
x.ceiling_sqrt_assign();
assert_eq!(x, 11);

let mut x = Integer::from(1000000000);
x.ceiling_sqrt_assign();
assert_eq!(x, 31623);

let mut x = Integer::from(10000000000u64);
x.ceiling_sqrt_assign();
assert_eq!(x, 100000);

Determines the Hamming distance between two Integers.

The two Integers have infinitely many leading zeros or infinitely many leading ones, depending on their signs. If they are both non-negative or both negative, the Hamming distance is finite. If one is non-negative and the other is negative, the Hamming distance 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 max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::logic::traits::CheckedHammingDistance;
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(123).checked_hamming_distance(&Integer::from(123)), Some(0));
// 105 = 1101001b, 123 = 1111011
assert_eq!(Integer::from(-105).checked_hamming_distance(&Integer::from(-123)), Some(2));
assert_eq!(Integer::from(-105).checked_hamming_distance(&Integer::from(123)), None);

Returns the the $n$th root of an Integer, or None if the Integer is not a perfect $n$th power. The Integer is taken by reference.

$$ f(x, n) = \begin{cases} \operatorname{Some}(sqrt[n]{x}) & \text{if} \quad \sqrt[n]{x} \in \Z, \\ \operatorname{None} & \textrm{otherwise}. \end{cases} $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if exp is zero, or if exp is even and self is negative.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CheckedRoot;
use malachite_base::strings::ToDebugString;
use malachite_nz::integer::Integer;

assert_eq!((&Integer::from(999)).checked_root(3).to_debug_string(), "None");
assert_eq!((&Integer::from(1000)).checked_root(3).to_debug_string(), "Some(10)");
assert_eq!((&Integer::from(1001)).checked_root(3).to_debug_string(), "None");
assert_eq!((&Integer::from(100000000000i64)).checked_root(5).to_debug_string(), "None");
assert_eq!((&Integer::from(-100000000000i64)).checked_root(5).to_debug_string(), "None");
assert_eq!((&Integer::from(10000000000i64)).checked_root(5).to_debug_string(), "Some(100)");
assert_eq!(
    (&Integer::from(-10000000000i64)).checked_root(5).to_debug_string(),
    "Some(-100)"
);

Returns the the $n$th root of an Integer, or None if the Integer is not a perfect $n$th power. The Integer is taken by value.

$$ f(x, n) = \begin{cases} \operatorname{Some}(sqrt[n]{x}) & \text{if} \quad \sqrt[n]{x} \in \Z, \\ \operatorname{None} & \textrm{otherwise}. \end{cases} $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if exp is zero, or if exp is even and self is negative.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CheckedRoot;
use malachite_base::strings::ToDebugString;
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(999).checked_root(3).to_debug_string(), "None");
assert_eq!(Integer::from(1000).checked_root(3).to_debug_string(), "Some(10)");
assert_eq!(Integer::from(1001).checked_root(3).to_debug_string(), "None");
assert_eq!(Integer::from(100000000000i64).checked_root(5).to_debug_string(), "None");
assert_eq!(Integer::from(-100000000000i64).checked_root(5).to_debug_string(), "None");
assert_eq!(Integer::from(10000000000i64).checked_root(5).to_debug_string(), "Some(100)");
assert_eq!(Integer::from(-10000000000i64).checked_root(5).to_debug_string(), "Some(-100)");

Returns the the square root of an Integer, or None if it is not a perfect square. The Integer is taken by reference.

$$ f(x) = \begin{cases} \operatorname{Some}(sqrt{x}) & \text{if} \quad \sqrt{x} \in \Z, \\ \operatorname{None} & \textrm{otherwise}. \end{cases} $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if self is negative.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CheckedSqrt;
use malachite_base::strings::ToDebugString;
use malachite_nz::integer::Integer;

assert_eq!((&Integer::from(99u8)).checked_sqrt().to_debug_string(), "None");
assert_eq!((&Integer::from(100u8)).checked_sqrt().to_debug_string(), "Some(10)");
assert_eq!((&Integer::from(101u8)).checked_sqrt().to_debug_string(), "None");
assert_eq!((&Integer::from(1000000000u32)).checked_sqrt().to_debug_string(), "None");
assert_eq!(
    (&Integer::from(10000000000u64)).checked_sqrt().to_debug_string(),
    "Some(100000)"
);

Returns the the square root of an Integer, or None if it is not a perfect square. The Integer is taken by value.

$$ f(x) = \begin{cases} \operatorname{Some}(sqrt{x}) & \text{if} \quad \sqrt{x} \in \Z, \\ \operatorname{None} & \textrm{otherwise}. \end{cases} $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if self is negative.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::CheckedSqrt;
use malachite_base::strings::ToDebugString;
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(99u8).checked_sqrt().to_debug_string(), "None");
assert_eq!(Integer::from(100u8).checked_sqrt().to_debug_string(), "Some(10)");
assert_eq!(Integer::from(101u8).checked_sqrt().to_debug_string(), "None");
assert_eq!(Integer::from(1000000000u32).checked_sqrt().to_debug_string(), "None");
assert_eq!(Integer::from(10000000000u64).checked_sqrt().to_debug_string(), "Some(100000)");
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more

Determines whether an Integer can be converted to a Natural (when the Integer is non-negative). Takes the Integer by reference.

Worst-case complexity

Constant time and additional memory.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::conversion::traits::ConvertibleFrom;
use malachite_nz::integer::Integer;
use malachite_nz::natural::Natural;

assert_eq!(Natural::convertible_from(&Integer::from(123)), true);
assert_eq!(Natural::convertible_from(&Integer::from(-123)), false);
assert_eq!(Natural::convertible_from(&Integer::from(10u32).pow(12)), true);
assert_eq!(Natural::convertible_from(&-Integer::from(10u32).pow(12)), false);

Determines whether an Integer can be exactly converted to a primitive float.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is value.significant_bits().

Examples

See here.

Determines whether an Integer can be exactly converted to a primitive float.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is value.significant_bits().

Examples

See here.

Determines whether an Integer can be converted to a signed primitive integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Determines whether an Integer can be converted to a signed primitive integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Determines whether an Integer can be converted to a signed primitive integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Determines whether an Integer can be converted to a signed primitive integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Determines whether an Integer can be converted to a signed primitive integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Determines whether an Integer can be converted to a signed primitive integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Determines whether an Integer can be converted to an unsigned primitive integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Determines whether an Integer can be converted to an unsigned primitive integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Determines whether an Integer can be converted to an unsigned primitive integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Determines whether an Integer can be converted to an unsigned primitive integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Determines whether an Integer can be converted to an unsigned primitive integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Determines whether an Integer can be converted to an unsigned primitive integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Determines whether an Integer can be converted to a Natural (when the Integer is non-negative). Takes the Integer by value.

Worst-case complexity

Constant time and additional memory.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Pow;
use malachite_base::num::conversion::traits::ConvertibleFrom;
use malachite_nz::integer::Integer;
use malachite_nz::natural::Natural;

assert_eq!(Natural::convertible_from(Integer::from(123)), true);
assert_eq!(Natural::convertible_from(Integer::from(-123)), false);
assert_eq!(Natural::convertible_from(Integer::from(10u32).pow(12)), true);
assert_eq!(Natural::convertible_from(-Integer::from(10u32).pow(12)), false);

Determines whether a primitive float can be exactly converted to an Integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Determines whether a primitive float can be exactly converted to an Integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts an Integer to a String.

This is the same as the Display::fmt implementation.

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::Zero;
use malachite_base::strings::ToDebugString;
use malachite_nz::integer::Integer;
use std::str::FromStr;

assert_eq!(Integer::ZERO.to_debug_string(), "0");

assert_eq!(Integer::from(123).to_debug_string(), "123");
assert_eq!(
    Integer::from_str("1000000000000")
        .unwrap()
        .to_debug_string(),
    "1000000000000"
);
assert_eq!(format!("{:05?}", Integer::from(123)), "00123");

assert_eq!(Integer::from(-123).to_debug_string(), "-123");
assert_eq!(
    Integer::from_str("-1000000000000")
        .unwrap()
        .to_debug_string(),
    "-1000000000000"
);
assert_eq!(format!("{:05?}", Integer::from(-123)), "-0123");

The default value of an Integer, 0.

Converts an Integer to a String.

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;
use std::str::FromStr;

assert_eq!(Integer::ZERO.to_string(), "0");

assert_eq!(Integer::from(123).to_string(), "123");
assert_eq!(
    Integer::from_str("1000000000000").unwrap().to_string(),
    "1000000000000"
);
assert_eq!(format!("{:05}", Integer::from(123)), "00123");

assert_eq!(Integer::from(-123).to_string(), "-123");
assert_eq!(
    Integer::from_str("-1000000000000").unwrap().to_string(),
    "-1000000000000"
);
assert_eq!(format!("{:05}", Integer::from(-123)), "-0123");

Divides an Integer by another Integer, taking the first by value and the second by reference. The quotient is rounded towards zero. The quotient and remainder (which is not computed) satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right | \right \rfloor. $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
assert_eq!(Integer::from(23) / &Integer::from(10), 2);

// -2 * -10 + 3 = 23
assert_eq!(Integer::from(23) / &Integer::from(-10), -2);

// -2 * 10 + -3 = -23
assert_eq!(Integer::from(-23) / &Integer::from(10), -2);

// 2 * -10 + -3 = -23
assert_eq!(Integer::from(-23) / &Integer::from(-10), 2);
The resulting type after applying the / operator.

Divides an Integer by another Integer, taking both by reference. The quotient is rounded towards zero. The quotient and remainder (which is not computed) satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right | \right \rfloor. $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
assert_eq!(&Integer::from(23) / &Integer::from(10), 2);

// -2 * -10 + 3 = 23
assert_eq!(&Integer::from(23) / &Integer::from(-10), -2);

// -2 * 10 + -3 = -23
assert_eq!(&Integer::from(-23) / &Integer::from(10), -2);

// 2 * -10 + -3 = -23
assert_eq!(&Integer::from(-23) / &Integer::from(-10), 2);
The resulting type after applying the / operator.

Divides an Integer by another Integer, taking the first by reference and the second by value. The quotient is rounded towards zero. The quotient and remainder (which is not computed) satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right | \right \rfloor. $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
assert_eq!(&Integer::from(23) / Integer::from(10), 2);

// -2 * -10 + 3 = 23
assert_eq!(&Integer::from(23) / Integer::from(-10), -2);

// -2 * 10 + -3 = -23
assert_eq!(&Integer::from(-23) / Integer::from(10), -2);

// 2 * -10 + -3 = -23
assert_eq!(&Integer::from(-23) / Integer::from(-10), 2);
The resulting type after applying the / operator.

Divides an Integer by another Integer, taking both by value. The quotient is rounded towards zero. The quotient and remainder (which is not computed) satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right | \right \rfloor. $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
assert_eq!(Integer::from(23) / Integer::from(10), 2);

// -2 * -10 + 3 = 23
assert_eq!(Integer::from(23) / Integer::from(-10), -2);

// -2 * 10 + -3 = -23
assert_eq!(Integer::from(-23) / Integer::from(10), -2);

// 2 * -10 + -3 = -23
assert_eq!(Integer::from(-23) / Integer::from(-10), 2);
The resulting type after applying the / operator.

Divides an Integer by another Integer in place, taking the Integer on the right-hand side by reference. The quotient is rounded towards zero. The quotient and remainder (which is not computed) satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ x \gets \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right | \right \rfloor. $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
let mut x = Integer::from(23);
x /= &Integer::from(10);
assert_eq!(x, 2);

// -2 * -10 + 3 = 23
let mut x = Integer::from(23);
x /= &Integer::from(-10);
assert_eq!(x, -2);

// -2 * 10 + -3 = -23
let mut x = Integer::from(-23);
x /= &Integer::from(10);
assert_eq!(x, -2);

// 2 * -10 + -3 = -23
let mut x = Integer::from(-23);
x /= &Integer::from(-10);
assert_eq!(x, 2);

Divides an Integer by another Integer in place, taking the Integer on the right-hand side by value. The quotient is rounded towards zero. The quotient and remainder (which is not computed) satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ x \gets \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right | \right \rfloor. $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
let mut x = Integer::from(23);
x /= Integer::from(10);
assert_eq!(x, 2);

// -2 * -10 + 3 = 23
let mut x = Integer::from(23);
x /= Integer::from(-10);
assert_eq!(x, -2);

// -2 * 10 + -3 = -23
let mut x = Integer::from(-23);
x /= Integer::from(10);
assert_eq!(x, -2);

// 2 * -10 + -3 = -23
let mut x = Integer::from(-23);
x /= Integer::from(-10);
assert_eq!(x, 2);

Divides an Integer by another Integer in place, taking the Integer on the right-hand side by reference and returning the remainder. The quotient is rounded towards negative infinity, and the remainder has the same sign as the second Integer.

The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = x - y\left \lfloor \frac{x}{y} \right \rfloor, $$ $$ x \gets \left \lfloor \frac{x}{y} \right \rfloor. $$

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
use malachite_base::num::arithmetic::traits::DivAssignMod;
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
let mut x = Integer::from(23);
assert_eq!(x.div_assign_mod(&Integer::from(10)), 3);
assert_eq!(x, 2);

// -3 * -10 + -7 = 23
let mut x = Integer::from(23);
assert_eq!(x.div_assign_mod(&Integer::from(-10)), -7);
assert_eq!(x, -3);

// -3 * 10 + 7 = -23
let mut x = Integer::from(-23);
assert_eq!(x.div_assign_mod(&Integer::from(10)), 7);
assert_eq!(x, -3);

// 2 * -10 + -3 = -23
let mut x = Integer::from(-23);
assert_eq!(x.div_assign_mod(&Integer::from(-10)), -3);
assert_eq!(x, 2);

Divides an Integer by another Integer in place, taking the Integer on the right-hand side by value and returning the remainder. The quotient is rounded towards negative infinity, and the remainder has the same sign as the second Integer.

The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = x - y\left \lfloor \frac{x}{y} \right \rfloor, $$ $$ x \gets \left \lfloor \frac{x}{y} \right \rfloor. $$

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
use malachite_base::num::arithmetic::traits::DivAssignMod;
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
let mut x = Integer::from(23);
assert_eq!(x.div_assign_mod(Integer::from(10)), 3);
assert_eq!(x, 2);

// -3 * -10 + -7 = 23
let mut x = Integer::from(23);
assert_eq!(x.div_assign_mod(Integer::from(-10)), -7);
assert_eq!(x, -3);

// -3 * 10 + 7 = -23
let mut x = Integer::from(-23);
assert_eq!(x.div_assign_mod(Integer::from(10)), 7);
assert_eq!(x, -3);

// 2 * -10 + -3 = -23
let mut x = Integer::from(-23);
assert_eq!(x.div_assign_mod(Integer::from(-10)), -3);
assert_eq!(x, 2);

Divides an Integer by another Integer in place, taking the Integer on the right-hand side by reference and returning the remainder. The quotient is rounded towards zero and the remainder has the same sign as the first Integer.

The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = x - y \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right | \right \rfloor, $$ $$ x \gets \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right | \right \rfloor. $$

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
use malachite_base::num::arithmetic::traits::DivAssignRem;
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
let mut x = Integer::from(23);
assert_eq!(x.div_assign_rem(&Integer::from(10)), 3);
assert_eq!(x, 2);

// -2 * -10 + 3 = 23
let mut x = Integer::from(23);
assert_eq!(x.div_assign_rem(&Integer::from(-10)), 3);
assert_eq!(x, -2);

// -2 * 10 + -3 = -23
let mut x = Integer::from(-23);
assert_eq!(x.div_assign_rem(&Integer::from(10)), -3);
assert_eq!(x, -2);

// 2 * -10 + -3 = -23
let mut x = Integer::from(-23);
assert_eq!(x.div_assign_rem(&Integer::from(-10)), -3);
assert_eq!(x, 2);

Divides an Integer by another Integer in place, taking the Integer on the right-hand side by value and returning the remainder. The quotient is rounded towards zero and the remainder has the same sign as the first Integer.

The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = x - y \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right | \right \rfloor, $$ $$ x \gets \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right | \right \rfloor. $$

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
use malachite_base::num::arithmetic::traits::DivAssignRem;
use malachite_base::strings::ToDebugString;
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
let mut x = Integer::from(23);
assert_eq!(x.div_assign_rem(Integer::from(10)), 3);
assert_eq!(x, 2);

// -2 * -10 + 3 = 23
let mut x = Integer::from(23);
assert_eq!(x.div_assign_rem(Integer::from(-10)), 3);
assert_eq!(x, -2);

// -2 * 10 + -3 = -23
let mut x = Integer::from(-23);
assert_eq!(x.div_assign_rem(Integer::from(10)), -3);
assert_eq!(x, -2);

// 2 * -10 + -3 = -23
let mut x = Integer::from(-23);
assert_eq!(x.div_assign_rem(Integer::from(-10)), -3);
assert_eq!(x, 2);

Divides an Integer by another Integer, taking the first by value and the second by reference. The first Integer must be exactly divisible by the second. If it isn’t, this function may panic or return a meaningless result.

$$ f(x, y) = \frac{x}{y}. $$

If you are unsure whether the division will be exact, use self / &other instead. If you’re unsure and you want to know, use self.div_mod(&other) and check whether the remainder is zero. If you want a function that panics if the division is not exact, use self.div_round(&other, RoundingMode::Exact).

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero. May panic if self is not divisible by other.

Examples
use malachite_base::num::arithmetic::traits::DivExact;
use malachite_nz::integer::Integer;
use std::str::FromStr;

// -123 * 456 = -56088
assert_eq!(Integer::from(-56088).div_exact(&Integer::from(456)), -123);

// -123456789000 * -987654321000 = 121932631112635269000000
assert_eq!(
    Integer::from_str("121932631112635269000000").unwrap()
            .div_exact(&Integer::from_str("-987654321000").unwrap()),
    -123456789000i64
);

Divides an Integer by another Integer, taking both by reference. The first Integer must be exactly divisible by the second. If it isn’t, this function may panic or return a meaningless result.

$$ f(x, y) = \frac{x}{y}. $$

If you are unsure whether the division will be exact, use &self / &other instead. If you’re unsure and you want to know, use (&self).div_mod(&other) and check whether the remainder is zero. If you want a function that panics if the division is not exact, use (&self).div_round(&other, RoundingMode::Exact).

Panics

Panics if other is zero. May panic if self is not divisible by other.

Examples
use malachite_base::num::arithmetic::traits::DivExact;
use malachite_nz::integer::Integer;
use std::str::FromStr;

// -123 * 456 = -56088
assert_eq!((&Integer::from(-56088)).div_exact(&Integer::from(456)), -123);

// -123456789000 * -987654321000 = 121932631112635269000000
assert_eq!(
    (&Integer::from_str("121932631112635269000000").unwrap())
            .div_exact(&Integer::from_str("-987654321000").unwrap()),
    -123456789000i64
);

Divides an Integer by another Integer, taking the first by reference and the second by value. The first Integer must be exactly divisible by the second. If it isn’t, this function may panic or return a meaningless result.

$$ f(x, y) = \frac{x}{y}. $$

If you are unsure whether the division will be exact, use &self / other instead. If you’re unsure and you want to know, use self.div_mod(other) and check whether the remainder is zero. If you want a function that panics if the division is not exact, use (&self).div_round(other, RoundingMode::Exact).

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero. May panic if self is not divisible by other.

Examples
use malachite_base::num::arithmetic::traits::DivExact;
use malachite_nz::integer::Integer;
use std::str::FromStr;

// -123 * 456 = -56088
assert_eq!((&Integer::from(-56088)).div_exact(Integer::from(456)), -123);

// -123456789000 * -987654321000 = 121932631112635269000000
assert_eq!(
    (&Integer::from_str("121932631112635269000000").unwrap())
            .div_exact(Integer::from_str("-987654321000").unwrap()),
    -123456789000i64
);

Divides an Integer by another Integer, taking both by value. The first Integer must be exactly divisible by the second. If it isn’t, this function may panic or return a meaningless result.

$$ f(x, y) = \frac{x}{y}. $$

If you are unsure whether the division will be exact, use self / other instead. If you’re unsure and you want to know, use self.div_mod(other) and check whether the remainder is zero. If you want a function that panics if the division is not exact, use self.div_round(other, RoundingMode::Exact).

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero. May panic if self is not divisible by other.

Examples
use malachite_base::num::arithmetic::traits::DivExact;
use malachite_nz::integer::Integer;
use std::str::FromStr;

// -123 * 456 = -56088
assert_eq!(Integer::from(-56088).div_exact(Integer::from(456)), -123);

// -123456789000 * -987654321000 = 121932631112635269000000
assert_eq!(
    Integer::from_str("121932631112635269000000").unwrap()
            .div_exact(Integer::from_str("-987654321000").unwrap()),
    -123456789000i64
);

Divides an Integer by another Integer in place, taking the Integer on the right-hand side by reference. The first Integer must be exactly divisible by the second. If it isn’t, this function may panic or return a meaningless result.

$$ x \gets \frac{x}{y}. $$

If you are unsure whether the division will be exact, use self /= &other instead. If you’re unsure and you want to know, use self.div_assign_mod(&other) and check whether the remainder is zero. If you want a function that panics if the division is not exact, use self.div_round_assign(&other, RoundingMode::Exact).

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero. May panic if self is not divisible by other.

Examples
use malachite_base::num::arithmetic::traits::DivExactAssign;
use malachite_nz::integer::Integer;
use std::str::FromStr;

// -123 * 456 = -56088
let mut x = Integer::from(-56088);
x.div_exact_assign(&Integer::from(456));
assert_eq!(x, -123);

// -123456789000 * -987654321000 = 121932631112635269000000
let mut x = Integer::from_str("121932631112635269000000").unwrap();
x.div_exact_assign(&Integer::from_str("-987654321000").unwrap());
assert_eq!(x, -123456789000i64);

Divides an Integer by another Integer in place, taking the Integer on the right-hand side by value. The first Integer must be exactly divisible by the second. If it isn’t, this function may panic or return a meaningless result.

$$ x \gets \frac{x}{y}. $$

If you are unsure whether the division will be exact, use self /= other instead. If you’re unsure and you want to know, use self.div_assign_mod(other) and check whether the remainder is zero. If you want a function that panics if the division is not exact, use self.div_round_assign(other, RoundingMode::Exact).

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero. May panic if self is not divisible by other.

Examples
use malachite_base::num::arithmetic::traits::DivExactAssign;
use malachite_nz::integer::Integer;
use std::str::FromStr;

// -123 * 456 = -56088
let mut x = Integer::from(-56088);
x.div_exact_assign(Integer::from(456));
assert_eq!(x, -123);

// -123456789000 * -987654321000 = 121932631112635269000000
let mut x = Integer::from_str("121932631112635269000000").unwrap();
x.div_exact_assign(Integer::from_str("-987654321000").unwrap());
assert_eq!(x, -123456789000i64);

Divides an Integer by another Integer, taking the first by value and the second by reference and returning the quotient and remainder. The quotient is rounded towards negative infinity, and the remainder has the same sign as the second Integer.

The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = \left ( \left \lfloor \frac{x}{y} \right \rfloor, \space x - y\left \lfloor \frac{x}{y} \right \rfloor \right ). $$

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
use malachite_base::num::arithmetic::traits::DivMod;
use malachite_base::strings::ToDebugString;
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
assert_eq!(Integer::from(23).div_mod(&Integer::from(10)).to_debug_string(), "(2, 3)");

// -3 * -10 + -7 = 23
assert_eq!(Integer::from(23).div_mod(&Integer::from(-10)).to_debug_string(), "(-3, -7)");

// -3 * 10 + 7 = -23
assert_eq!(Integer::from(-23).div_mod(&Integer::from(10)).to_debug_string(), "(-3, 7)");

// 2 * -10 + -3 = -23
assert_eq!(Integer::from(-23).div_mod(&Integer::from(-10)).to_debug_string(), "(2, -3)");

Divides an Integer by another Integer, taking both by reference and returning the quotient and remainder. The quotient is rounded towards negative infinity, and the remainder has the same sign as the second Integer.

The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = \left ( \left \lfloor \frac{x}{y} \right \rfloor, \space x - y\left \lfloor \frac{x}{y} \right \rfloor \right ). $$

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
use malachite_base::num::arithmetic::traits::DivMod;
use malachite_base::strings::ToDebugString;
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
assert_eq!((&Integer::from(23)).div_mod(&Integer::from(10)).to_debug_string(), "(2, 3)");

// -3 * -10 + -7 = 23
assert_eq!(
    (&Integer::from(23)).div_mod(&Integer::from(-10)).to_debug_string(),
    "(-3, -7)"
);

// -3 * 10 + 7 = -23
assert_eq!((&Integer::from(-23)).div_mod(&Integer::from(10)).to_debug_string(), "(-3, 7)");

// 2 * -10 + -3 = -23
assert_eq!(
    (&Integer::from(-23)).div_mod(&Integer::from(-10)).to_debug_string(),
    "(2, -3)"
);

Divides an Integer by another Integer, taking the first by reference and the second by value and returning the quotient and remainder. The quotient is rounded towards negative infinity, and the remainder has the same sign as the second Integer.

The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = \left ( \left \lfloor \frac{x}{y} \right \rfloor, \space x - y\left \lfloor \frac{x}{y} \right \rfloor \right ). $$

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
use malachite_base::num::arithmetic::traits::DivMod;
use malachite_base::strings::ToDebugString;
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
assert_eq!((&Integer::from(23)).div_mod(Integer::from(10)).to_debug_string(), "(2, 3)");

// -3 * -10 + -7 = 23
assert_eq!((&Integer::from(23)).div_mod(Integer::from(-10)).to_debug_string(), "(-3, -7)");

// -3 * 10 + 7 = -23
assert_eq!((&Integer::from(-23)).div_mod(Integer::from(10)).to_debug_string(), "(-3, 7)");

// 2 * -10 + -3 = -23
assert_eq!((&Integer::from(-23)).div_mod(Integer::from(-10)).to_debug_string(), "(2, -3)");

Divides an Integer by another Integer, taking both by value and returning the quotient and remainder. The quotient is rounded towards negative infinity, and the remainder has the same sign as the second Integer.

The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = \left ( \left \lfloor \frac{x}{y} \right \rfloor, \space x - y\left \lfloor \frac{x}{y} \right \rfloor \right ). $$

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
use malachite_base::num::arithmetic::traits::DivMod;
use malachite_base::strings::ToDebugString;
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
assert_eq!(Integer::from(23).div_mod(Integer::from(10)).to_debug_string(), "(2, 3)");

// -3 * -10 + -7 = 23
assert_eq!(Integer::from(23).div_mod(Integer::from(-10)).to_debug_string(), "(-3, -7)");

// -3 * 10 + 7 = -23
assert_eq!(Integer::from(-23).div_mod(Integer::from(10)).to_debug_string(), "(-3, 7)");

// 2 * -10 + -3 = -23
assert_eq!(Integer::from(-23).div_mod(Integer::from(-10)).to_debug_string(), "(2, -3)");

Divides an Integer by another Integer, taking the first by value and the second by reference and returning the quotient and remainder. The quotient is rounded towards zero and the remainder has the same sign as the first Integer.

The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = \left ( \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right | \right \rfloor, \space x - y \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right | \right \rfloor \right ). $$

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
use malachite_base::num::arithmetic::traits::DivRem;
use malachite_base::strings::ToDebugString;
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
assert_eq!(Integer::from(23).div_rem(&Integer::from(10)).to_debug_string(), "(2, 3)");

// -2 * -10 + 3 = 23
assert_eq!(Integer::from(23).div_rem(&Integer::from(-10)).to_debug_string(), "(-2, 3)");

// -2 * 10 + -3 = -23
assert_eq!(Integer::from(-23).div_rem(&Integer::from(10)).to_debug_string(), "(-2, -3)");

// 2 * -10 + -3 = -23
assert_eq!(Integer::from(-23).div_rem(&Integer::from(-10)).to_debug_string(), "(2, -3)");

Divides an Integer by another Integer, taking both by reference and returning the quotient and remainder. The quotient is rounded towards zero and the remainder has the same sign as the first Integer.

The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = \left ( \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right | \right \rfloor, \space x - y \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right | \right \rfloor \right ). $$

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
use malachite_base::num::arithmetic::traits::DivRem;
use malachite_base::strings::ToDebugString;
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
assert_eq!((&Integer::from(23)).div_rem(&Integer::from(10)).to_debug_string(), "(2, 3)");

// -2 * -10 + 3 = 23
assert_eq!((&Integer::from(23)).div_rem(&Integer::from(-10)).to_debug_string(), "(-2, 3)");

// -2 * 10 + -3 = -23
assert_eq!(
    (&Integer::from(-23)).div_rem(&Integer::from(10)).to_debug_string(),
    "(-2, -3)"
);

// 2 * -10 + -3 = -23
assert_eq!(
    (&Integer::from(-23)).div_rem(&Integer::from(-10)).to_debug_string(),
    "(2, -3)"
);

Divides an Integer by another Integer, taking the first by reference and the second by value and returning the quotient and remainder. The quotient is rounded towards zero and the remainder has the same sign as the first Integer.

The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = \left ( \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right | \right \rfloor, \space x - y \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right | \right \rfloor \right ). $$

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
use malachite_base::num::arithmetic::traits::DivRem;
use malachite_base::strings::ToDebugString;
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
assert_eq!((&Integer::from(23)).div_rem(Integer::from(10)).to_debug_string(), "(2, 3)");

// -2 * -10 + 3 = 23
assert_eq!((&Integer::from(23)).div_rem(Integer::from(-10)).to_debug_string(), "(-2, 3)");

// -2 * 10 + -3 = -23
assert_eq!((&Integer::from(-23)).div_rem(Integer::from(10)).to_debug_string(), "(-2, -3)");

// 2 * -10 + -3 = -23
assert_eq!((&Integer::from(-23)).div_rem(Integer::from(-10)).to_debug_string(), "(2, -3)");

Divides an Integer by another Integer, taking both by value and returning the quotient and remainder. The quotient is rounded towards zero and the remainder has the same sign as the first Integer.

The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = \left ( \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right | \right \rfloor, \space x - y \operatorname{sgn}(xy) \left \lfloor \left | \frac{x}{y} \right | \right \rfloor \right ). $$

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
use malachite_base::num::arithmetic::traits::DivRem;
use malachite_base::strings::ToDebugString;
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
assert_eq!(Integer::from(23).div_rem(Integer::from(10)).to_debug_string(), "(2, 3)");

// -2 * -10 + 3 = 23
assert_eq!(Integer::from(23).div_rem(Integer::from(-10)).to_debug_string(), "(-2, 3)");

// -2 * 10 + -3 = -23
assert_eq!(Integer::from(-23).div_rem(Integer::from(10)).to_debug_string(), "(-2, -3)");

// 2 * -10 + -3 = -23
assert_eq!(Integer::from(-23).div_rem(Integer::from(-10)).to_debug_string(), "(2, -3)");

Divides an Integer by another Integer, taking the first by value and the second by reference and rounding according to a specified rounding mode.

Let $q = \frac{x}{y}$:

$$ f(x, y, \mathrm{Down}) = \operatorname{sgn}(q) \lfloor |q| \rfloor. $$

$$ f(x, y, \mathrm{Up}) = \operatorname{sgn}(q) \lceil |q| \rceil. $$

$$ f(x, y, \mathrm{Floor}) = \lfloor q \rfloor. $$

$$ f(x, y, \mathrm{Ceiling}) = \lceil q \rceil. $$

$$ f(x, y, \mathrm{Nearest}) = \begin{cases} \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor < \frac{1}{2}, \\ \lceil q \rceil & q - \lfloor q \rfloor > \frac{1}{2}, \\ \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is even}, \\ \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is odd.} \end{cases} $$

$f(x, y, \mathrm{Exact}) = q$, but panics if $q \notin \Z$.

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero, or if rm is Exact but self is not divisible by other.

Examples
use malachite_base::num::arithmetic::traits::{DivRound, Pow};
use malachite_base::rounding_modes::RoundingMode;
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(-10).div_round(&Integer::from(4), RoundingMode::Down), -2);
assert_eq!(
    (-Integer::from(10u32).pow(12)).div_round(&Integer::from(3), RoundingMode::Floor),
    -333333333334i64
);
assert_eq!(Integer::from(-10).div_round(&Integer::from(4), RoundingMode::Up), -3);
assert_eq!(
    (-Integer::from(10u32).pow(12)).div_round(&Integer::from(3), RoundingMode::Ceiling),
    -333333333333i64
);
assert_eq!(Integer::from(-10).div_round(&Integer::from(5), RoundingMode::Exact), -2);
assert_eq!(Integer::from(-10).div_round(&Integer::from(3), RoundingMode::Nearest), -3);
assert_eq!(Integer::from(-20).div_round(&Integer::from(3), RoundingMode::Nearest), -7);
assert_eq!(Integer::from(-10).div_round(&Integer::from(4), RoundingMode::Nearest), -2);
assert_eq!(Integer::from(-14).div_round(&Integer::from(4), RoundingMode::Nearest), -4);

assert_eq!(Integer::from(-10).div_round(&Integer::from(-4), RoundingMode::Down), 2);
assert_eq!(
    (-Integer::from(10u32).pow(12)).div_round(&Integer::from(-3), RoundingMode::Floor),
    333333333333i64
);
assert_eq!(Integer::from(-10).div_round(&Integer::from(-4), RoundingMode::Up), 3);
assert_eq!(
    (-Integer::from(10u32).pow(12)).div_round(&Integer::from(-3), RoundingMode::Ceiling),
    333333333334i64
);
assert_eq!(Integer::from(-10).div_round(&Integer::from(-5), RoundingMode::Exact), 2);
assert_eq!(Integer::from(-10).div_round(&Integer::from(-3), RoundingMode::Nearest), 3);
assert_eq!(Integer::from(-20).div_round(&Integer::from(-3), RoundingMode::Nearest), 7);
assert_eq!(Integer::from(-10).div_round(&Integer::from(-4), RoundingMode::Nearest), 2);
assert_eq!(Integer::from(-14).div_round(&Integer::from(-4), RoundingMode::Nearest), 4);

Divides an Integer by another Integer, taking both by reference and rounding according to a specified rounding mode.

Let $q = \frac{x}{y}$:

$$ f(x, y, \mathrm{Down}) = \operatorname{sgn}(q) \lfloor |q| \rfloor. $$

$$ f(x, y, \mathrm{Up}) = \operatorname{sgn}(q) \lceil |q| \rceil. $$

$$ f(x, y, \mathrm{Floor}) = \lfloor q \rfloor. $$

$$ f(x, y, \mathrm{Ceiling}) = \lceil q \rceil. $$

$$ f(x, y, \mathrm{Nearest}) = \begin{cases} \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor < \frac{1}{2}, \\ \lceil q \rceil & q - \lfloor q \rfloor > \frac{1}{2}, \\ \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is even}, \\ \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is odd.} \end{cases} $$

$f(x, y, \mathrm{Exact}) = q$, but panics if $q \notin \Z$.

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero, or if rm is Exact but self is not divisible by other.

Examples
use malachite_base::num::arithmetic::traits::{DivRound, Pow};
use malachite_base::rounding_modes::RoundingMode;
use malachite_nz::integer::Integer;

assert_eq!((&Integer::from(-10)).div_round(&Integer::from(4), RoundingMode::Down), -2);
assert_eq!(
    (&-Integer::from(10u32).pow(12)).div_round(&Integer::from(3), RoundingMode::Floor),
    -333333333334i64
);
assert_eq!(Integer::from(-10).div_round(&Integer::from(4), RoundingMode::Up), -3);
assert_eq!(
    (&-Integer::from(10u32).pow(12)).div_round(&Integer::from(3), RoundingMode::Ceiling),
    -333333333333i64
);
assert_eq!((&Integer::from(-10)).div_round(&Integer::from(5), RoundingMode::Exact), -2);
assert_eq!((&Integer::from(-10)).div_round(&Integer::from(3), RoundingMode::Nearest), -3);
assert_eq!((&Integer::from(-20)).div_round(&Integer::from(3), RoundingMode::Nearest), -7);
assert_eq!((&Integer::from(-10)).div_round(&Integer::from(4), RoundingMode::Nearest), -2);
assert_eq!((&Integer::from(-14)).div_round(&Integer::from(4), RoundingMode::Nearest), -4);

assert_eq!((&Integer::from(-10)).div_round(&Integer::from(-4), RoundingMode::Down), 2);
assert_eq!(
    (&-Integer::from(10u32).pow(12)).div_round(&Integer::from(-3), RoundingMode::Floor),
    333333333333i64
);
assert_eq!(Integer::from(-10).div_round(&Integer::from(-4), RoundingMode::Up), 3);
assert_eq!(
    (&-Integer::from(10u32).pow(12)).div_round(&Integer::from(-3), RoundingMode::Ceiling),
    333333333334i64
);
assert_eq!((&Integer::from(-10)).div_round(&Integer::from(-5), RoundingMode::Exact), 2);
assert_eq!((&Integer::from(-10)).div_round(&Integer::from(-3), RoundingMode::Nearest), 3);
assert_eq!((&Integer::from(-20)).div_round(&Integer::from(-3), RoundingMode::Nearest), 7);
assert_eq!((&Integer::from(-10)).div_round(&Integer::from(-4), RoundingMode::Nearest), 2);
assert_eq!((&Integer::from(-14)).div_round(&Integer::from(-4), RoundingMode::Nearest), 4);

Divides an Integer by another Integer, taking the first by reference and the second by value and rounding according to a specified rounding mode.

Let $q = \frac{x}{y}$:

$$ f(x, y, \mathrm{Down}) = \operatorname{sgn}(q) \lfloor |q| \rfloor. $$

$$ f(x, y, \mathrm{Up}) = \operatorname{sgn}(q) \lceil |q| \rceil. $$

$$ f(x, y, \mathrm{Floor}) = \lfloor q \rfloor. $$

$$ f(x, y, \mathrm{Ceiling}) = \lceil q \rceil. $$

$$ f(x, y, \mathrm{Nearest}) = \begin{cases} \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor < \frac{1}{2}, \\ \lceil q \rceil & q - \lfloor q \rfloor > \frac{1}{2}, \\ \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is even}, \\ \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is odd.} \end{cases} $$

$f(x, y, \mathrm{Exact}) = q$, but panics if $q \notin \Z$.

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero, or if rm is Exact but self is not divisible by other.

Examples
use malachite_base::num::arithmetic::traits::{DivRound, Pow};
use malachite_base::rounding_modes::RoundingMode;
use malachite_nz::integer::Integer;

assert_eq!((&Integer::from(-10)).div_round(Integer::from(4), RoundingMode::Down), -2);
assert_eq!(
    (&-Integer::from(10u32).pow(12)).div_round(Integer::from(3), RoundingMode::Floor),
    -333333333334i64
);
assert_eq!(Integer::from(-10).div_round(Integer::from(4), RoundingMode::Up), -3);
assert_eq!(
    (&-Integer::from(10u32).pow(12)).div_round(Integer::from(3), RoundingMode::Ceiling),
    -333333333333i64
);
assert_eq!((&Integer::from(-10)).div_round(Integer::from(5), RoundingMode::Exact), -2);
assert_eq!((&Integer::from(-10)).div_round(Integer::from(3), RoundingMode::Nearest), -3);
assert_eq!((&Integer::from(-20)).div_round(Integer::from(3), RoundingMode::Nearest), -7);
assert_eq!((&Integer::from(-10)).div_round(Integer::from(4), RoundingMode::Nearest), -2);
assert_eq!((&Integer::from(-14)).div_round(Integer::from(4), RoundingMode::Nearest), -4);

assert_eq!((&Integer::from(-10)).div_round(Integer::from(-4), RoundingMode::Down), 2);
assert_eq!(
    (&-Integer::from(10u32).pow(12)).div_round(Integer::from(-3), RoundingMode::Floor),
    333333333333i64
);
assert_eq!(Integer::from(-10).div_round(Integer::from(-4), RoundingMode::Up), 3);
assert_eq!(
    (&-Integer::from(10u32).pow(12)).div_round(Integer::from(-3), RoundingMode::Ceiling),
    333333333334i64
);
assert_eq!((&Integer::from(-10)).div_round(Integer::from(-5), RoundingMode::Exact), 2);
assert_eq!((&Integer::from(-10)).div_round(Integer::from(-3), RoundingMode::Nearest), 3);
assert_eq!((&Integer::from(-20)).div_round(Integer::from(-3), RoundingMode::Nearest), 7);
assert_eq!((&Integer::from(-10)).div_round(Integer::from(-4), RoundingMode::Nearest), 2);
assert_eq!((&Integer::from(-14)).div_round(Integer::from(-4), RoundingMode::Nearest), 4);

Divides an Integer by another Integer, taking both by value and rounding according to a specified rounding mode.

Let $q = \frac{x}{y}$:

$$ f(x, y, \mathrm{Down}) = \operatorname{sgn}(q) \lfloor |q| \rfloor. $$

$$ f(x, y, \mathrm{Up}) = \operatorname{sgn}(q) \lceil |q| \rceil. $$

$$ f(x, y, \mathrm{Floor}) = \lfloor q \rfloor. $$

$$ f(x, y, \mathrm{Ceiling}) = \lceil q \rceil. $$

$$ f(x, y, \mathrm{Nearest}) = \begin{cases} \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor < \frac{1}{2}, \\ \lceil q \rceil & q - \lfloor q \rfloor > \frac{1}{2}, \\ \lfloor q \rfloor & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is even}, \\ \lceil q \rceil & \text{if} \quad q - \lfloor q \rfloor = \frac{1}{2} \ \text{and} \ \lfloor q \rfloor \ \text{is odd.} \end{cases} $$

$f(x, y, \mathrm{Exact}) = q$, but panics if $q \notin \Z$.

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero, or if rm is Exact but self is not divisible by other.

Examples
use malachite_base::num::arithmetic::traits::{DivRound, Pow};
use malachite_base::rounding_modes::RoundingMode;
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(-10).div_round(Integer::from(4), RoundingMode::Down), -2);
assert_eq!(
    (-Integer::from(10u32).pow(12)).div_round(Integer::from(3), RoundingMode::Floor),
    -333333333334i64
);
assert_eq!(Integer::from(-10).div_round(Integer::from(4), RoundingMode::Up), -3);
assert_eq!(
    (-Integer::from(10u32).pow(12)).div_round(Integer::from(3), RoundingMode::Ceiling),
    -333333333333i64
);
assert_eq!(Integer::from(-10).div_round(Integer::from(5), RoundingMode::Exact), -2);
assert_eq!(Integer::from(-10).div_round(Integer::from(3), RoundingMode::Nearest), -3);
assert_eq!(Integer::from(-20).div_round(Integer::from(3), RoundingMode::Nearest), -7);
assert_eq!(Integer::from(-10).div_round(Integer::from(4), RoundingMode::Nearest), -2);
assert_eq!(Integer::from(-14).div_round(Integer::from(4), RoundingMode::Nearest), -4);

assert_eq!(Integer::from(-10).div_round(Integer::from(-4), RoundingMode::Down), 2);
assert_eq!(
    (-Integer::from(10u32).pow(12)).div_round(Integer::from(-3), RoundingMode::Floor),
    333333333333i64
);
assert_eq!(Integer::from(-10).div_round(Integer::from(-4), RoundingMode::Up), 3);
assert_eq!(
    (-Integer::from(10u32).pow(12)).div_round(Integer::from(-3), RoundingMode::Ceiling),
    333333333334i64
);
assert_eq!(Integer::from(-10).div_round(Integer::from(-5), RoundingMode::Exact), 2);
assert_eq!(Integer::from(-10).div_round(Integer::from(-3), RoundingMode::Nearest), 3);
assert_eq!(Integer::from(-20).div_round(Integer::from(-3), RoundingMode::Nearest), 7);
assert_eq!(Integer::from(-10).div_round(Integer::from(-4), RoundingMode::Nearest), 2);
assert_eq!(Integer::from(-14).div_round(Integer::from(-4), RoundingMode::Nearest), 4);

Divides an Integer by another Integer in place, taking the Integer on the right-hand side by reference and rounding according to a specified rounding mode.

See the DivRound documentation for details.

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero, or if rm is Exact but self is not divisible by other.

Examples
use malachite_base::num::arithmetic::traits::{DivRoundAssign, Pow};
use malachite_base::rounding_modes::RoundingMode;
use malachite_nz::integer::Integer;

let mut n = Integer::from(-10);
n.div_round_assign(&Integer::from(4), RoundingMode::Down);
assert_eq!(n, -2);

let mut n = -Integer::from(10u32).pow(12);
n.div_round_assign(&Integer::from(3), RoundingMode::Floor);
assert_eq!(n, -333333333334i64);

let mut n = Integer::from(-10);
n.div_round_assign(&Integer::from(4), RoundingMode::Up);
assert_eq!(n, -3);

let mut n = -Integer::from(10u32).pow(12);
n.div_round_assign(&Integer::from(3), RoundingMode::Ceiling);
assert_eq!(n, -333333333333i64);

let mut n = Integer::from(-10);
n.div_round_assign(&Integer::from(5), RoundingMode::Exact);
assert_eq!(n, -2);

let mut n = Integer::from(-10);
n.div_round_assign(Integer::from(3), RoundingMode::Nearest);
assert_eq!(n, -3);

let mut n = Integer::from(-20);
n.div_round_assign(&Integer::from(3), RoundingMode::Nearest);
assert_eq!(n, -7);

let mut n = Integer::from(-10);
n.div_round_assign(&Integer::from(4), RoundingMode::Nearest);
assert_eq!(n, -2);

let mut n = Integer::from(-14);
n.div_round_assign(&Integer::from(4), RoundingMode::Nearest);
assert_eq!(n, -4);

let mut n = Integer::from(-10);
n.div_round_assign(&Integer::from(-4), RoundingMode::Down);
assert_eq!(n, 2);

let mut n = -Integer::from(10u32).pow(12);
n.div_round_assign(&Integer::from(-3), RoundingMode::Floor);
assert_eq!(n, 333333333333i64);

let mut n = Integer::from(-10);
n.div_round_assign(&Integer::from(-4), RoundingMode::Up);
assert_eq!(n, 3);

let mut n = -Integer::from(10u32).pow(12);
n.div_round_assign(&Integer::from(-3), RoundingMode::Ceiling);
assert_eq!(n, 333333333334i64);

let mut n = Integer::from(-10);
n.div_round_assign(&Integer::from(-5), RoundingMode::Exact);
assert_eq!(n, 2);

let mut n = Integer::from(-10);
n.div_round_assign(&Integer::from(-3), RoundingMode::Nearest);
assert_eq!(n, 3);

let mut n = Integer::from(-20);
n.div_round_assign(&Integer::from(-3), RoundingMode::Nearest);
assert_eq!(n, 7);

let mut n = Integer::from(-10);
n.div_round_assign(&Integer::from(-4), RoundingMode::Nearest);
assert_eq!(n, 2);

let mut n = Integer::from(-14);
n.div_round_assign(&Integer::from(-4), RoundingMode::Nearest);
assert_eq!(n, 4);

Divides an Integer by another Integer in place, taking the Integer on the right-hand side by value and rounding according to a specified rounding mode.

See the DivRound documentation for details.

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero, or if rm is Exact but self is not divisible by other.

Examples
use malachite_base::num::arithmetic::traits::{DivRoundAssign, Pow};
use malachite_base::rounding_modes::RoundingMode;
use malachite_nz::integer::Integer;

let mut n = Integer::from(-10);
n.div_round_assign(Integer::from(4), RoundingMode::Down);
assert_eq!(n, -2);

let mut n = -Integer::from(10u32).pow(12);
n.div_round_assign(Integer::from(3), RoundingMode::Floor);
assert_eq!(n, -333333333334i64);

let mut n = Integer::from(-10);
n.div_round_assign(Integer::from(4), RoundingMode::Up);
assert_eq!(n, -3);

let mut n = -Integer::from(10u32).pow(12);
n.div_round_assign(Integer::from(3), RoundingMode::Ceiling);
assert_eq!(n, -333333333333i64);

let mut n = Integer::from(-10);
n.div_round_assign(Integer::from(5), RoundingMode::Exact);
assert_eq!(n, -2);

let mut n = Integer::from(-10);
n.div_round_assign(Integer::from(3), RoundingMode::Nearest);
assert_eq!(n, -3);

let mut n = Integer::from(-20);
n.div_round_assign(Integer::from(3), RoundingMode::Nearest);
assert_eq!(n, -7);

let mut n = Integer::from(-10);
n.div_round_assign(Integer::from(4), RoundingMode::Nearest);
assert_eq!(n, -2);

let mut n = Integer::from(-14);
n.div_round_assign(Integer::from(4), RoundingMode::Nearest);
assert_eq!(n, -4);

let mut n = Integer::from(-10);
n.div_round_assign(Integer::from(-4), RoundingMode::Down);
assert_eq!(n, 2);

let mut n = -Integer::from(10u32).pow(12);
n.div_round_assign(Integer::from(-3), RoundingMode::Floor);
assert_eq!(n, 333333333333i64);

let mut n = Integer::from(-10);
n.div_round_assign(Integer::from(-4), RoundingMode::Up);
assert_eq!(n, 3);

let mut n = -Integer::from(10u32).pow(12);
n.div_round_assign(Integer::from(-3), RoundingMode::Ceiling);
assert_eq!(n, 333333333334i64);

let mut n = Integer::from(-10);
n.div_round_assign(Integer::from(-5), RoundingMode::Exact);
assert_eq!(n, 2);

let mut n = Integer::from(-10);
n.div_round_assign(Integer::from(-3), RoundingMode::Nearest);
assert_eq!(n, 3);

let mut n = Integer::from(-20);
n.div_round_assign(Integer::from(-3), RoundingMode::Nearest);
assert_eq!(n, 7);

let mut n = Integer::from(-10);
n.div_round_assign(Integer::from(-4), RoundingMode::Nearest);
assert_eq!(n, 2);

let mut n = Integer::from(-14);
n.div_round_assign(Integer::from(-4), RoundingMode::Nearest);
assert_eq!(n, 4);

Returns whether an Integer is divisible by another Integer; in other words, whether the first is a multiple of the second. The first Integer is taken by value and the second by reference.

This means that zero is divisible by any Integer, including zero; but a nonzero Integer is never divisible by zero.

It’s more efficient to use this function than to compute the remainder and check whether it’s zero.

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples
use malachite_base::num::arithmetic::traits::DivisibleBy;
use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;
use std::str::FromStr;

assert_eq!(Integer::ZERO.divisible_by(&Integer::ZERO), true);
assert_eq!(Integer::from(-100).divisible_by(&Integer::from(-3)), false);
assert_eq!(Integer::from(102).divisible_by(&Integer::from(-3)), true);
assert_eq!(
    Integer::from_str("-1000000000000000000000000").unwrap()
            .divisible_by(&Integer::from_str("1000000000000").unwrap()),
    true
);

Returns whether an Integer is divisible by another Integer; in other words, whether the first is a multiple of the second. Both Integers are taken by reference.

This means that zero is divisible by any Integer, including zero; but a nonzero Integer is never divisible by zero.

It’s more efficient to use this function than to compute the remainder and check whether it’s zero.

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples
use malachite_base::num::arithmetic::traits::DivisibleBy;
use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;
use std::str::FromStr;

assert_eq!((&Integer::ZERO).divisible_by(&Integer::ZERO), true);
assert_eq!((&Integer::from(-100)).divisible_by(&Integer::from(-3)), false);
assert_eq!((&Integer::from(102)).divisible_by(&Integer::from(-3)), true);
assert_eq!(
    (&Integer::from_str("-1000000000000000000000000").unwrap())
            .divisible_by(&Integer::from_str("1000000000000").unwrap()),
    true
);

Returns whether an Integer is divisible by another Integer; in other words, whether the first is a multiple of the second. The first Integer is taken by reference and the second by value.

This means that zero is divisible by any Integer, including zero; but a nonzero Integer is never divisible by zero.

It’s more efficient to use this function than to compute the remainder and check whether it’s zero.

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples
use malachite_base::num::arithmetic::traits::DivisibleBy;
use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;
use std::str::FromStr;

assert_eq!((&Integer::ZERO).divisible_by(Integer::ZERO), true);
assert_eq!((&Integer::from(-100)).divisible_by(Integer::from(-3)), false);
assert_eq!((&Integer::from(102)).divisible_by(Integer::from(-3)), true);
assert_eq!(
    (&Integer::from_str("-1000000000000000000000000").unwrap())
            .divisible_by(Integer::from_str("1000000000000").unwrap()),
    true
);

Returns whether an Integer is divisible by another Integer; in other words, whether the first is a multiple of the second. Both Integers are taken by value.

This means that zero is divisible by any Integer, including zero; but a nonzero Integer is never divisible by zero.

It’s more efficient to use this function than to compute the remainder and check whether it’s zero.

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Examples
use malachite_base::num::arithmetic::traits::DivisibleBy;
use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;
use std::str::FromStr;

assert_eq!(Integer::ZERO.divisible_by(Integer::ZERO), true);
assert_eq!(Integer::from(-100).divisible_by(Integer::from(-3)), false);
assert_eq!(Integer::from(102).divisible_by(Integer::from(-3)), true);
assert_eq!(
    Integer::from_str("-1000000000000000000000000").unwrap()
            .divisible_by(Integer::from_str("1000000000000").unwrap()),
    true
);

Returns whether an Integer is divisible by $2^k$.

$f(x, k) = (2^k|x)$.

$f(x, k) = (\exists n \in \N : \ x = n2^k)$.

If self is 0, the result is always true; otherwise, it is equivalent to self.trailing_zeros().unwrap() <= pow, but more efficient.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is min(pow, self.significant_bits()).

Examples
use malachite_base::num::arithmetic::traits::{DivisibleByPowerOf2, Pow};
use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;

assert_eq!(Integer::ZERO.divisible_by_power_of_2(100), true);
assert_eq!(Integer::from(-100).divisible_by_power_of_2(2), true);
assert_eq!(Integer::from(100u32).divisible_by_power_of_2(3), false);
assert_eq!((-Integer::from(10u32).pow(12)).divisible_by_power_of_2(12), true);
assert_eq!((-Integer::from(10u32).pow(12)).divisible_by_power_of_2(13), false);

Returns whether an Integer is equivalent to another Integer modulo a Natural; that is, whether the difference between the two Integers is a multiple of the Natural. The first number is taken by value and the second and third by reference.

Two Integers are equal to each other modulo 0 iff they are equal.

$f(x, y, m) = (x \equiv y \mod m)$.

$f(x, y, m) = (\exists k \in \Z : x - y = km)$.

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
use malachite_base::num::arithmetic::traits::EqMod;
use malachite_nz::integer::Integer;
use malachite_nz::natural::Natural;
use std::str::FromStr;

assert_eq!(
    Integer::from(123).eq_mod(&Integer::from(223), &Natural::from(100u32)),
    true
);
assert_eq!(
    Integer::from_str("1000000987654").unwrap().eq_mod(
        &Integer::from_str("-999999012346").unwrap(),
        &Natural::from_str("1000000000000").unwrap()
    ),
    true
);
assert_eq!(
    Integer::from_str("1000000987654").unwrap().eq_mod(
        &Integer::from_str("2000000987655").unwrap(),
        &Natural::from_str("1000000000000").unwrap()
    ),
    false
);

Returns whether an Integer is equivalent to another Integer modulo a Natural; that is, whether the difference between the two Integers is a multiple of the Natural. The first and third numbers are taken by value and the second by reference.

Two Integers are equal to each other modulo 0 iff they are equal.

$f(x, y, m) = (x \equiv y \mod m)$.

$f(x, y, m) = (\exists k \in \Z : x - y = km)$.

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
use malachite_base::num::arithmetic::traits::EqMod;
use malachite_nz::integer::Integer;
use malachite_nz::natural::Natural;
use std::str::FromStr;

assert_eq!(
    Integer::from(123).eq_mod(&Integer::from(223), Natural::from(100u32)),
    true
);
assert_eq!(
    Integer::from_str("1000000987654").unwrap().eq_mod(
        &Integer::from_str("-999999012346").unwrap(),
        Natural::from_str("1000000000000").unwrap()
    ),
    true
);
assert_eq!(
    Integer::from_str("1000000987654").unwrap().eq_mod(
        &Integer::from_str("2000000987655").unwrap(),
        Natural::from_str("1000000000000").unwrap()
    ),
    false
);

Returns whether an Integer is equivalent to another Integer modulo a Natural; that is, whether the difference between the two Integers is a multiple of the Natural. All three numbers are taken by reference.

Two Integers are equal to each other modulo 0 iff they are equal.

$f(x, y, m) = (x \equiv y \mod m)$.

$f(x, y, m) = (\exists k \in \Z : x - y = km)$.

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
use malachite_base::num::arithmetic::traits::EqMod;
use malachite_nz::integer::Integer;
use malachite_nz::natural::Natural;
use std::str::FromStr;

assert_eq!(
    (&Integer::from(123)).eq_mod(&Integer::from(223), &Natural::from(100u32)),
    true
);
assert_eq!(
    (&Integer::from_str("1000000987654").unwrap()).eq_mod(
        &Integer::from_str("-999999012346").unwrap(),
        &Natural::from_str("1000000000000").unwrap()
    ),
    true
);
assert_eq!(
    (&Integer::from_str("1000000987654").unwrap()).eq_mod(
        &Integer::from_str("2000000987655").unwrap(),
        &Natural::from_str("1000000000000").unwrap()
    ),
    false
);

Returns whether an Integer is equivalent to another Integer modulo a Natural; that is, whether the difference between the two Integers is a multiple of the Natural. The first two numbers are taken by reference and the third by value.

Two Integers are equal to each other modulo 0 iff they are equal.

$f(x, y, m) = (x \equiv y \mod m)$.

$f(x, y, m) = (\exists k \in \Z : x - y = km)$.

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
use malachite_base::num::arithmetic::traits::EqMod;
use malachite_nz::integer::Integer;
use malachite_nz::natural::Natural;
use std::str::FromStr;

assert_eq!(
    (&Integer::from(123)).eq_mod(&Integer::from(223), Natural::from(100u32)),
    true
);
assert_eq!(
    (&Integer::from_str("1000000987654").unwrap()).eq_mod(
        &Integer::from_str("-999999012346").unwrap(),
        Natural::from_str("1000000000000").unwrap()
    ),
    true
);
assert_eq!(
    (&Integer::from_str("1000000987654").unwrap()).eq_mod(
        &Integer::from_str("2000000987655").unwrap(),
        Natural::from_str("1000000000000").unwrap()
    ),
    false
);

Returns whether an Integer is equivalent to another Integer modulo a Natural; that is, whether the difference between the two Integers is a multiple of the Natural. The first two numbers are taken by value and the third by reference.

Two Integers are equal to each other modulo 0 iff they are equal.

$f(x, y, m) = (x \equiv y \mod m)$.

$f(x, y, m) = (\exists k \in \Z : x - y = km)$.

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
use malachite_base::num::arithmetic::traits::EqMod;
use malachite_nz::integer::Integer;
use malachite_nz::natural::Natural;
use std::str::FromStr;

assert_eq!(
    Integer::from(123).eq_mod(Integer::from(223), &Natural::from(100u32)),
    true
);
assert_eq!(
    Integer::from_str("1000000987654").unwrap().eq_mod(
        Integer::from_str("-999999012346").unwrap(),
        &Natural::from_str("1000000000000").unwrap()
    ),
    true
);
assert_eq!(
    Integer::from_str("1000000987654").unwrap().eq_mod(
        Integer::from_str("2000000987655").unwrap(),
        &Natural::from_str("1000000000000").unwrap()
    ),
    false
);

Returns whether an Integer is equivalent to another Integer modulo a Natural; that is, whether the difference between the two Integers is a multiple of the Natural. The first and third numbers are taken by reference and the third by value.

Two Integers are equal to each other modulo 0 iff they are equal.

$f(x, y, m) = (x \equiv y \mod m)$.

$f(x, y, m) = (\exists k \in \Z : x - y = km)$.

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
use malachite_base::num::arithmetic::traits::EqMod;
use malachite_nz::integer::Integer;
use malachite_nz::natural::Natural;
use std::str::FromStr;

assert_eq!(
    (&Integer::from(123)).eq_mod(Integer::from(223), &Natural::from(100u32)),
    true
);
assert_eq!(
    (&Integer::from_str("1000000987654").unwrap()).eq_mod(
        Integer::from_str("-999999012346").unwrap(),
        &Natural::from_str("1000000000000").unwrap()
    ),
    true
);
assert_eq!(
    (&Integer::from_str("1000000987654").unwrap()).eq_mod(
        Integer::from_str("2000000987655").unwrap(),
        &Natural::from_str("1000000000000").unwrap()
    ),
    false
);

Returns whether an Integer is equivalent to another Integer modulo a Natural; that is, whether the difference between the two Integers is a multiple of the Natural. The first number is taken by reference and the second and third by value.

Two Integers are equal to each other modulo 0 iff they are equal.

$f(x, y, m) = (x \equiv y \mod m)$.

$f(x, y, m) = (\exists k \in \Z : x - y = km)$.

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
use malachite_base::num::arithmetic::traits::EqMod;
use malachite_nz::integer::Integer;
use malachite_nz::natural::Natural;
use std::str::FromStr;

assert_eq!(
    (&Integer::from(123)).eq_mod(Integer::from(223), Natural::from(100u32)),
    true
);
assert_eq!(
    (&Integer::from_str("1000000987654").unwrap()).eq_mod(
        Integer::from_str("-999999012346").unwrap(),
        Natural::from_str("1000000000000").unwrap()
    ),
    true
);
assert_eq!(
    (&Integer::from_str("1000000987654").unwrap()).eq_mod(
        Integer::from_str("2000000987655").unwrap(),
        Natural::from_str("1000000000000").unwrap()
    ),
    false
);

Returns whether an Integer is equivalent to another Integer modulo a Natural; that is, whether the difference between the two Integers is a multiple of the Natural. All three numbers are taken by value.

Two Integers are equal to each other modulo 0 iff they are equal.

$f(x, y, m) = (x \equiv y \mod m)$.

$f(x, y, m) = (\exists k \in \Z : x - y = km)$.

Worst-case complexity

$T(n) = O(n \log n \log \log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
use malachite_base::num::arithmetic::traits::EqMod;
use malachite_nz::integer::Integer;
use malachite_nz::natural::Natural;
use std::str::FromStr;

assert_eq!(
    Integer::from(123).eq_mod(Integer::from(223), Natural::from(100u32)),
    true
);
assert_eq!(
    Integer::from_str("1000000987654").unwrap().eq_mod(
        Integer::from_str("-999999012346").unwrap(),
        Natural::from_str("1000000000000").unwrap()
    ),
    true
);
assert_eq!(
    Integer::from_str("1000000987654").unwrap().eq_mod(
        Integer::from_str("2000000987655").unwrap(),
        Natural::from_str("1000000000000").unwrap()
    ),
    false
);

Returns whether one Integer is equal to another modulo $2^k$; that is, whether their $k$ least-significant bits (in two’s complement) are equal.

$f(x, y, k) = (x \equiv y \mod 2^k)$.

$f(x, y, k) = (\exists n \in \Z : x - y = n2^k)$.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is min(pow, self.significant_bits(), other.significant_bits()).

Examples
use malachite_base::num::arithmetic::traits::EqModPowerOf2;
use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;

assert_eq!(Integer::ZERO.eq_mod_power_of_2(&Integer::from(-256), 8), true);
assert_eq!(Integer::from(-0b1101).eq_mod_power_of_2(&Integer::from(0b11011), 3), true);
assert_eq!(Integer::from(-0b1101).eq_mod_power_of_2(&Integer::from(0b11011), 4), false);

Computes the GCD (greatest common divisor) of two Integers $a$ and $b$, and also the coefficients $x$ and $y$ in Bézout’s identity $ax+by=\gcd(a,b)$. Both Integers are taken by reference.

The are infinitely many $x$, $y$ that satisfy the identity for any $a$, $b$, so the full specification is more detailed:

  • $f(0, 0) = (0, 0, 0)$.
  • $f(a, ak) = (a, 1, 0)$ if $a > 0$ and $k \neq 1$.
  • $f(a, ak) = (-a, -1, 0)$ if $a < 0$ and $k \neq 1$.
  • $f(bk, b) = (b, 0, 1)$ if $b > 0$.
  • $f(bk, b) = (-b, 0, -1)$ if $b < 0$.
  • $f(a, b) = (g, x, y)$ if $a \neq 0$ and $b \neq 0$ and $\gcd(a, b) \neq \min(|a|, |b|)$, where $g = \gcd(a, b) \geq 0$, $ax + by = g$, $x \leq \lfloor b/g \rfloor$, and $y \leq \lfloor a/g \rfloor$.
Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
use malachite_base::num::arithmetic::traits::ExtendedGcd;
use malachite_base::strings::ToDebugString;
use malachite_nz::integer::Integer;

assert_eq!(
    (&Integer::from(3)).extended_gcd(&Integer::from(5)).to_debug_string(),
    "(1, 2, -1)"
);
assert_eq!(
    (&Integer::from(240)).extended_gcd(&Integer::from(46)).to_debug_string(),
    "(2, -9, 47)"
);
assert_eq!(
    (&Integer::from(-111)).extended_gcd(&Integer::from(300)).to_debug_string(),
    "(3, 27, 10)"
);

Computes the GCD (greatest common divisor) of two Integers $a$ and $b$, and also the coefficients $x$ and $y$ in Bézout’s identity $ax+by=\gcd(a,b)$. The first Integer is taken by value and the second by reference.

The are infinitely many $x$, $y$ that satisfy the identity for any $a$, $b$, so the full specification is more detailed:

  • $f(0, 0) = (0, 0, 0)$.
  • $f(a, ak) = (a, 1, 0)$ if $a > 0$ and $k \neq 1$.
  • $f(a, ak) = (-a, -1, 0)$ if $a < 0$ and $k \neq 1$.
  • $f(bk, b) = (b, 0, 1)$ if $b > 0$.
  • $f(bk, b) = (-b, 0, -1)$ if $b < 0$.
  • $f(a, b) = (g, x, y)$ if $a \neq 0$ and $b \neq 0$ and $\gcd(a, b) \neq \min(|a|, |b|)$, where $g = \gcd(a, b) \geq 0$, $ax + by = g$, $x \leq \lfloor b/g \rfloor$, and $y \leq \lfloor a/g \rfloor$.
Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
use malachite_base::num::arithmetic::traits::ExtendedGcd;
use malachite_base::strings::ToDebugString;
use malachite_nz::integer::Integer;

assert_eq!(
    Integer::from(3).extended_gcd(&Integer::from(5)).to_debug_string(),
    "(1, 2, -1)"
);
assert_eq!(
    Integer::from(240).extended_gcd(&Integer::from(46)).to_debug_string(),
    "(2, -9, 47)"
);
assert_eq!(
    Integer::from(-111).extended_gcd(&Integer::from(300)).to_debug_string(),
    "(3, 27, 10)"
);

Computes the GCD (greatest common divisor) of two Integers $a$ and $b$, and also the coefficients $x$ and $y$ in Bézout’s identity $ax+by=\gcd(a,b)$. The first Integer is taken by reference and the second by value.

The are infinitely many $x$, $y$ that satisfy the identity for any $a$, $b$, so the full specification is more detailed:

  • $f(0, 0) = (0, 0, 0)$.
  • $f(a, ak) = (a, 1, 0)$ if $a > 0$ and $k \neq 1$.
  • $f(a, ak) = (-a, -1, 0)$ if $a < 0$ and $k \neq 1$.
  • $f(bk, b) = (b, 0, 1)$ if $b > 0$.
  • $f(bk, b) = (-b, 0, -1)$ if $b < 0$.
  • $f(a, b) = (g, x, y)$ if $a \neq 0$ and $b \neq 0$ and $\gcd(a, b) \neq \min(|a|, |b|)$, where $g = \gcd(a, b) \geq 0$, $ax + by = g$, $x \leq \lfloor b/g \rfloor$, and $y \leq \lfloor a/g \rfloor$.
Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
use malachite_base::num::arithmetic::traits::ExtendedGcd;
use malachite_base::strings::ToDebugString;
use malachite_nz::integer::Integer;

assert_eq!(
    (&Integer::from(3)).extended_gcd(Integer::from(5)).to_debug_string(),
    "(1, 2, -1)"
);
assert_eq!(
    (&Integer::from(240)).extended_gcd(Integer::from(46)).to_debug_string(),
    "(2, -9, 47)"
);
assert_eq!(
    (&Integer::from(-111)).extended_gcd(Integer::from(300)).to_debug_string(),
    "(3, 27, 10)"
);

Computes the GCD (greatest common divisor) of two Integers $a$ and $b$, and also the coefficients $x$ and $y$ in Bézout’s identity $ax+by=\gcd(a,b)$. Both Integers are taken by value.

The are infinitely many $x$, $y$ that satisfy the identity for any $a$, $b$, so the full specification is more detailed:

  • $f(0, 0) = (0, 0, 0)$.
  • $f(a, ak) = (a, 1, 0)$ if $a > 0$ and $k \neq 1$.
  • $f(a, ak) = (-a, -1, 0)$ if $a < 0$ and $k \neq 1$.
  • $f(bk, b) = (b, 0, 1)$ if $b > 0$.
  • $f(bk, b) = (-b, 0, -1)$ if $b < 0$.
  • $f(a, b) = (g, x, y)$ if $a \neq 0$ and $b \neq 0$ and $\gcd(a, b) \neq \min(|a|, |b|)$, where $g = \gcd(a, b) \geq 0$, $ax + by = g$, $x \leq \lfloor b/g \rfloor$, and $y \leq \lfloor a/g \rfloor$.
Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
use malachite_base::num::arithmetic::traits::ExtendedGcd;
use malachite_base::strings::ToDebugString;
use malachite_nz::integer::Integer;

assert_eq!(
    Integer::from(3).extended_gcd(Integer::from(5)).to_debug_string(),
    "(1, 2, -1)"
);
assert_eq!(
    Integer::from(240).extended_gcd(Integer::from(46)).to_debug_string(),
    "(2, -9, 47)"
);
assert_eq!(
    Integer::from(-111).extended_gcd(Integer::from(300)).to_debug_string(),
    "(3, 27, 10)"
);

Returns the floor of the $n$th root of an Integer, taking the Integer by reference.

$f(x, n) = \lfloor\sqrt[n]{x}\rfloor$.

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if exp is zero, or if exp is even and self is negative.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::FloorRoot;
use malachite_nz::integer::Integer;

assert_eq!((&Integer::from(999)).floor_root(3), 9);
assert_eq!((&Integer::from(1000)).floor_root(3), 10);
assert_eq!((&Integer::from(1001)).floor_root(3), 10);
assert_eq!((&Integer::from(100000000000i64)).floor_root(5), 158);
assert_eq!((&Integer::from(-100000000000i64)).floor_root(5), -159);

Returns the floor of the $n$th root of an Integer, taking the Integer by value.

$f(x, n) = \lfloor\sqrt[n]{x}\rfloor$.

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if exp is zero, or if exp is even and self is negative.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::FloorRoot;
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(999).floor_root(3), 9);
assert_eq!(Integer::from(1000).floor_root(3), 10);
assert_eq!(Integer::from(1001).floor_root(3), 10);
assert_eq!(Integer::from(100000000000i64).floor_root(5), 158);
assert_eq!(Integer::from(-100000000000i64).floor_root(5), -159);

Replaces an Integer with the floor of its $n$th root.

$x \gets \lfloor\sqrt[n]{x}\rfloor$.

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if exp is zero, or if exp is even and self is negative.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::FloorRootAssign;
use malachite_nz::integer::Integer;

let mut x = Integer::from(999);
x.floor_root_assign(3);
assert_eq!(x, 9);

let mut x = Integer::from(1000);
x.floor_root_assign(3);
assert_eq!(x, 10);

let mut x = Integer::from(1001);
x.floor_root_assign(3);
assert_eq!(x, 10);

let mut x = Integer::from(100000000000i64);
x.floor_root_assign(5);
assert_eq!(x, 158);

let mut x = Integer::from(-100000000000i64);
x.floor_root_assign(5);
assert_eq!(x, -159);

Returns the floor of the square root of an Integer, taking it by reference.

$f(x) = \lfloor\sqrt{x}\rfloor$.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if self is negative.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::FloorSqrt;
use malachite_nz::integer::Integer;

assert_eq!((&Integer::from(99)).floor_sqrt(), 9);
assert_eq!((&Integer::from(100)).floor_sqrt(), 10);
assert_eq!((&Integer::from(101)).floor_sqrt(), 10);
assert_eq!((&Integer::from(1000000000)).floor_sqrt(), 31622);
assert_eq!((&Integer::from(10000000000u64)).floor_sqrt(), 100000);

Returns the floor of the square root of an Integer, taking it by value.

$f(x) = \lfloor\sqrt{x}\rfloor$.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if self is negative.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::FloorSqrt;
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(99).floor_sqrt(), 9);
assert_eq!(Integer::from(100).floor_sqrt(), 10);
assert_eq!(Integer::from(101).floor_sqrt(), 10);
assert_eq!(Integer::from(1000000000).floor_sqrt(), 31622);
assert_eq!(Integer::from(10000000000u64).floor_sqrt(), 100000);

Replaces an Integer with the floor of its square root.

$x \gets \lfloor\sqrt{x}\rfloor$.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if self is negative.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::FloorSqrtAssign;
use malachite_nz::integer::Integer;

let mut x = Integer::from(99);
x.floor_sqrt_assign();
assert_eq!(x, 9);

let mut x = Integer::from(100);
x.floor_sqrt_assign();
assert_eq!(x, 10);

let mut x = Integer::from(101);
x.floor_sqrt_assign();
assert_eq!(x, 10);

let mut x = Integer::from(1000000000);
x.floor_sqrt_assign();
assert_eq!(x, 31622);

let mut x = Integer::from(10000000000u64);
x.floor_sqrt_assign();
assert_eq!(x, 100000);

Converts a Natural to an Integer, taking the Natural by reference.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is value.significant_bits().

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Pow;
use malachite_nz::integer::Integer;
use malachite_nz::natural::Natural;

assert_eq!(Integer::from(&Natural::from(123u32)), 123);
assert_eq!(Integer::from(&Natural::from(10u32).pow(12)), 1000000000000u64);

Converts a Natural to an Integer, taking the Natural by value.

Worst-case complexity

Constant time and additional memory.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Pow;
use malachite_nz::integer::Integer;
use malachite_nz::natural::Natural;

assert_eq!(Integer::from(Natural::from(123u32)), 123);
assert_eq!(Integer::from(Natural::from(10u32).pow(12)), 1000000000000u64);

Converts a signed primitive integer to an Integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts a signed primitive integer to an Integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts a signed primitive integer to an Integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts a signed primitive integer to an Integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts a signed primitive integer to an Integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts a signed primitive integer to an Integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts an unsigned primitive integer to an Integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts an unsigned primitive integer to an Integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts an unsigned primitive integer to an Integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts an unsigned primitive integer to an Integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts an unsigned primitive integer to an Integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts an unsigned primitive integer to an Integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts a string, possibly in scientfic notation, to an Integer.

Use FromSciStringOptions to specify the base (from 2 to 36, inclusive) and the rounding mode, in case rounding is necessary because the string represents a non-integer.

If the base is greater than 10, the higher digits are represented by the letters 'a' through 'z' or 'A' through 'Z'; the case doesn’t matter and doesn’t need to be consistent.

Exponents are allowed, and are indicated using the character 'e' or 'E'. If the base is 15 or greater, an ambiguity arises where it may not be clear whether 'e' is a digit or an exponent indicator. To resolve this ambiguity, always use a '+' or '-' sign after the exponent indicator when the base is 15 or greater.

The exponent itself is always parsed using base 10.

Decimal (or other-base) points are allowed. These are most useful in conjunction with exponents, but they may be used on their own. If the string represents a non-integer, the rounding mode specified in options is used to round to an integer.

If the string is unparseable, None is returned. None is also returned if the rounding mode in options is Exact, but rounding is necessary.

Worst-case complexity

$T(n, m) = O(m^n n \log m (\log n + \log\log m))$

$M(n, m) = O(m^n n \log m)$

where $T$ is time, $M$ is additional memory, $n$ is s.len(), and $m$ is options.base.

Examples
extern crate malachite_base;

use malachite_base::num::conversion::string::options::FromSciStringOptions;
use malachite_base::num::conversion::traits::FromSciString;
use malachite_base::rounding_modes::RoundingMode;
use malachite_nz::integer::Integer;

assert_eq!(Integer::from_sci_string("123").unwrap(), 123);
assert_eq!(Integer::from_sci_string("123.5").unwrap(), 124);
assert_eq!(Integer::from_sci_string("-123.5").unwrap(), -124);
assert_eq!(Integer::from_sci_string("1.23e10").unwrap(), 12300000000i64);

let mut options = FromSciStringOptions::default();
assert_eq!(Integer::from_sci_string_with_options("123.5", options).unwrap(), 124);

options.set_rounding_mode(RoundingMode::Floor);
assert_eq!(Integer::from_sci_string_with_options("123.5", options).unwrap(), 123);

options = FromSciStringOptions::default();
options.set_base(16);
assert_eq!(Integer::from_sci_string_with_options("ff", options).unwrap(), 255);
Converts a &str, possibly in scientific notation, to a number, using the default FromSciStringOptions. Read more

Converts an string to an Integer.

If the string does not represent a valid Integer, an Err is returned. To be valid, the string must be nonempty and only contain the chars '0' through '9', with an optional leading '-'. Leading zeros are allowed, as is the string "-0". The string "-" is not.

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is s.len().

Examples
use malachite_nz::integer::Integer;
use std::str::FromStr;

assert_eq!(Integer::from_str("123456").unwrap(), 123456);
assert_eq!(Integer::from_str("00123456").unwrap(), 123456);
assert_eq!(Integer::from_str("0").unwrap(), 0);
assert_eq!(Integer::from_str("-123456").unwrap(), -123456);
assert_eq!(Integer::from_str("-00123456").unwrap(), -123456);
assert_eq!(Integer::from_str("-0").unwrap(), 0);

assert!(Integer::from_str("").is_err());
assert!(Integer::from_str("a").is_err());
The associated error which can be returned from parsing.

Converts an string, in a specified base, to an Integer.

If the string does not represent a valid Integer, an Err is returned. To be valid, the string must be nonempty and only contain the chars '0' through '9', 'a' through 'z', and 'A' through 'Z', with an optional leading '-'; and only characters that represent digits smaller than the base are allowed. Leading zeros are allowed, as is the string "-0". The string "-" is not.

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is s.len().

Panics

Panics if base is less than 2 or greater than 36.

Examples
extern crate malachite_base;

use malachite_base::num::conversion::traits::{Digits, FromStringBase};
use malachite_nz::integer::Integer;

assert_eq!(Integer::from_string_base(10, "123456").unwrap(), 123456);
assert_eq!(Integer::from_string_base(10, "00123456").unwrap(), 123456);
assert_eq!(Integer::from_string_base(16, "0").unwrap(), 0);
assert_eq!(
    Integer::from_string_base(16, "deadbeef").unwrap(),
    3735928559i64
);
assert_eq!(
    Integer::from_string_base(16, "deAdBeEf").unwrap(),
    3735928559i64
);
assert_eq!(Integer::from_string_base(10, "-123456").unwrap(), -123456);
assert_eq!(Integer::from_string_base(10, "-00123456").unwrap(), -123456);
assert_eq!(Integer::from_string_base(16, "-0").unwrap(), 0);
assert_eq!(
    Integer::from_string_base(16, "-deadbeef").unwrap(),
    -3735928559i64
);
assert_eq!(
    Integer::from_string_base(16, "-deAdBeEf").unwrap(),
    -3735928559i64
);

assert!(Integer::from_string_base(10, "").is_none());
assert!(Integer::from_string_base(10, "a").is_none());
assert!(Integer::from_string_base(2, "2").is_none());
assert!(Integer::from_string_base(2, "-2").is_none());
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more

Determines whether an Integer is an integer. It always returns true.

$f(x) = \textrm{true}$.

Worst-case complexity

Constant time and additional memory.

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::{NegativeOne, One, Zero};
use malachite_base::num::conversion::traits::IsInteger;
use malachite_nz::integer::Integer;

assert_eq!(Integer::ZERO.is_integer(), true);
assert_eq!(Integer::ONE.is_integer(), true);
assert_eq!(Integer::from(100).is_integer(), true);
assert_eq!(Integer::NEGATIVE_ONE.is_integer(), true);
assert_eq!(Integer::from(-100).is_integer(), true);

Computes the Jacobi symbol of two Integers, taking both by reference.

$$ f(x, y) = \left ( \frac{x}{y} \right ). $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Panics

Panics if self is negative or if other is even.

Examples
use malachite_base::num::arithmetic::traits::JacobiSymbol;
use malachite_nz::integer::Integer;

assert_eq!((&Integer::from(10)).jacobi_symbol(&Integer::from(5)), 0);
assert_eq!((&Integer::from(7)).jacobi_symbol(&Integer::from(5)), -1);
assert_eq!((&Integer::from(11)).jacobi_symbol(&Integer::from(5)), 1);
assert_eq!((&Integer::from(11)).jacobi_symbol(&Integer::from(9)), 1);
assert_eq!((&Integer::from(-7)).jacobi_symbol(&Integer::from(5)), -1);
assert_eq!((&Integer::from(-11)).jacobi_symbol(&Integer::from(5)), 1);
assert_eq!((&Integer::from(-11)).jacobi_symbol(&Integer::from(9)), 1);

Computes the Jacobi symbol of two Integers, taking the first by value and the second by reference.

$$ f(x, y) = \left ( \frac{x}{y} \right ). $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Panics

Panics if self is negative or if other is even.

Examples
use malachite_base::num::arithmetic::traits::JacobiSymbol;
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(10).jacobi_symbol(&Integer::from(5)), 0);
assert_eq!(Integer::from(7).jacobi_symbol(&Integer::from(5)), -1);
assert_eq!(Integer::from(11).jacobi_symbol(&Integer::from(5)), 1);
assert_eq!(Integer::from(11).jacobi_symbol(&Integer::from(9)), 1);
assert_eq!(Integer::from(-7).jacobi_symbol(&Integer::from(5)), -1);
assert_eq!(Integer::from(-11).jacobi_symbol(&Integer::from(5)), 1);
assert_eq!(Integer::from(-11).jacobi_symbol(&Integer::from(9)), 1);

Computes the Jacobi symbol of two Integers, taking the first by reference and the second by value.

$$ f(x, y) = \left ( \frac{x}{y} \right ). $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Panics

Panics if self is negative or if other is even.

Examples
use malachite_base::num::arithmetic::traits::JacobiSymbol;
use malachite_nz::integer::Integer;

assert_eq!((&Integer::from(10)).jacobi_symbol(Integer::from(5)), 0);
assert_eq!((&Integer::from(7)).jacobi_symbol(Integer::from(5)), -1);
assert_eq!((&Integer::from(11)).jacobi_symbol(Integer::from(5)), 1);
assert_eq!((&Integer::from(11)).jacobi_symbol(Integer::from(9)), 1);
assert_eq!((&Integer::from(-7)).jacobi_symbol(Integer::from(5)), -1);
assert_eq!((&Integer::from(-11)).jacobi_symbol(Integer::from(5)), 1);
assert_eq!((&Integer::from(-11)).jacobi_symbol(Integer::from(9)), 1);

Computes the Jacobi symbol of two Integers, taking both by value.

$$ f(x, y) = \left ( \frac{x}{y} \right ). $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Panics

Panics if self is negative or if other is even.

Examples
use malachite_base::num::arithmetic::traits::JacobiSymbol;
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(10).jacobi_symbol(Integer::from(5)), 0);
assert_eq!(Integer::from(7).jacobi_symbol(Integer::from(5)), -1);
assert_eq!(Integer::from(11).jacobi_symbol(Integer::from(5)), 1);
assert_eq!(Integer::from(11).jacobi_symbol(Integer::from(9)), 1);
assert_eq!(Integer::from(-7).jacobi_symbol(Integer::from(5)), -1);
assert_eq!(Integer::from(-11).jacobi_symbol(Integer::from(5)), 1);
assert_eq!(Integer::from(-11).jacobi_symbol(Integer::from(9)), 1);

Computes the Kronecker symbol of two Integers, taking both by reference.

$$ f(x, y) = \left ( \frac{x}{y} \right ). $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
use malachite_base::num::arithmetic::traits::KroneckerSymbol;
use malachite_nz::integer::Integer;

assert_eq!((&Integer::from(10)).kronecker_symbol(&Integer::from(5)), 0);
assert_eq!((&Integer::from(7)).kronecker_symbol(&Integer::from(5)), -1);
assert_eq!((&Integer::from(11)).kronecker_symbol(&Integer::from(5)), 1);
assert_eq!((&Integer::from(11)).kronecker_symbol(&Integer::from(9)), 1);
assert_eq!((&Integer::from(11)).kronecker_symbol(&Integer::from(8)), -1);
assert_eq!((&Integer::from(-7)).kronecker_symbol(&Integer::from(5)), -1);
assert_eq!((&Integer::from(-11)).kronecker_symbol(&Integer::from(5)), 1);
assert_eq!((&Integer::from(-11)).kronecker_symbol(&Integer::from(9)), 1);
assert_eq!((&Integer::from(-11)).kronecker_symbol(&Integer::from(8)), -1);
assert_eq!((&Integer::from(-11)).kronecker_symbol(&Integer::from(-8)), 1);

Computes the Kronecker symbol of two Integers, taking the first by value and the second by reference.

$$ f(x, y) = \left ( \frac{x}{y} \right ). $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
use malachite_base::num::arithmetic::traits::KroneckerSymbol;
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(10).kronecker_symbol(&Integer::from(5)), 0);
assert_eq!(Integer::from(7).kronecker_symbol(&Integer::from(5)), -1);
assert_eq!(Integer::from(11).kronecker_symbol(&Integer::from(5)), 1);
assert_eq!(Integer::from(11).kronecker_symbol(&Integer::from(9)), 1);
assert_eq!(Integer::from(11).kronecker_symbol(&Integer::from(8)), -1);
assert_eq!(Integer::from(-7).kronecker_symbol(&Integer::from(5)), -1);
assert_eq!(Integer::from(-11).kronecker_symbol(&Integer::from(5)), 1);
assert_eq!(Integer::from(-11).kronecker_symbol(&Integer::from(9)), 1);
assert_eq!(Integer::from(-11).kronecker_symbol(&Integer::from(8)), -1);
assert_eq!(Integer::from(-11).kronecker_symbol(&Integer::from(-8)), 1);

Computes the Kronecker symbol of two Integers, taking the first by reference and the second value.

$$ f(x, y) = \left ( \frac{x}{y} \right ). $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
use malachite_base::num::arithmetic::traits::KroneckerSymbol;
use malachite_nz::integer::Integer;

assert_eq!((&Integer::from(10)).kronecker_symbol(Integer::from(5)), 0);
assert_eq!((&Integer::from(7)).kronecker_symbol(Integer::from(5)), -1);
assert_eq!((&Integer::from(11)).kronecker_symbol(Integer::from(5)), 1);
assert_eq!((&Integer::from(11)).kronecker_symbol(Integer::from(9)), 1);
assert_eq!((&Integer::from(11)).kronecker_symbol(Integer::from(8)), -1);
assert_eq!((&Integer::from(-7)).kronecker_symbol(Integer::from(5)), -1);
assert_eq!((&Integer::from(-11)).kronecker_symbol(Integer::from(5)), 1);
assert_eq!((&Integer::from(-11)).kronecker_symbol(Integer::from(9)), 1);
assert_eq!((&Integer::from(-11)).kronecker_symbol(Integer::from(8)), -1);
assert_eq!((&Integer::from(-11)).kronecker_symbol(Integer::from(-8)), 1);

Computes the Kronecker symbol of two Integers, taking both by value.

$$ f(x, y) = \left ( \frac{x}{y} \right ). $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
use malachite_base::num::arithmetic::traits::KroneckerSymbol;
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(10).kronecker_symbol(Integer::from(5)), 0);
assert_eq!(Integer::from(7).kronecker_symbol(Integer::from(5)), -1);
assert_eq!(Integer::from(11).kronecker_symbol(Integer::from(5)), 1);
assert_eq!(Integer::from(11).kronecker_symbol(Integer::from(9)), 1);
assert_eq!(Integer::from(11).kronecker_symbol(Integer::from(8)), -1);
assert_eq!(Integer::from(-7).kronecker_symbol(Integer::from(5)), -1);
assert_eq!(Integer::from(-11).kronecker_symbol(Integer::from(5)), 1);
assert_eq!(Integer::from(-11).kronecker_symbol(Integer::from(9)), 1);
assert_eq!(Integer::from(-11).kronecker_symbol(Integer::from(8)), -1);
assert_eq!(Integer::from(-11).kronecker_symbol(Integer::from(-8)), 1);

Computes the Legendre symbol of two Integers, taking both by reference.

This implementation is identical to that of JacobiSymbol, since there is no computational benefit to requiring that the denominator be prime.

$$ f(x, y) = \left ( \frac{x}{y} \right ). $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Panics

Panics if self is negative or if other is even.

Examples
use malachite_base::num::arithmetic::traits::LegendreSymbol;
use malachite_nz::integer::Integer;

assert_eq!((&Integer::from(10)).legendre_symbol(&Integer::from(5)), 0);
assert_eq!((&Integer::from(7)).legendre_symbol(&Integer::from(5)), -1);
assert_eq!((&Integer::from(11)).legendre_symbol(&Integer::from(5)), 1);
assert_eq!((&Integer::from(-7)).legendre_symbol(&Integer::from(5)), -1);
assert_eq!((&Integer::from(-11)).legendre_symbol(&Integer::from(5)), 1);

Computes the Legendre symbol of two Integers, taking the first by value and the second by reference.

This implementation is identical to that of JacobiSymbol, since there is no computational benefit to requiring that the denominator be prime.

$$ f(x, y) = \left ( \frac{x}{y} \right ). $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Panics

Panics if self is negative or if other is even.

Examples
use malachite_base::num::arithmetic::traits::LegendreSymbol;
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(10).legendre_symbol(&Integer::from(5)), 0);
assert_eq!(Integer::from(7).legendre_symbol(&Integer::from(5)), -1);
assert_eq!(Integer::from(11).legendre_symbol(&Integer::from(5)), 1);
assert_eq!(Integer::from(-7).legendre_symbol(&Integer::from(5)), -1);
assert_eq!(Integer::from(-11).legendre_symbol(&Integer::from(5)), 1);

Computes the Legendre symbol of two Integers, taking the first by reference and the second by value.

This implementation is identical to that of JacobiSymbol, since there is no computational benefit to requiring that the denominator be prime.

$$ f(x, y) = \left ( \frac{x}{y} \right ). $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Panics

Panics if self is negative or if other is even.

Examples
use malachite_base::num::arithmetic::traits::LegendreSymbol;
use malachite_nz::integer::Integer;

assert_eq!((&Integer::from(10)).legendre_symbol(Integer::from(5)), 0);
assert_eq!((&Integer::from(7)).legendre_symbol(Integer::from(5)), -1);
assert_eq!((&Integer::from(11)).legendre_symbol(Integer::from(5)), 1);
assert_eq!((&Integer::from(-7)).legendre_symbol(Integer::from(5)), -1);
assert_eq!((&Integer::from(-11)).legendre_symbol(Integer::from(5)), 1);

Computes the Legendre symbol of two Integers, taking both by value.

This implementation is identical to that of JacobiSymbol, since there is no computational benefit to requiring that the denominator be prime.

$$ f(x, y) = \left ( \frac{x}{y} \right ). $$

Worst-case complexity

$T(n) = O(n (\log n)^2 \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Panics

Panics if self is negative or if other is even.

Examples
use malachite_base::num::arithmetic::traits::LegendreSymbol;
use malachite_nz::integer::Integer;

assert_eq!(Integer::from(10).legendre_symbol(Integer::from(5)), 0);
assert_eq!(Integer::from(7).legendre_symbol(Integer::from(5)), -1);
assert_eq!(Integer::from(11).legendre_symbol(Integer::from(5)), 1);
assert_eq!(Integer::from(-7).legendre_symbol(Integer::from(5)), -1);
assert_eq!(Integer::from(-11).legendre_symbol(Integer::from(5)), 1);

Returns an Integer whose least significant $b$ bits are true and whose other bits are false.

$f(b) = 2^b - 1$.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is bits.

Examples
extern crate malachite_base;

use malachite_base::num::logic::traits::LowMask;
use malachite_nz::integer::Integer;

assert_eq!(Integer::low_mask(0), 0);
assert_eq!(Integer::low_mask(3), 7);
assert_eq!(Integer::low_mask(100).to_string(), "1267650600228229401496703205375");

Converts an Integer to a hexadecimal String using lowercase characters.

Using the # format flag prepends "0x" to the string.

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
extern crate malachite_base;

use malachite_base::num::basic::traits::Zero;
use malachite_base::strings::ToLowerHexString;
use malachite_nz::integer::Integer;
use std::str::FromStr;

assert_eq!(Integer::ZERO.to_lower_hex_string(), "0");
assert_eq!(Integer::from(123).to_lower_hex_string(), "7b");
assert_eq!(
    Integer::from_str("1000000000000")
        .unwrap()
        .to_lower_hex_string(),
    "e8d4a51000"
);
assert_eq!(format!("{:07x}", Integer::from(123)), "000007b");
assert_eq!(Integer::from(-123).to_lower_hex_string(), "-7b");
assert_eq!(
    Integer::from_str("-1000000000000")
        .unwrap()
        .to_lower_hex_string(),
    "-e8d4a51000"
);
assert_eq!(format!("{:07x}", Integer::from(-123)), "-00007b");

assert_eq!(format!("{:#x}", Integer::ZERO), "0x0");
assert_eq!(format!("{:#x}", Integer::from(123)), "0x7b");
assert_eq!(
    format!("{:#x}", Integer::from_str("1000000000000").unwrap()),
    "0xe8d4a51000"
);
assert_eq!(format!("{:#07x}", Integer::from(123)), "0x0007b");
assert_eq!(format!("{:#x}", Integer::from(-123)), "-0x7b");
assert_eq!(
    format!("{:#x}", Integer::from_str("-1000000000000").unwrap()),
    "-0xe8d4a51000"
);
assert_eq!(format!("{:#07x}", Integer::from(-123)), "-0x007b");

Divides an Integer by another Integer, taking the first by value and the second by reference and returning just the remainder. The remainder has the same sign as the second Integer.

If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = x - y\left \lfloor \frac{x}{y} \right \rfloor. $$

This function is called mod_op rather than mod because mod is a Rust keyword.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Mod;
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
assert_eq!(Integer::from(23).mod_op(&Integer::from(10)), 3);

// -3 * -10 + -7 = 23
assert_eq!(Integer::from(23).mod_op(&Integer::from(-10)), -7);

// -3 * 10 + 7 = -23
assert_eq!(Integer::from(-23).mod_op(&Integer::from(10)), 7);

// 2 * -10 + -3 = -23
assert_eq!(Integer::from(-23).mod_op(&Integer::from(-10)), -3);

Divides an Integer by another Integer, taking both by reference and returning just the remainder. The remainder has the same sign as the second Integer.

If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = x - y\left \lfloor \frac{x}{y} \right \rfloor. $$

This function is called mod_op rather than mod because mod is a Rust keyword.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Mod;
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
assert_eq!((&Integer::from(23)).mod_op(&Integer::from(10)), 3);

// -3 * -10 + -7 = 23
assert_eq!((&Integer::from(23)).mod_op(&Integer::from(-10)), -7);

// -3 * 10 + 7 = -23
assert_eq!((&Integer::from(-23)).mod_op(&Integer::from(10)), 7);

// 2 * -10 + -3 = -23
assert_eq!((&Integer::from(-23)).mod_op(&Integer::from(-10)), -3);

Divides an Integer by another Integer, taking the first by reference and the second by value and returning just the remainder. The remainder has the same sign as the second Integer.

If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = x - y\left \lfloor \frac{x}{y} \right \rfloor. $$

This function is called mod_op rather than mod because mod is a Rust keyword.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Mod;
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
assert_eq!((&Integer::from(23)).mod_op(Integer::from(10)), 3);

// -3 * -10 + -7 = 23
assert_eq!((&Integer::from(23)).mod_op(Integer::from(-10)), -7);

// -3 * 10 + 7 = -23
assert_eq!((&Integer::from(-23)).mod_op(Integer::from(10)), 7);

// 2 * -10 + -3 = -23
assert_eq!((&Integer::from(-23)).mod_op(Integer::from(-10)), -3);

Divides an Integer by another Integer, taking both by value and returning just the remainder. The remainder has the same sign as the second Integer.

If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ f(x, y) = x - y\left \lfloor \frac{x}{y} \right \rfloor. $$

This function is called mod_op rather than mod because mod is a Rust keyword.

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::Mod;
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
assert_eq!(Integer::from(23).mod_op(Integer::from(10)), 3);

// -3 * -10 + -7 = 23
assert_eq!(Integer::from(23).mod_op(Integer::from(-10)), -7);

// -3 * 10 + 7 = -23
assert_eq!(Integer::from(-23).mod_op(Integer::from(10)), 7);

// 2 * -10 + -3 = -23
assert_eq!(Integer::from(-23).mod_op(Integer::from(-10)), -3);

Divides an Integer by another Integer, taking the second Integer by reference and replacing the first by the remainder. The remainder has the same sign as the second Integer.

If the quotient were computed, he quotient and remainder would satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ x \gets x - y\left \lfloor \frac{x}{y} \right \rfloor. $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::ModAssign;
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
let mut x = Integer::from(23);
x.mod_assign(&Integer::from(10));
assert_eq!(x, 3);

// -3 * -10 + -7 = 23
let mut x = Integer::from(23);
x.mod_assign(&Integer::from(-10));
assert_eq!(x, -7);

// -3 * 10 + 7 = -23
let mut x = Integer::from(-23);
x.mod_assign(&Integer::from(10));
assert_eq!(x, 7);

// 2 * -10 + -3 = -23
let mut x = Integer::from(-23);
x.mod_assign(&Integer::from(-10));
assert_eq!(x, -3);

Divides an Integer by another Integer, taking the second Integer by value and replacing the first by the remainder. The remainder has the same sign as the second Integer.

If the quotient were computed, he quotient and remainder would satisfy $x = qy + r$ and $0 \leq |r| < |y|$.

$$ x \gets x - y\left \lfloor \frac{x}{y} \right \rfloor. $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is self.significant_bits().

Panics

Panics if other is zero.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::ModAssign;
use malachite_nz::integer::Integer;

// 2 * 10 + 3 = 23
let mut x = Integer::from(23);
x.mod_assign(Integer::from(10));
assert_eq!(x, 3);

// -3 * -10 + -7 = 23
let mut x = Integer::from(23);
x.mod_assign(Integer::from(-10));
assert_eq!(x, -7);

// -3 * 10 + 7 = -23
let mut x = Integer::from(-23);
x.mod_assign(Integer::from(10));
assert_eq!(x, 7);

// 2 * -10 + -3 = -23
let mut x = Integer::from(-23);
x.mod_assign(Integer::from(-10));
assert_eq!(x, -3);

Divides an Integer by $2^k$, taking it by reference and returning just the remainder. The remainder is non-negative.

If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0 \leq r < 2^k$.

$$ f(x, k) = x - 2^k\left \lfloor \frac{x}{2^k} \right \rfloor. $$

Unlike rem_power_of_2, this function always returns a non-negative number.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is pow.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::ModPowerOf2;
use malachite_nz::integer::Integer;

// 1 * 2^8 + 4 = 260
assert_eq!((&Integer::from(260)).mod_power_of_2(8), 4);
// -101 * 2^4 + 5 = -1611
assert_eq!((&Integer::from(-1611)).mod_power_of_2(4), 5);

Divides an Integer by $2^k$, taking it by value and returning just the remainder. The remainder is non-negative.

If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0 \leq r < 2^k$.

$$ f(x, k) = x - 2^k\left \lfloor \frac{x}{2^k} \right \rfloor. $$

Unlike rem_power_of_2, this function always returns a non-negative number.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is pow.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::ModPowerOf2;
use malachite_nz::integer::Integer;

// 1 * 2^8 + 4 = 260
assert_eq!(Integer::from(260).mod_power_of_2(8), 4);

// -101 * 2^4 + 5 = -1611
assert_eq!(Integer::from(-1611).mod_power_of_2(4), 5);

Divides an Integer by $2^k$, replacing the Integer by the remainder. The remainder is non-negative.

If the quotient were computed, he quotient and remainder would satisfy $x = q2^k + r$ and $0 \leq r < 2^k$.

$$ x \gets x - 2^k\left \lfloor \frac{x}{2^k} \right \rfloor. $$

Unlike rem_power_of_2_assign, this function always assigns a non-negative number.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(n)$

where $T$ is time, $M$ is additional memory, and $n$ is pow.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::ModPowerOf2Assign;
use malachite_nz::integer::Integer;

// 1 * 2^8 + 4 = 260
let mut x = Integer::from(260);
x.mod_power_of_2_assign(8);
assert_eq!(x, 4);

// -101 * 2^4 + 5 = -1611
let mut x = Integer::from(-1611);
x.mod_power_of_2_assign(4);
assert_eq!(x, 5);

Multiplies two Integers, taking both by reference.

$$ f(x, y) = xy. $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::{One, Zero};
use malachite_nz::integer::Integer;
use std::str::FromStr;

assert_eq!(&Integer::ONE * &Integer::from(123), 123);
assert_eq!(&Integer::from(123) * &Integer::ZERO, 0);
assert_eq!(&Integer::from(123) * &Integer::from(-456), -56088);
assert_eq!(
    (&Integer::from(-123456789000i64) * &Integer::from(-987654321000i64)).to_string(),
    "121932631112635269000000"
);
The resulting type after applying the * operator.

Multiplies two Integers, taking the first by value and the second by reference.

$$ f(x, y) = xy. $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::{One, Zero};
use malachite_nz::integer::Integer;
use std::str::FromStr;

assert_eq!(Integer::ONE * &Integer::from(123), 123);
assert_eq!(Integer::from(123) * &Integer::ZERO, 0);
assert_eq!(Integer::from(123) * &Integer::from(-456), -56088);
assert_eq!(
    (Integer::from(-123456789000i64) * &Integer::from(-987654321000i64)).to_string(),
    "121932631112635269000000"
);
The resulting type after applying the * operator.

Multiplies two Integers, taking the first by reference and the second by value.

$$ f(x, y) = xy. $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::{One, Zero};
use malachite_nz::integer::Integer;
use std::str::FromStr;

assert_eq!(&Integer::ONE * Integer::from(123), 123);
assert_eq!(&Integer::from(123) * Integer::ZERO, 0);
assert_eq!(&Integer::from(123) * Integer::from(-456), -56088);
assert_eq!(
    (&Integer::from(-123456789000i64) * Integer::from(-987654321000i64)).to_string(),
    "121932631112635269000000"
);
The resulting type after applying the * operator.

Multiplies two Integers, taking both by value.

$$ f(x, y) = xy. $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::{One, Zero};
use malachite_nz::integer::Integer;
use std::str::FromStr;

assert_eq!(Integer::ONE * Integer::from(123), 123);
assert_eq!(Integer::from(123) * Integer::ZERO, 0);
assert_eq!(Integer::from(123) * Integer::from(-456), -56088);
assert_eq!(
    (Integer::from(-123456789000i64) * Integer::from(-987654321000i64)).to_string(),
    "121932631112635269000000"
);
The resulting type after applying the * operator.

Multiplies an Integer by an Integer in place, taking the Integer on the right-hand side by reference.

$$ x \gets = xy. $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::NegativeOne;
use malachite_nz::integer::Integer;
use std::str::FromStr;

let mut x = Integer::NEGATIVE_ONE;
x *= &Integer::from(1000);
x *= &Integer::from(2000);
x *= &Integer::from(3000);
x *= &Integer::from(4000);
assert_eq!(x, -24000000000000i64);

Multiplies an Integer by an Integer in place, taking the Integer on the right-hand side by value.

$$ x \gets = xy. $$

Worst-case complexity

$T(n) = O(n \log n \log\log n)$

$M(n) = O(n \log n)$

where $T$ is time, $M$ is additional memory, and $n$ is max(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::NegativeOne;
use malachite_nz::integer::Integer;
use std::str::FromStr;

let mut x = Integer::NEGATIVE_ONE;
x *= Integer::from(1000);
x *= Integer::from(2000);
x *= Integer::from(3000);
x *= Integer::from(4000);
assert_eq!(x, -24000000000000i64);

The name of this type, as given by the stringify macro.

See the documentation for impl_named for more details.

Negates an Integer, taking it by reference.

$$ f(x) = -x. $$

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
extern crate malachite_base;

use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;

assert_eq!(-&Integer::ZERO, 0);
assert_eq!(-&Integer::from(123), -123);
assert_eq!(-&Integer::from(-123), 123);
The resulting type after applying the - operator.

Negates an Integer, taking it by value.

$$ f(x) = -x. $$

Worst-case complexity

Constant time and additional memory.

Examples
extern crate malachite_base;

use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;

assert_eq!(-Integer::ZERO, 0);
assert_eq!(-Integer::from(123), -123);
assert_eq!(-Integer::from(-123), 123);
The resulting type after applying the - operator.

Negates an Integer in place.

$$ x \gets -x. $$

Worst-case complexity

Constant time and additional memory.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::NegAssign;
use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;

let mut x = Integer::ZERO;
x.neg_assign();
assert_eq!(x, 0);

let mut x = Integer::from(123);
x.neg_assign();
assert_eq!(x, -123);

let mut x = Integer::from(-123);
x.neg_assign();
assert_eq!(x, 123);

The constant -1.

Returns the bitwise negation of an Integer, taking it by reference.

$$ f(n) = -n - 1. $$

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
extern crate malachite_base;

use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;

assert_eq!(!&Integer::ZERO, -1);
assert_eq!(!&Integer::from(123), -124);
assert_eq!(!&Integer::from(-123), 122);
The resulting type after applying the ! operator.

Returns the bitwise negation of an Integer, taking it by value.

$$ f(n) = -n - 1. $$

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
extern crate malachite_base;

use malachite_base::num::basic::traits::Zero;
use malachite_nz::integer::Integer;

assert_eq!(!Integer::ZERO, -1);
assert_eq!(!Integer::from(123), -124);
assert_eq!(!Integer::from(-123), 122);
The resulting type after applying the ! operator.

Replaces an Integer with its bitwise negation.

$$ n \gets -n - 1. $$

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
extern crate malachite_base;

use malachite_base::num::basic::traits::Zero;
use malachite_base::num::logic::traits::NotAssign;
use malachite_nz::integer::Integer;

let mut x = Integer::ZERO;
x.not_assign();
assert_eq!(x, -1);

let mut x = Integer::from(123);
x.not_assign();
assert_eq!(x, -124);

let mut x = Integer::from(-123);
x.not_assign();
assert_eq!(x, 122);

Converts an Integer to an octal String.

Using the # format flag prepends "0o" to the string.

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
extern crate malachite_base;

use malachite_base::num::basic::traits::Zero;
use malachite_base::strings::ToOctalString;
use malachite_nz::integer::Integer;
use std::str::FromStr;

assert_eq!(Integer::ZERO.to_octal_string(), "0");
assert_eq!(Integer::from(123).to_octal_string(), "173");
assert_eq!(
    Integer::from_str("1000000000000")
        .unwrap()
        .to_octal_string(),
    "16432451210000"
);
assert_eq!(format!("{:07o}", Integer::from(123)), "0000173");
assert_eq!(Integer::from(-123).to_octal_string(), "-173");
assert_eq!(
    Integer::from_str("-1000000000000")
        .unwrap()
        .to_octal_string(),
    "-16432451210000"
);
assert_eq!(format!("{:07o}", Integer::from(-123)), "-000173");

assert_eq!(format!("{:#o}", Integer::ZERO), "0o0");
assert_eq!(format!("{:#o}", Integer::from(123)), "0o173");
assert_eq!(
    format!("{:#o}", Integer::from_str("1000000000000").unwrap()),
    "0o16432451210000"
);
assert_eq!(format!("{:#07o}", Integer::from(123)), "0o00173");
assert_eq!(format!("{:#o}", Integer::from(-123)), "-0o173");
assert_eq!(
    format!("{:#o}", Integer::from_str("-1000000000000").unwrap()),
    "-0o16432451210000"
);
assert_eq!(format!("{:#07o}", Integer::from(-123)), "-0o0173");

The constant 1.

Compares two Integers.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is min(self.significant_bits(), other.significant_bits()).

Examples
use malachite_nz::integer::Integer;

assert!(Integer::from(-123) < Integer::from(-122));
assert!(Integer::from(-123) <= Integer::from(-122));
assert!(Integer::from(-123) > Integer::from(-124));
assert!(Integer::from(-123) >= Integer::from(-124));
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more

Compares the absolute values of two Integers.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is min(self.significant_bits(), other.significant_bits()).

Examples
extern crate malachite_base;

use malachite_base::num::comparison::traits::PartialOrdAbs;
use malachite_nz::integer::Integer;

assert!(Integer::from(-123).lt_abs(&Integer::from(-124)));
assert!(Integer::from(-123).le_abs(&Integer::from(-124)));
assert!(Integer::from(-124).gt_abs(&Integer::from(-123)));
assert!(Integer::from(-124).ge_abs(&Integer::from(-123)));

Converts an Integer to a signed primitive integer, wrapping modulo $2^W$, where $W$ is the width of the primitive integer.

The returned boolean value indicates whether wrapping occurred.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts an Integer to a signed primitive integer, wrapping modulo $2^W$, where $W$ is the width of the primitive integer.

The returned boolean value indicates whether wrapping occurred.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts an Integer to a signed primitive integer, wrapping modulo $2^W$, where $W$ is the width of the primitive integer.

The returned boolean value indicates whether wrapping occurred.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts an Integer to a signed primitive integer, wrapping modulo $2^W$, where $W$ is the width of the primitive integer.

The returned boolean value indicates whether wrapping occurred.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts an Integer to a signed primitive integer, wrapping modulo $2^W$, where $W$ is the width of the primitive integer.

The returned boolean value indicates whether wrapping occurred.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts an Integer to a signed primitive integer, wrapping modulo $2^W$, where $W$ is the width of the primitive integer.

The returned boolean value indicates whether wrapping occurred.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts an Integer to an unsigned primitive integer, wrapping modulo $2^W$, where $W$ is the width of the primitive integer.

The returned boolean value indicates whether wrapping occurred.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts an Integer to an unsigned primitive integer, wrapping modulo $2^W$, where $W$ is the width of the primitive integer.

The returned boolean value indicates whether wrapping occurred.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts an Integer to an unsigned primitive integer, wrapping modulo $2^W$, where $W$ is the width of the primitive integer.

The returned boolean value indicates whether wrapping occurred.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts an Integer to an unsigned primitive integer, wrapping modulo $2^W$, where $W$ is the width of the primitive integer.

The returned boolean value indicates whether wrapping occurred.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts an Integer to an unsigned primitive integer, wrapping modulo $2^W$, where $W$ is the width of the primitive integer.

The returned boolean value indicates whether wrapping occurred.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Converts an Integer to an unsigned primitive integer, wrapping modulo $2^W$, where $W$ is the width of the primitive integer.

The returned boolean value indicates whether wrapping occurred.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

Tests whether an Integer is even.

$f(x) = (2|x)$.

$f(x) = (\exists k \in \N : x = 2k)$.

Worst-case complexity

Constant time and additional memory.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::{Parity, Pow};
use malachite_base::num::basic::traits::{One, Zero};
use malachite_nz::integer::Integer;

assert_eq!(Integer::ZERO.even(), true);
assert_eq!(Integer::from(123).even(), false);
assert_eq!(Integer::from(-0x80).even(), true);
assert_eq!(Integer::from(10u32).pow(12).even(), true);
assert_eq!((-Integer::from(10u32).pow(12) - Integer::ONE).even(), false);

Tests whether an Integer is odd.

$f(x) = (2\nmid x)$.

$f(x) = (\exists k \in \N : x = 2k+1)$.

Worst-case complexity

Constant time and additional memory.

Examples
extern crate malachite_base;

use malachite_base::num::arithmetic::traits::{Parity, Pow};
use malachite_base::num::basic::traits::{One, Zero};
use malachite_nz::integer::Integer;

assert_eq!(Integer::ZERO.odd(), false);
assert_eq!(Integer::from(123).odd(), true);
assert_eq!(Integer::from(-0x80).odd(), false);
assert_eq!(Integer::from(10u32).pow(12).odd(), false);
assert_eq!((-Integer::from(10u32).pow(12) - Integer::ONE).odd(), true);
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a Natural is equal to an Integer.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where n = min(self.significant_bits(), other.significant_bits())

Examples
use malachite_nz::integer::Integer;
use malachite_nz::natural::Natural;

assert!(Natural::from(123u32) == Integer::from(123));
assert!(Natural::from(123u32) != Integer::from(5));
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a primitive float is equal to an Integer.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is other.significant_bits().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a primitive float is equal to an Integer.

Worst-case complexity

$T(n) = O(n)$

$M(n) = O(1)$

where $T$ is time, $M$ is additional memory, and $n$ is other.significant_bits().

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a signed primitive integer is equal to an Integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a signed primitive integer is equal to an Integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a signed primitive integer is equal to an Integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a signed primitive integer is equal to an Integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a signed primitive integer is equal to an Integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether a signed primitive integer is equal to an Integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether an unsigned primitive integer is equal to an Integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether an unsigned primitive integer is equal to an Integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether an unsigned primitive integer is equal to an Integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Determines whether an unsigned primitive integer is equal to an Integer.

Worst-case complexity

Constant time and additional memory.

Examples

See here.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more