Struct BitUint

Source
pub struct BitUint<T: Unsigned + PrimInt, const N: u32>(/* private fields */);
Expand description

BitUint is a type that represents a N-bit unsigned integer.

N is the number of bits in the value. The largest size of N is equal to the size of the underlying type in bits.

§Examples

type Uint = BitUint<u8, 7>;

let n = Uint::new(127).unwrap();
assert_eq!(n, Uint::MAX);

assert_eq!(n.checked_add(1), None);
assert_eq!(n.get().checked_add(1), Some(128));

In this case, N must be less than or equal to u32::BITS:

let _ = BitUint::<u32, 33>::new(42);

N must be greater than 0:

let _ = BitUint::<u64, 0>::new(0);

Implementations§

Source§

impl<const N: u32> BitUint<u8, N>

Source

pub const MIN: Self

The smallest value that can be represented by this BitUint.

The value is always 0.

§Examples
assert_eq!(BitUint::<u8, 7>::MIN.get(), 0);
Source

pub const MAX: Self

The largest value that can be represented by this BitUint.

§Examples
assert_eq!(BitUint::<u8, 7>::MAX.get(), 127);
Source

pub const BITS: u32 = N

The size of this BitUint in bits.

§Examples
assert_eq!(BitUint::<u8, 7>::BITS, 7);
Source§

impl<const N: u32> BitUint<u16, N>

Source

pub const MIN: Self

The smallest value that can be represented by this BitUint.

The value is always 0.

§Examples
assert_eq!(BitUint::<u16, 7>::MIN.get(), 0);
Source

pub const MAX: Self

The largest value that can be represented by this BitUint.

§Examples
assert_eq!(BitUint::<u16, 7>::MAX.get(), 127);
Source

pub const BITS: u32 = N

The size of this BitUint in bits.

§Examples
assert_eq!(BitUint::<u16, 7>::BITS, 7);
Source§

impl<const N: u32> BitUint<u32, N>

Source

pub const MIN: Self

The smallest value that can be represented by this BitUint.

The value is always 0.

§Examples
assert_eq!(BitUint::<u32, 7>::MIN.get(), 0);
Source

pub const MAX: Self

The largest value that can be represented by this BitUint.

§Examples
assert_eq!(BitUint::<u32, 7>::MAX.get(), 127);
Source

pub const BITS: u32 = N

The size of this BitUint in bits.

§Examples
assert_eq!(BitUint::<u32, 7>::BITS, 7);
Source§

impl<const N: u32> BitUint<u64, N>

Source

pub const MIN: Self

The smallest value that can be represented by this BitUint.

The value is always 0.

§Examples
assert_eq!(BitUint::<u64, 7>::MIN.get(), 0);
Source

pub const MAX: Self

The largest value that can be represented by this BitUint.

§Examples
assert_eq!(BitUint::<u64, 7>::MAX.get(), 127);
Source

pub const BITS: u32 = N

The size of this BitUint in bits.

§Examples
assert_eq!(BitUint::<u64, 7>::BITS, 7);
Source§

impl<const N: u32> BitUint<u128, N>

Source

pub const MIN: Self

The smallest value that can be represented by this BitUint.

The value is always 0.

§Examples
assert_eq!(BitUint::<u128, 7>::MIN.get(), 0);
Source

pub const MAX: Self

The largest value that can be represented by this BitUint.

§Examples
assert_eq!(BitUint::<u128, 7>::MAX.get(), 127);
Source

pub const BITS: u32 = N

The size of this BitUint in bits.

§Examples
assert_eq!(BitUint::<u128, 7>::BITS, 7);
Source§

impl<const N: u32> BitUint<usize, N>

Source

pub const MIN: Self

The smallest value that can be represented by this BitUint.

The value is always 0.

§Examples
assert_eq!(BitUint::<usize, 7>::MIN.get(), 0);
Source

pub const MAX: Self

The largest value that can be represented by this BitUint.

§Examples
assert_eq!(BitUint::<usize, 7>::MAX.get(), 127);
Source

pub const BITS: u32 = N

The size of this BitUint in bits.

§Examples
assert_eq!(BitUint::<usize, 7>::BITS, 7);
Source§

impl<const N: u32> BitUint<u8, N>

Source

pub const fn checked_add(self, rhs: u8) -> Option<Self>

Calculates the addition of self and rhs.

Returns None if overflow occurred.

§Examples
let n = BitUint::<u8, 6>::new(42).unwrap();

assert_eq!(n.checked_add(21).map(BitUint::get), Some(63));
assert_eq!(n.checked_add(22), None);
Source

pub const fn checked_sub(self, rhs: u8) -> Option<Self>

Calculates the subtraction of rhs from self.

Returns None if overflow occurred.

§Examples
let n = BitUint::<u8, 6>::new(42).unwrap();

assert_eq!(n.checked_sub(42).map(BitUint::get), Some(0));
assert_eq!(n.checked_sub(43), None);
Source

pub const fn checked_mul(self, rhs: u8) -> Option<Self>

Calculates the multiplication of self and rhs.

Returns None if overflow occurred.

§Examples
let n = BitUint::<u8, 6>::new(21).unwrap();

assert_eq!(n.checked_mul(2).map(BitUint::get), Some(42));
assert_eq!(n.checked_mul(4), None);
Source

pub const fn checked_div(self, rhs: u8) -> Option<Self>

Calculates the divisor when self is divided by rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<u8, 6>::new(42).unwrap();

assert_eq!(n.checked_div(2).map(BitUint::get), Some(21));
assert_eq!(n.checked_div(0), None);
Source

pub const fn checked_div_euclid(self, rhs: u8) -> Option<Self>

Calculates the quotient of Euclidean division of self by rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<u8, 6>::new(42).unwrap();

assert_eq!(n.checked_div_euclid(2).map(BitUint::get), Some(21));
assert_eq!(n.checked_div_euclid(0), None);
Source

pub const fn checked_rem(self, rhs: u8) -> Option<Self>

Calculates the remainder when self is divided by rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<u8, 3>::new(5).unwrap();

assert_eq!(n.checked_rem(2).map(BitUint::get), Some(1));
assert_eq!(n.checked_rem(0), None);
Source

pub const fn checked_rem_euclid(self, rhs: u8) -> Option<Self>

Calculates the multiplication of self and rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<u8, 3>::new(5).unwrap();

assert_eq!(n.checked_rem_euclid(2).map(BitUint::get), Some(1));
assert_eq!(n.checked_rem_euclid(0), None);
Source

pub const fn checked_ilog(self, base: u8) -> Option<u32>

Returns the logarithm of the number with respect to an arbitrary base, rounded down.

Returns None if the number is zero, or if the base is not at least 2.

§Examples
let n = BitUint::<u8, 3>::new(5).unwrap();

assert_eq!(n.checked_ilog(5), Some(1));
Source

pub const fn checked_ilog2(self) -> Option<u32>

Returns the base 2 logarithm of the number, rounded down.

Returns None if the number is zero.

§Examples
let n = BitUint::<u8, 2>::new(2).unwrap();

assert_eq!(n.checked_ilog2(), Some(1));
Source

pub const fn checked_ilog10(self) -> Option<u32>

Returns the base 10 logarithm of the number, rounded down.

Returns None if the number is zero.

§Examples
let n = BitUint::<u8, 4>::new(10).unwrap();

assert_eq!(n.checked_ilog10(), Some(1));
Source

pub const fn checked_neg(self) -> Option<Self>

Negates self.

Returns None unless self is 0.

Note that negating any positive integer will overflow.

§Examples
assert_eq!(
    BitUint::<u8, 1>::MIN.checked_neg().map(BitUint::get),
    Some(0)
);
assert_eq!(BitUint::<u8, 1>::MAX.checked_neg(), None);
Source

pub const fn checked_shl(self, rhs: u32) -> Option<Self>

Shifts self left by rhs bits.

Returns None if rhs is larger than or equal to the number of bits in self.

§Examples
let n = BitUint::<u8, 5>::new(0x01).unwrap();
let m = BitUint::<u8, 5>::new(0x10).unwrap();

assert_eq!(n.checked_shl(4).map(BitUint::get), Some(0x10));
assert_eq!(m.checked_shl(129), None);
assert_eq!(m.checked_shl(u8::BITS - 1).map(BitUint::get), Some(0x00));
Source

pub const fn checked_shr(self, rhs: u32) -> Option<Self>

Shifts self right by rhs bits.

Returns None if rhs is larger than or equal to the number of bits in self.

§Examples
let n = BitUint::<u8, 5>::new(0x10).unwrap();

assert_eq!(n.checked_shr(4).map(BitUint::get), Some(0x01));
assert_eq!(n.checked_shr(129), None);
Source

pub const fn checked_pow(self, exp: u32) -> Option<Self>

Raises self to the power of exp, using exponentiation by squaring.

Returns None if overflow occurred.

§Examples
let n = BitUint::<u8, 6>::new(2).unwrap();

assert_eq!(n.checked_pow(5).map(BitUint::get), Some(32));
assert_eq!(BitUint::<u8, 6>::MAX.checked_pow(2), None);
Source§

impl<const N: u32> BitUint<u16, N>

Source

pub const fn checked_add(self, rhs: u16) -> Option<Self>

Calculates the addition of self and rhs.

Returns None if overflow occurred.

§Examples
let n = BitUint::<u16, 6>::new(42).unwrap();

assert_eq!(n.checked_add(21).map(BitUint::get), Some(63));
assert_eq!(n.checked_add(22), None);
Source

pub const fn checked_sub(self, rhs: u16) -> Option<Self>

Calculates the subtraction of rhs from self.

Returns None if overflow occurred.

§Examples
let n = BitUint::<u16, 6>::new(42).unwrap();

assert_eq!(n.checked_sub(42).map(BitUint::get), Some(0));
assert_eq!(n.checked_sub(43), None);
Source

pub const fn checked_mul(self, rhs: u16) -> Option<Self>

Calculates the multiplication of self and rhs.

Returns None if overflow occurred.

§Examples
let n = BitUint::<u16, 6>::new(21).unwrap();

assert_eq!(n.checked_mul(2).map(BitUint::get), Some(42));
assert_eq!(n.checked_mul(4), None);
Source

pub const fn checked_div(self, rhs: u16) -> Option<Self>

Calculates the divisor when self is divided by rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<u16, 6>::new(42).unwrap();

assert_eq!(n.checked_div(2).map(BitUint::get), Some(21));
assert_eq!(n.checked_div(0), None);
Source

pub const fn checked_div_euclid(self, rhs: u16) -> Option<Self>

Calculates the quotient of Euclidean division of self by rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<u16, 6>::new(42).unwrap();

assert_eq!(n.checked_div_euclid(2).map(BitUint::get), Some(21));
assert_eq!(n.checked_div_euclid(0), None);
Source

pub const fn checked_rem(self, rhs: u16) -> Option<Self>

Calculates the remainder when self is divided by rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<u16, 3>::new(5).unwrap();

assert_eq!(n.checked_rem(2).map(BitUint::get), Some(1));
assert_eq!(n.checked_rem(0), None);
Source

pub const fn checked_rem_euclid(self, rhs: u16) -> Option<Self>

Calculates the multiplication of self and rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<u16, 3>::new(5).unwrap();

assert_eq!(n.checked_rem_euclid(2).map(BitUint::get), Some(1));
assert_eq!(n.checked_rem_euclid(0), None);
Source

pub const fn checked_ilog(self, base: u16) -> Option<u32>

Returns the logarithm of the number with respect to an arbitrary base, rounded down.

Returns None if the number is zero, or if the base is not at least 2.

§Examples
let n = BitUint::<u16, 3>::new(5).unwrap();

assert_eq!(n.checked_ilog(5), Some(1));
Source

pub const fn checked_ilog2(self) -> Option<u32>

Returns the base 2 logarithm of the number, rounded down.

Returns None if the number is zero.

§Examples
let n = BitUint::<u16, 2>::new(2).unwrap();

assert_eq!(n.checked_ilog2(), Some(1));
Source

pub const fn checked_ilog10(self) -> Option<u32>

Returns the base 10 logarithm of the number, rounded down.

Returns None if the number is zero.

§Examples
let n = BitUint::<u16, 4>::new(10).unwrap();

assert_eq!(n.checked_ilog10(), Some(1));
Source

pub const fn checked_neg(self) -> Option<Self>

Negates self.

Returns None unless self is 0.

Note that negating any positive integer will overflow.

§Examples
assert_eq!(
    BitUint::<u16, 1>::MIN.checked_neg().map(BitUint::get),
    Some(0)
);
assert_eq!(BitUint::<u16, 1>::MAX.checked_neg(), None);
Source

pub const fn checked_shl(self, rhs: u32) -> Option<Self>

Shifts self left by rhs bits.

Returns None if rhs is larger than or equal to the number of bits in self.

§Examples
let n = BitUint::<u16, 5>::new(0x01).unwrap();
let m = BitUint::<u16, 5>::new(0x10).unwrap();

assert_eq!(n.checked_shl(4).map(BitUint::get), Some(0x10));
assert_eq!(m.checked_shl(129), None);
assert_eq!(m.checked_shl(u16::BITS - 1).map(BitUint::get), Some(0x00));
Source

pub const fn checked_shr(self, rhs: u32) -> Option<Self>

Shifts self right by rhs bits.

Returns None if rhs is larger than or equal to the number of bits in self.

§Examples
let n = BitUint::<u16, 5>::new(0x10).unwrap();

assert_eq!(n.checked_shr(4).map(BitUint::get), Some(0x01));
assert_eq!(n.checked_shr(129), None);
Source

pub const fn checked_pow(self, exp: u32) -> Option<Self>

Raises self to the power of exp, using exponentiation by squaring.

Returns None if overflow occurred.

§Examples
let n = BitUint::<u16, 6>::new(2).unwrap();

assert_eq!(n.checked_pow(5).map(BitUint::get), Some(32));
assert_eq!(BitUint::<u16, 6>::MAX.checked_pow(2), None);
Source§

impl<const N: u32> BitUint<u32, N>

Source

pub const fn checked_add(self, rhs: u32) -> Option<Self>

Calculates the addition of self and rhs.

Returns None if overflow occurred.

§Examples
let n = BitUint::<u32, 6>::new(42).unwrap();

assert_eq!(n.checked_add(21).map(BitUint::get), Some(63));
assert_eq!(n.checked_add(22), None);
Source

pub const fn checked_sub(self, rhs: u32) -> Option<Self>

Calculates the subtraction of rhs from self.

Returns None if overflow occurred.

§Examples
let n = BitUint::<u32, 6>::new(42).unwrap();

assert_eq!(n.checked_sub(42).map(BitUint::get), Some(0));
assert_eq!(n.checked_sub(43), None);
Source

pub const fn checked_mul(self, rhs: u32) -> Option<Self>

Calculates the multiplication of self and rhs.

Returns None if overflow occurred.

§Examples
let n = BitUint::<u32, 6>::new(21).unwrap();

assert_eq!(n.checked_mul(2).map(BitUint::get), Some(42));
assert_eq!(n.checked_mul(4), None);
Source

pub const fn checked_div(self, rhs: u32) -> Option<Self>

Calculates the divisor when self is divided by rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<u32, 6>::new(42).unwrap();

assert_eq!(n.checked_div(2).map(BitUint::get), Some(21));
assert_eq!(n.checked_div(0), None);
Source

pub const fn checked_div_euclid(self, rhs: u32) -> Option<Self>

Calculates the quotient of Euclidean division of self by rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<u32, 6>::new(42).unwrap();

assert_eq!(n.checked_div_euclid(2).map(BitUint::get), Some(21));
assert_eq!(n.checked_div_euclid(0), None);
Source

pub const fn checked_rem(self, rhs: u32) -> Option<Self>

Calculates the remainder when self is divided by rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<u32, 3>::new(5).unwrap();

assert_eq!(n.checked_rem(2).map(BitUint::get), Some(1));
assert_eq!(n.checked_rem(0), None);
Source

pub const fn checked_rem_euclid(self, rhs: u32) -> Option<Self>

Calculates the multiplication of self and rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<u32, 3>::new(5).unwrap();

assert_eq!(n.checked_rem_euclid(2).map(BitUint::get), Some(1));
assert_eq!(n.checked_rem_euclid(0), None);
Source

pub const fn checked_ilog(self, base: u32) -> Option<u32>

Returns the logarithm of the number with respect to an arbitrary base, rounded down.

Returns None if the number is zero, or if the base is not at least 2.

§Examples
let n = BitUint::<u32, 3>::new(5).unwrap();

assert_eq!(n.checked_ilog(5), Some(1));
Source

pub const fn checked_ilog2(self) -> Option<u32>

Returns the base 2 logarithm of the number, rounded down.

Returns None if the number is zero.

§Examples
let n = BitUint::<u32, 2>::new(2).unwrap();

assert_eq!(n.checked_ilog2(), Some(1));
Source

pub const fn checked_ilog10(self) -> Option<u32>

Returns the base 10 logarithm of the number, rounded down.

Returns None if the number is zero.

§Examples
let n = BitUint::<u32, 4>::new(10).unwrap();

assert_eq!(n.checked_ilog10(), Some(1));
Source

pub const fn checked_neg(self) -> Option<Self>

Negates self.

Returns None unless self is 0.

Note that negating any positive integer will overflow.

§Examples
assert_eq!(
    BitUint::<u32, 1>::MIN.checked_neg().map(BitUint::get),
    Some(0)
);
assert_eq!(BitUint::<u32, 1>::MAX.checked_neg(), None);
Source

pub const fn checked_shl(self, rhs: u32) -> Option<Self>

Shifts self left by rhs bits.

Returns None if rhs is larger than or equal to the number of bits in self.

§Examples
let n = BitUint::<u32, 5>::new(0x01).unwrap();
let m = BitUint::<u32, 5>::new(0x10).unwrap();

assert_eq!(n.checked_shl(4).map(BitUint::get), Some(0x10));
assert_eq!(m.checked_shl(129), None);
assert_eq!(m.checked_shl(u32::BITS - 1).map(BitUint::get), Some(0x00));
Source

pub const fn checked_shr(self, rhs: u32) -> Option<Self>

Shifts self right by rhs bits.

Returns None if rhs is larger than or equal to the number of bits in self.

§Examples
let n = BitUint::<u32, 5>::new(0x10).unwrap();

assert_eq!(n.checked_shr(4).map(BitUint::get), Some(0x01));
assert_eq!(n.checked_shr(129), None);
Source

pub const fn checked_pow(self, exp: u32) -> Option<Self>

Raises self to the power of exp, using exponentiation by squaring.

Returns None if overflow occurred.

§Examples
let n = BitUint::<u32, 6>::new(2).unwrap();

assert_eq!(n.checked_pow(5).map(BitUint::get), Some(32));
assert_eq!(BitUint::<u32, 6>::MAX.checked_pow(2), None);
Source§

impl<const N: u32> BitUint<u64, N>

Source

pub const fn checked_add(self, rhs: u64) -> Option<Self>

Calculates the addition of self and rhs.

Returns None if overflow occurred.

§Examples
let n = BitUint::<u64, 6>::new(42).unwrap();

assert_eq!(n.checked_add(21).map(BitUint::get), Some(63));
assert_eq!(n.checked_add(22), None);
Source

pub const fn checked_sub(self, rhs: u64) -> Option<Self>

Calculates the subtraction of rhs from self.

Returns None if overflow occurred.

§Examples
let n = BitUint::<u64, 6>::new(42).unwrap();

assert_eq!(n.checked_sub(42).map(BitUint::get), Some(0));
assert_eq!(n.checked_sub(43), None);
Source

pub const fn checked_mul(self, rhs: u64) -> Option<Self>

Calculates the multiplication of self and rhs.

Returns None if overflow occurred.

§Examples
let n = BitUint::<u64, 6>::new(21).unwrap();

assert_eq!(n.checked_mul(2).map(BitUint::get), Some(42));
assert_eq!(n.checked_mul(4), None);
Source

pub const fn checked_div(self, rhs: u64) -> Option<Self>

Calculates the divisor when self is divided by rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<u64, 6>::new(42).unwrap();

assert_eq!(n.checked_div(2).map(BitUint::get), Some(21));
assert_eq!(n.checked_div(0), None);
Source

pub const fn checked_div_euclid(self, rhs: u64) -> Option<Self>

Calculates the quotient of Euclidean division of self by rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<u64, 6>::new(42).unwrap();

assert_eq!(n.checked_div_euclid(2).map(BitUint::get), Some(21));
assert_eq!(n.checked_div_euclid(0), None);
Source

pub const fn checked_rem(self, rhs: u64) -> Option<Self>

Calculates the remainder when self is divided by rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<u64, 3>::new(5).unwrap();

assert_eq!(n.checked_rem(2).map(BitUint::get), Some(1));
assert_eq!(n.checked_rem(0), None);
Source

pub const fn checked_rem_euclid(self, rhs: u64) -> Option<Self>

Calculates the multiplication of self and rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<u64, 3>::new(5).unwrap();

assert_eq!(n.checked_rem_euclid(2).map(BitUint::get), Some(1));
assert_eq!(n.checked_rem_euclid(0), None);
Source

pub const fn checked_ilog(self, base: u64) -> Option<u32>

Returns the logarithm of the number with respect to an arbitrary base, rounded down.

Returns None if the number is zero, or if the base is not at least 2.

§Examples
let n = BitUint::<u64, 3>::new(5).unwrap();

assert_eq!(n.checked_ilog(5), Some(1));
Source

pub const fn checked_ilog2(self) -> Option<u32>

Returns the base 2 logarithm of the number, rounded down.

Returns None if the number is zero.

§Examples
let n = BitUint::<u64, 2>::new(2).unwrap();

assert_eq!(n.checked_ilog2(), Some(1));
Source

pub const fn checked_ilog10(self) -> Option<u32>

Returns the base 10 logarithm of the number, rounded down.

Returns None if the number is zero.

§Examples
let n = BitUint::<u64, 4>::new(10).unwrap();

assert_eq!(n.checked_ilog10(), Some(1));
Source

pub const fn checked_neg(self) -> Option<Self>

Negates self.

Returns None unless self is 0.

Note that negating any positive integer will overflow.

§Examples
assert_eq!(
    BitUint::<u64, 1>::MIN.checked_neg().map(BitUint::get),
    Some(0)
);
assert_eq!(BitUint::<u64, 1>::MAX.checked_neg(), None);
Source

pub const fn checked_shl(self, rhs: u32) -> Option<Self>

Shifts self left by rhs bits.

Returns None if rhs is larger than or equal to the number of bits in self.

§Examples
let n = BitUint::<u64, 5>::new(0x01).unwrap();
let m = BitUint::<u64, 5>::new(0x10).unwrap();

assert_eq!(n.checked_shl(4).map(BitUint::get), Some(0x10));
assert_eq!(m.checked_shl(129), None);
assert_eq!(m.checked_shl(u64::BITS - 1).map(BitUint::get), Some(0x00));
Source

pub const fn checked_shr(self, rhs: u32) -> Option<Self>

Shifts self right by rhs bits.

Returns None if rhs is larger than or equal to the number of bits in self.

§Examples
let n = BitUint::<u64, 5>::new(0x10).unwrap();

assert_eq!(n.checked_shr(4).map(BitUint::get), Some(0x01));
assert_eq!(n.checked_shr(129), None);
Source

pub const fn checked_pow(self, exp: u32) -> Option<Self>

Raises self to the power of exp, using exponentiation by squaring.

Returns None if overflow occurred.

§Examples
let n = BitUint::<u64, 6>::new(2).unwrap();

assert_eq!(n.checked_pow(5).map(BitUint::get), Some(32));
assert_eq!(BitUint::<u64, 6>::MAX.checked_pow(2), None);
Source§

impl<const N: u32> BitUint<u128, N>

Source

pub const fn checked_add(self, rhs: u128) -> Option<Self>

Calculates the addition of self and rhs.

Returns None if overflow occurred.

§Examples
let n = BitUint::<u128, 6>::new(42).unwrap();

assert_eq!(n.checked_add(21).map(BitUint::get), Some(63));
assert_eq!(n.checked_add(22), None);
Source

pub const fn checked_sub(self, rhs: u128) -> Option<Self>

Calculates the subtraction of rhs from self.

Returns None if overflow occurred.

§Examples
let n = BitUint::<u128, 6>::new(42).unwrap();

assert_eq!(n.checked_sub(42).map(BitUint::get), Some(0));
assert_eq!(n.checked_sub(43), None);
Source

pub const fn checked_mul(self, rhs: u128) -> Option<Self>

Calculates the multiplication of self and rhs.

Returns None if overflow occurred.

§Examples
let n = BitUint::<u128, 6>::new(21).unwrap();

assert_eq!(n.checked_mul(2).map(BitUint::get), Some(42));
assert_eq!(n.checked_mul(4), None);
Source

pub const fn checked_div(self, rhs: u128) -> Option<Self>

Calculates the divisor when self is divided by rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<u128, 6>::new(42).unwrap();

assert_eq!(n.checked_div(2).map(BitUint::get), Some(21));
assert_eq!(n.checked_div(0), None);
Source

pub const fn checked_div_euclid(self, rhs: u128) -> Option<Self>

Calculates the quotient of Euclidean division of self by rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<u128, 6>::new(42).unwrap();

assert_eq!(n.checked_div_euclid(2).map(BitUint::get), Some(21));
assert_eq!(n.checked_div_euclid(0), None);
Source

pub const fn checked_rem(self, rhs: u128) -> Option<Self>

Calculates the remainder when self is divided by rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<u128, 3>::new(5).unwrap();

assert_eq!(n.checked_rem(2).map(BitUint::get), Some(1));
assert_eq!(n.checked_rem(0), None);
Source

pub const fn checked_rem_euclid(self, rhs: u128) -> Option<Self>

Calculates the multiplication of self and rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<u128, 3>::new(5).unwrap();

assert_eq!(n.checked_rem_euclid(2).map(BitUint::get), Some(1));
assert_eq!(n.checked_rem_euclid(0), None);
Source

pub const fn checked_ilog(self, base: u128) -> Option<u32>

Returns the logarithm of the number with respect to an arbitrary base, rounded down.

Returns None if the number is zero, or if the base is not at least 2.

§Examples
let n = BitUint::<u128, 3>::new(5).unwrap();

assert_eq!(n.checked_ilog(5), Some(1));
Source

pub const fn checked_ilog2(self) -> Option<u32>

Returns the base 2 logarithm of the number, rounded down.

Returns None if the number is zero.

§Examples
let n = BitUint::<u128, 2>::new(2).unwrap();

assert_eq!(n.checked_ilog2(), Some(1));
Source

pub const fn checked_ilog10(self) -> Option<u32>

Returns the base 10 logarithm of the number, rounded down.

Returns None if the number is zero.

§Examples
let n = BitUint::<u128, 4>::new(10).unwrap();

assert_eq!(n.checked_ilog10(), Some(1));
Source

pub const fn checked_neg(self) -> Option<Self>

Negates self.

Returns None unless self is 0.

Note that negating any positive integer will overflow.

§Examples
assert_eq!(
    BitUint::<u128, 1>::MIN.checked_neg().map(BitUint::get),
    Some(0)
);
assert_eq!(BitUint::<u128, 1>::MAX.checked_neg(), None);
Source

pub const fn checked_shl(self, rhs: u32) -> Option<Self>

Shifts self left by rhs bits.

Returns None if rhs is larger than or equal to the number of bits in self.

§Examples
let n = BitUint::<u128, 5>::new(0x01).unwrap();
let m = BitUint::<u128, 5>::new(0x10).unwrap();

assert_eq!(n.checked_shl(4).map(BitUint::get), Some(0x10));
assert_eq!(m.checked_shl(129), None);
assert_eq!(m.checked_shl(u128::BITS - 1).map(BitUint::get), Some(0x00));
Source

pub const fn checked_shr(self, rhs: u32) -> Option<Self>

Shifts self right by rhs bits.

Returns None if rhs is larger than or equal to the number of bits in self.

§Examples
let n = BitUint::<u128, 5>::new(0x10).unwrap();

assert_eq!(n.checked_shr(4).map(BitUint::get), Some(0x01));
assert_eq!(n.checked_shr(129), None);
Source

pub const fn checked_pow(self, exp: u32) -> Option<Self>

Raises self to the power of exp, using exponentiation by squaring.

Returns None if overflow occurred.

§Examples
let n = BitUint::<u128, 6>::new(2).unwrap();

assert_eq!(n.checked_pow(5).map(BitUint::get), Some(32));
assert_eq!(BitUint::<u128, 6>::MAX.checked_pow(2), None);
Source§

impl<const N: u32> BitUint<usize, N>

Source

pub const fn checked_add(self, rhs: usize) -> Option<Self>

Calculates the addition of self and rhs.

Returns None if overflow occurred.

§Examples
let n = BitUint::<usize, 6>::new(42).unwrap();

assert_eq!(n.checked_add(21).map(BitUint::get), Some(63));
assert_eq!(n.checked_add(22), None);
Source

pub const fn checked_sub(self, rhs: usize) -> Option<Self>

Calculates the subtraction of rhs from self.

Returns None if overflow occurred.

§Examples
let n = BitUint::<usize, 6>::new(42).unwrap();

assert_eq!(n.checked_sub(42).map(BitUint::get), Some(0));
assert_eq!(n.checked_sub(43), None);
Source

pub const fn checked_mul(self, rhs: usize) -> Option<Self>

Calculates the multiplication of self and rhs.

Returns None if overflow occurred.

§Examples
let n = BitUint::<usize, 6>::new(21).unwrap();

assert_eq!(n.checked_mul(2).map(BitUint::get), Some(42));
assert_eq!(n.checked_mul(4), None);
Source

pub const fn checked_div(self, rhs: usize) -> Option<Self>

Calculates the divisor when self is divided by rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<usize, 6>::new(42).unwrap();

assert_eq!(n.checked_div(2).map(BitUint::get), Some(21));
assert_eq!(n.checked_div(0), None);
Source

pub const fn checked_div_euclid(self, rhs: usize) -> Option<Self>

Calculates the quotient of Euclidean division of self by rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<usize, 6>::new(42).unwrap();

assert_eq!(n.checked_div_euclid(2).map(BitUint::get), Some(21));
assert_eq!(n.checked_div_euclid(0), None);
Source

pub const fn checked_rem(self, rhs: usize) -> Option<Self>

Calculates the remainder when self is divided by rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<usize, 3>::new(5).unwrap();

assert_eq!(n.checked_rem(2).map(BitUint::get), Some(1));
assert_eq!(n.checked_rem(0), None);
Source

pub const fn checked_rem_euclid(self, rhs: usize) -> Option<Self>

Calculates the multiplication of self and rhs.

Returns None if rhs is 0.

§Examples
let n = BitUint::<usize, 3>::new(5).unwrap();

assert_eq!(n.checked_rem_euclid(2).map(BitUint::get), Some(1));
assert_eq!(n.checked_rem_euclid(0), None);
Source

pub const fn checked_ilog(self, base: usize) -> Option<u32>

Returns the logarithm of the number with respect to an arbitrary base, rounded down.

Returns None if the number is zero, or if the base is not at least 2.

§Examples
let n = BitUint::<usize, 3>::new(5).unwrap();

assert_eq!(n.checked_ilog(5), Some(1));
Source

pub const fn checked_ilog2(self) -> Option<u32>

Returns the base 2 logarithm of the number, rounded down.

Returns None if the number is zero.

§Examples
let n = BitUint::<usize, 2>::new(2).unwrap();

assert_eq!(n.checked_ilog2(), Some(1));
Source

pub const fn checked_ilog10(self) -> Option<u32>

Returns the base 10 logarithm of the number, rounded down.

Returns None if the number is zero.

§Examples
let n = BitUint::<usize, 4>::new(10).unwrap();

assert_eq!(n.checked_ilog10(), Some(1));
Source

pub const fn checked_neg(self) -> Option<Self>

Negates self.

Returns None unless self is 0.

Note that negating any positive integer will overflow.

§Examples
assert_eq!(
    BitUint::<usize, 1>::MIN.checked_neg().map(BitUint::get),
    Some(0)
);
assert_eq!(BitUint::<usize, 1>::MAX.checked_neg(), None);
Source

pub const fn checked_shl(self, rhs: u32) -> Option<Self>

Shifts self left by rhs bits.

Returns None if rhs is larger than or equal to the number of bits in self.

§Examples
let n = BitUint::<usize, 5>::new(0x01).unwrap();
let m = BitUint::<usize, 5>::new(0x10).unwrap();

assert_eq!(n.checked_shl(4).map(BitUint::get), Some(0x10));
assert_eq!(m.checked_shl(129), None);
assert_eq!(m.checked_shl(usize::BITS - 1).map(BitUint::get), Some(0x00));
Source

pub const fn checked_shr(self, rhs: u32) -> Option<Self>

Shifts self right by rhs bits.

Returns None if rhs is larger than or equal to the number of bits in self.

§Examples
let n = BitUint::<usize, 5>::new(0x10).unwrap();

assert_eq!(n.checked_shr(4).map(BitUint::get), Some(0x01));
assert_eq!(n.checked_shr(129), None);
Source

pub const fn checked_pow(self, exp: u32) -> Option<Self>

Raises self to the power of exp, using exponentiation by squaring.

Returns None if overflow occurred.

§Examples
let n = BitUint::<usize, 6>::new(2).unwrap();

assert_eq!(n.checked_pow(5).map(BitUint::get), Some(32));
assert_eq!(BitUint::<usize, 6>::MAX.checked_pow(2), None);
Source§

impl<const N: u32> BitUint<u8, N>

Source

pub const fn new(n: u8) -> Option<Self>

Creates a new BitUint with the given unsigned integer value.

Returns None if the value is not a valid N-bit unsigned integer.

§Examples
let n = BitUint::<u8, 6>::new(42);
assert_eq!(n.map(BitUint::get), Some(42));

let m = BitUint::<u8, 5>::new(42);
assert_eq!(m, None);
Source§

impl<const N: u32> BitUint<u16, N>

Source

pub const fn new(n: u16) -> Option<Self>

Creates a new BitUint with the given unsigned integer value.

Returns None if the value is not a valid N-bit unsigned integer.

§Examples
let n = BitUint::<u16, 6>::new(42);
assert_eq!(n.map(BitUint::get), Some(42));

let m = BitUint::<u16, 5>::new(42);
assert_eq!(m, None);
Source§

impl<const N: u32> BitUint<u32, N>

Source

pub const fn new(n: u32) -> Option<Self>

Creates a new BitUint with the given unsigned integer value.

Returns None if the value is not a valid N-bit unsigned integer.

§Examples
let n = BitUint::<u32, 6>::new(42);
assert_eq!(n.map(BitUint::get), Some(42));

let m = BitUint::<u32, 5>::new(42);
assert_eq!(m, None);
Source§

impl<const N: u32> BitUint<u64, N>

Source

pub const fn new(n: u64) -> Option<Self>

Creates a new BitUint with the given unsigned integer value.

Returns None if the value is not a valid N-bit unsigned integer.

§Examples
let n = BitUint::<u64, 6>::new(42);
assert_eq!(n.map(BitUint::get), Some(42));

let m = BitUint::<u64, 5>::new(42);
assert_eq!(m, None);
Source§

impl<const N: u32> BitUint<u128, N>

Source

pub const fn new(n: u128) -> Option<Self>

Creates a new BitUint with the given unsigned integer value.

Returns None if the value is not a valid N-bit unsigned integer.

§Examples
let n = BitUint::<u128, 6>::new(42);
assert_eq!(n.map(BitUint::get), Some(42));

let m = BitUint::<u128, 5>::new(42);
assert_eq!(m, None);
Source§

impl<const N: u32> BitUint<usize, N>

Source

pub const fn new(n: usize) -> Option<Self>

Creates a new BitUint with the given unsigned integer value.

Returns None if the value is not a valid N-bit unsigned integer.

§Examples
let n = BitUint::<usize, 6>::new(42);
assert_eq!(n.map(BitUint::get), Some(42));

let m = BitUint::<usize, 5>::new(42);
assert_eq!(m, None);
Source§

impl<T: Unsigned + PrimInt, const N: u32> BitUint<T, N>

Source

pub const unsafe fn new_unchecked(n: T) -> Self

Creates a new BitUint with the given unsigned integer value.

This method does not check whether the value is a valid N-bit unsigned integer. This results in undefined behaviour if the value is not a valid N-bit unsigned integer.

§Safety

The value must be a valid N-bit unsigned integer.

Source

pub const fn get(self) -> T

Returns the contained value as the underlying type.

Trait Implementations§

Source§

impl<T: Unsigned + PrimInt + Binary, const N: u32> Binary for BitUint<T, N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Clone + Unsigned + PrimInt, const N: u32> Clone for BitUint<T, N>

Source§

fn clone(&self) -> BitUint<T, N>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Unsigned + PrimInt, const N: u32> Debug for BitUint<T, N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default + Unsigned + PrimInt, const N: u32> Default for BitUint<T, N>

Source§

fn default() -> BitUint<T, N>

Returns the “default value” for a type. Read more
Source§

impl<T: Unsigned + PrimInt + Display, const N: u32> Display for BitUint<T, N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const N: u32> From<BitUint<u128, N>> for u128

Source§

fn from(n: BitUint<u128, N>) -> Self

Converts to this type from the input type.
Source§

impl<const N: u32> From<BitUint<u16, N>> for u16

Source§

fn from(n: BitUint<u16, N>) -> Self

Converts to this type from the input type.
Source§

impl<const N: u32> From<BitUint<u32, N>> for u32

Source§

fn from(n: BitUint<u32, N>) -> Self

Converts to this type from the input type.
Source§

impl<const N: u32> From<BitUint<u64, N>> for u64

Source§

fn from(n: BitUint<u64, N>) -> Self

Converts to this type from the input type.
Source§

impl<const N: u32> From<BitUint<u8, N>> for u8

Source§

fn from(n: BitUint<u8, N>) -> Self

Converts to this type from the input type.
Source§

impl<const N: u32> From<BitUint<usize, N>> for usize

Source§

fn from(n: BitUint<usize, N>) -> Self

Converts to this type from the input type.
Source§

impl<T: Hash + Unsigned + PrimInt, const N: u32> Hash for BitUint<T, N>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: Unsigned + PrimInt + LowerExp, const N: u32> LowerExp for BitUint<T, N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Unsigned + PrimInt + LowerHex, const N: u32> LowerHex for BitUint<T, N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Unsigned + PrimInt + Octal, const N: u32> Octal for BitUint<T, N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Ord + Unsigned + PrimInt, const N: u32> Ord for BitUint<T, N>

Source§

fn cmp(&self, other: &BitUint<T, N>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq + Unsigned + PrimInt, const N: u32> PartialEq for BitUint<T, N>

Source§

fn eq(&self, other: &BitUint<T, N>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd + Unsigned + PrimInt, const N: u32> PartialOrd for BitUint<T, N>

Source§

fn partial_cmp(&self, other: &BitUint<T, N>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Unsigned + PrimInt + UpperExp, const N: u32> UpperExp for BitUint<T, N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Unsigned + PrimInt + UpperHex, const N: u32> UpperHex for BitUint<T, N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Copy + Unsigned + PrimInt, const N: u32> Copy for BitUint<T, N>

Source§

impl<T: Eq + Unsigned + PrimInt, const N: u32> Eq for BitUint<T, N>

Source§

impl<T: Unsigned + PrimInt, const N: u32> StructuralPartialEq for BitUint<T, N>

Auto Trait Implementations§

§

impl<T, const N: u32> Freeze for BitUint<T, N>
where T: Freeze,

§

impl<T, const N: u32> RefUnwindSafe for BitUint<T, N>
where T: RefUnwindSafe,

§

impl<T, const N: u32> Send for BitUint<T, N>
where T: Send,

§

impl<T, const N: u32> Sync for BitUint<T, N>
where T: Sync,

§

impl<T, const N: u32> Unpin for BitUint<T, N>
where T: Unpin,

§

impl<T, const N: u32> UnwindSafe for BitUint<T, N>
where T: UnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.