Struct ibig::UBig[][src]

pub struct UBig(_);

Unsigned big integer.

Arbitrarily large unsigned integer.

Examples

let a = ubig!(a2a123bbb127779cccc123123ccc base 32);
let b = ubig!(0x1231abcd4134);
let c = UBig::from_str_radix("a2a123bbb127779cccc123123ccc", 32)?;
let d = UBig::from_str_radix("1231abcd4134", 16)?;
assert_eq!(a, c);
assert_eq!(b, d);

Implementations

impl UBig[src]

pub fn bit(&self, n: usize) -> bool[src]

Returns true if the n-th bit is set.

Examples

assert_eq!(ubig!(0b10010).bit(1), true);
assert_eq!(ubig!(0b10010).bit(3), false);
assert_eq!(ubig!(0b10010).bit(100), false);

pub fn set_bit(&mut self, n: usize)[src]

Set the n-th bit.

Examples

let mut a = ubig!(0b100);
a.set_bit(0);
assert_eq!(a, ubig!(0b101));
a.set_bit(10);
assert_eq!(a, ubig!(0b10000000101));

pub fn clear_bit(&mut self, n: usize)[src]

Clear the n-th bit.

Examples

let mut a = ubig!(0b101);
a.clear_bit(0);
assert_eq!(a, ubig!(0b100));

pub fn trailing_zeros(&self) -> Option<usize>[src]

Returns the number of trailing zeros in the binary representation.

In other words, it is the smallest n such that 2 to the power of n divides the number.

For 0, it returns None.

Examples

assert_eq!(ubig!(17).trailing_zeros(), Some(0));
assert_eq!(ubig!(48).trailing_zeros(), Some(4));
assert_eq!(ubig!(0b101000000).trailing_zeros(), Some(6));
assert_eq!(ubig!(0).trailing_zeros(), None);

pub fn bit_len(&self) -> usize[src]

Bit length.

The length of the binary representation of the number.

For 0, the length is 0.

For non-zero numbers it is:

  • in_radix(2).to_string().len()
  • the index of the top 1 bit plus one
  • the floor of the logarithm base 2 of the number plus one.

Examples

assert_eq!(ubig!(17).bit_len(), 5);
assert_eq!(ubig!(0b101000000).bit_len(), 9);
assert_eq!(ubig!(0).bit_len(), 0);
let x = ubig!(_0x90ffff3450897234);
assert_eq!(x.bit_len(), x.in_radix(2).to_string().len());

pub fn is_power_of_two(&self) -> bool[src]

True if the number is a power of 2.

Examples

assert_eq!(ubig!(0).is_power_of_two(), false);
assert_eq!(ubig!(8).is_power_of_two(), true);
assert_eq!(ubig!(9).is_power_of_two(), false);

impl UBig[src]

pub fn from_le_bytes(bytes: &[u8]) -> UBig[src]

Construct from little-endian bytes.

Examples

assert_eq!(UBig::from_le_bytes(&[3, 2, 1]), ubig!(0x010203));

pub fn from_be_bytes(bytes: &[u8]) -> UBig[src]

Construct from big-endian bytes.

Examples

assert_eq!(UBig::from_be_bytes(&[1, 2, 3]), ubig!(0x010203));

pub fn to_le_bytes(&self) -> Vec<u8>[src]

Return little-endian bytes.

Examples

assert!(ubig!(0).to_le_bytes().is_empty());
assert_eq!(ubig!(0x010203).to_le_bytes(), [3, 2, 1]);

pub fn to_be_bytes(&self) -> Vec<u8>[src]

Return big-endian bytes.

Examples

assert!(ubig!(0).to_be_bytes().is_empty());
assert_eq!(ubig!(0x010203).to_be_bytes(), [1, 2, 3]);

pub fn to_f32(&self) -> f32[src]

Convert to f32.

Round to nearest, breaking ties to even last bit.

Examples

assert_eq!(ubig!(134).to_f32(), 134.0f32);

pub fn to_f64(&self) -> f64[src]

Convert to f64.

Round to nearest, breaking ties to even last bit.

Examples

assert_eq!(ubig!(134).to_f64(), 134.0f64);

impl UBig[src]

pub fn in_radix(&self, radix: u32) -> InRadix<'_>[src]

Representation in a given radix.

Panics

Panics if radix is not between 2 and 36 inclusive.

Examples

assert_eq!(format!("{}", ubig!(83).in_radix(3)), "10002");
assert_eq!(format!("{:+010}", ubig!(35).in_radix(36)), "+00000000z");

impl UBig[src]

pub fn from_str_radix(src: &str, radix: u32) -> Result<UBig, ParseError>[src]

Convert a string in a given base to UBig.

src may contain an optional + prefix. Digits 10-35 are represented by a-z or A-Z.

Panics

Panics if radix is not between 2 and 36 inclusive.

Examples

assert_eq!(UBig::from_str_radix("+7ab", 32)?, ubig!(7499));

pub fn from_str_with_radix_prefix(src: &str) -> Result<UBig, ParseError>[src]

Convert a string with an optional radix prefix to UBig.

src may contain an optional + after the radix prefix.

Allowed prefixes: 0b for binary, 0o for octal, 0x for hexadecimal.

Examples

assert_eq!(UBig::from_str_with_radix_prefix("+0o17")?, ubig!(0o17));
assert_eq!(UBig::from_str_with_radix_prefix("0x1f")?, ubig!(0x1f));

impl UBig[src]

pub fn pow(&self, exp: usize) -> UBig[src]

Raises self to the power of exp.

Example

assert_eq!(ubig!(3).pow(3), ubig!(27));

impl UBig[src]

pub const MAX_BIT_LEN: usize[src]

Maximum length in bits.

UBigs up to this length are supported. Creating a longer number will panic.

This does not guarantee that there is sufficient memory to store numbers up to this length. Memory allocation may fail even for smaller numbers.

The fact that this limit fits in usize guarantees that all bit addressing operations can be performed using usize.

It is typically close to usize::MAX, but the exact value is platform-dependent.

Trait Implementations

impl Add<&'_ UBig> for UBig[src]

type Output = UBig

The resulting type after applying the + operator.

impl Add<&'_ UBig> for &UBig[src]

type Output = UBig

The resulting type after applying the + operator.

impl Add<UBig> for UBig[src]

type Output = UBig

The resulting type after applying the + operator.

impl Add<UBig> for &UBig[src]

type Output = UBig

The resulting type after applying the + operator.

impl AddAssign<&'_ UBig> for UBig[src]

impl AddAssign<UBig> for UBig[src]

impl AndNot<&'_ UBig> for UBig[src]

type Output = UBig

impl AndNot<&'_ UBig> for &UBig[src]

type Output = UBig

impl AndNot<UBig> for UBig[src]

type Output = UBig

impl AndNot<UBig> for &UBig[src]

type Output = UBig

impl Binary for UBig[src]

impl BitAnd<&'_ UBig> for UBig[src]

type Output = UBig

The resulting type after applying the & operator.

impl BitAnd<&'_ UBig> for &UBig[src]

type Output = UBig

The resulting type after applying the & operator.

impl BitAnd<UBig> for UBig[src]

type Output = UBig

The resulting type after applying the & operator.

impl BitAnd<UBig> for &UBig[src]

type Output = UBig

The resulting type after applying the & operator.

impl BitAndAssign<&'_ UBig> for UBig[src]

impl BitAndAssign<UBig> for UBig[src]

impl BitOr<&'_ UBig> for UBig[src]

type Output = UBig

The resulting type after applying the | operator.

impl BitOr<&'_ UBig> for &UBig[src]

type Output = UBig

The resulting type after applying the | operator.

impl BitOr<UBig> for UBig[src]

type Output = UBig

The resulting type after applying the | operator.

impl BitOr<UBig> for &UBig[src]

type Output = UBig

The resulting type after applying the | operator.

impl BitOrAssign<&'_ UBig> for UBig[src]

impl BitOrAssign<UBig> for UBig[src]

impl BitXor<&'_ UBig> for UBig[src]

type Output = UBig

The resulting type after applying the ^ operator.

impl BitXor<&'_ UBig> for &UBig[src]

type Output = UBig

The resulting type after applying the ^ operator.

impl BitXor<UBig> for UBig[src]

type Output = UBig

The resulting type after applying the ^ operator.

impl BitXor<UBig> for &UBig[src]

type Output = UBig

The resulting type after applying the ^ operator.

impl BitXorAssign<&'_ UBig> for UBig[src]

impl BitXorAssign<UBig> for UBig[src]

impl Clone for UBig[src]

impl Debug for UBig[src]

impl Default for UBig[src]

fn default() -> UBig[src]

Default value: 0.

impl Display for UBig[src]

impl Div<&'_ UBig> for UBig[src]

type Output = UBig

The resulting type after applying the / operator.

impl Div<&'_ UBig> for &UBig[src]

type Output = UBig

The resulting type after applying the / operator.

impl Div<UBig> for UBig[src]

type Output = UBig

The resulting type after applying the / operator.

impl Div<UBig> for &UBig[src]

type Output = UBig

The resulting type after applying the / operator.

impl DivAssign<&'_ UBig> for UBig[src]

impl DivAssign<UBig> for UBig[src]

impl DivEuclid<&'_ UBig> for UBig[src]

type Output = UBig

impl DivEuclid<&'_ UBig> for &UBig[src]

type Output = UBig

impl DivEuclid<UBig> for UBig[src]

type Output = UBig

impl DivEuclid<UBig> for &UBig[src]

type Output = UBig

impl DivRem<&'_ UBig> for UBig[src]

type OutputDiv = UBig

type OutputRem = UBig

impl DivRem<&'_ UBig> for &UBig[src]

type OutputDiv = UBig

type OutputRem = UBig

impl DivRem<UBig> for UBig[src]

type OutputDiv = UBig

type OutputRem = UBig

impl DivRem<UBig> for &UBig[src]

type OutputDiv = UBig

type OutputRem = UBig

impl DivRemEuclid<&'_ UBig> for UBig[src]

impl DivRemEuclid<&'_ UBig> for &UBig[src]

impl DivRemEuclid<UBig> for UBig[src]

impl DivRemEuclid<UBig> for &UBig[src]

impl Eq for UBig[src]

impl From<&'_ UBig> for IBig[src]

impl From<UBig> for IBig[src]

impl From<bool> for UBig[src]

impl From<u128> for UBig[src]

impl From<u16> for UBig[src]

impl From<u32> for UBig[src]

impl From<u64> for UBig[src]

impl From<u8> for UBig[src]

impl From<usize> for UBig[src]

impl FromStr for UBig[src]

type Err = ParseError

The associated error which can be returned from parsing.

impl Hash for UBig[src]

impl IntoModulo for UBig[src]

impl IntoModulo for &UBig[src]

impl LowerHex for UBig[src]

impl Mul<&'_ UBig> for UBig[src]

type Output = UBig

The resulting type after applying the * operator.

impl Mul<&'_ UBig> for &UBig[src]

type Output = UBig

The resulting type after applying the * operator.

impl Mul<UBig> for UBig[src]

type Output = UBig

The resulting type after applying the * operator.

impl Mul<UBig> for &UBig[src]

type Output = UBig

The resulting type after applying the * operator.

impl MulAssign<&'_ UBig> for UBig[src]

impl MulAssign<UBig> for UBig[src]

impl NextPowerOfTwo for UBig[src]

impl NextPowerOfTwo for &UBig[src]

impl Num for UBig[src]

type FromStrRadixErr = ParseError

impl Octal for UBig[src]

impl One for UBig[src]

impl Ord for UBig[src]

impl PartialEq<UBig> for UBig[src]

impl PartialOrd<UBig> for UBig[src]

impl Pow<usize> for UBig[src]

type Output = UBig

The result after applying the operator.

impl Pow<usize> for &UBig[src]

type Output = UBig

The result after applying the operator.

impl Rem<&'_ UBig> for UBig[src]

type Output = UBig

The resulting type after applying the % operator.

impl Rem<&'_ UBig> for &UBig[src]

type Output = UBig

The resulting type after applying the % operator.

impl Rem<UBig> for UBig[src]

type Output = UBig

The resulting type after applying the % operator.

impl Rem<UBig> for &UBig[src]

type Output = UBig

The resulting type after applying the % operator.

impl RemAssign<&'_ UBig> for UBig[src]

impl RemAssign<UBig> for UBig[src]

impl RemEuclid<&'_ UBig> for UBig[src]

type Output = UBig

impl RemEuclid<&'_ UBig> for &UBig[src]

type Output = UBig

impl RemEuclid<UBig> for UBig[src]

type Output = UBig

impl RemEuclid<UBig> for &UBig[src]

type Output = UBig

impl SampleUniform for UBig[src]

type Sampler = UniformUBig

The UniformSampler implementation supporting type X.

impl Shl<&'_ usize> for UBig[src]

type Output = UBig

The resulting type after applying the << operator.

impl Shl<&'_ usize> for &UBig[src]

type Output = UBig

The resulting type after applying the << operator.

impl Shl<usize> for UBig[src]

type Output = UBig

The resulting type after applying the << operator.

impl Shl<usize> for &UBig[src]

type Output = UBig

The resulting type after applying the << operator.

impl ShlAssign<&'_ usize> for UBig[src]

impl ShlAssign<usize> for UBig[src]

impl Shr<&'_ usize> for UBig[src]

type Output = UBig

The resulting type after applying the >> operator.

impl Shr<&'_ usize> for &UBig[src]

type Output = UBig

The resulting type after applying the >> operator.

impl Shr<usize> for UBig[src]

type Output = UBig

The resulting type after applying the >> operator.

impl Shr<usize> for &UBig[src]

type Output = UBig

The resulting type after applying the >> operator.

impl ShrAssign<&'_ usize> for UBig[src]

impl ShrAssign<usize> for UBig[src]

impl StructuralEq for UBig[src]

impl StructuralPartialEq for UBig[src]

impl Sub<&'_ UBig> for UBig[src]

type Output = UBig

The resulting type after applying the - operator.

impl Sub<&'_ UBig> for &UBig[src]

type Output = UBig

The resulting type after applying the - operator.

impl Sub<UBig> for UBig[src]

type Output = UBig

The resulting type after applying the - operator.

impl Sub<UBig> for &UBig[src]

type Output = UBig

The resulting type after applying the - operator.

impl SubAssign<&'_ UBig> for UBig[src]

impl SubAssign<UBig> for UBig[src]

impl TryFrom<&'_ IBig> for UBig[src]

type Error = OutOfBoundsError

The type returned in the event of a conversion error.

impl TryFrom<IBig> for UBig[src]

type Error = OutOfBoundsError

The type returned in the event of a conversion error.

impl TryFrom<i128> for UBig[src]

type Error = OutOfBoundsError

The type returned in the event of a conversion error.

impl TryFrom<i16> for UBig[src]

type Error = OutOfBoundsError

The type returned in the event of a conversion error.

impl TryFrom<i32> for UBig[src]

type Error = OutOfBoundsError

The type returned in the event of a conversion error.

impl TryFrom<i64> for UBig[src]

type Error = OutOfBoundsError

The type returned in the event of a conversion error.

impl TryFrom<i8> for UBig[src]

type Error = OutOfBoundsError

The type returned in the event of a conversion error.

impl TryFrom<isize> for UBig[src]

type Error = OutOfBoundsError

The type returned in the event of a conversion error.

impl Unsigned for UBig[src]

impl UpperHex for UBig[src]

impl Zero for UBig[src]

Auto Trait Implementations

impl RefUnwindSafe for UBig

impl Send for UBig

impl Sync for UBig

impl Unpin for UBig

impl UnwindSafe for UBig

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> NumAssign for T where
    T: Num + NumAssignOps<T>, 
[src]

impl<T, Rhs> NumAssignOps<Rhs> for T where
    T: AddAssign<Rhs> + SubAssign<Rhs> + MulAssign<Rhs> + DivAssign<Rhs> + RemAssign<Rhs>, 
[src]

impl<T> NumAssignRef for T where
    T: NumAssign + for<'r> NumAssignOps<&'r T>, 
[src]

impl<T, Rhs, Output> NumOps<Rhs, Output> for T where
    T: Sub<Rhs, Output = Output> + Mul<Rhs, Output = Output> + Div<Rhs, Output = Output> + Add<Rhs, Output = Output> + Rem<Rhs, Output = Output>, 
[src]

impl<T> NumRef for T where
    T: Num + for<'r> NumOps<&'r T, T>, 
[src]

impl<T, Base> RefNum<Base> for T where
    T: NumOps<Base, Base> + for<'r> NumOps<&'r Base, Base>, 
[src]

impl<Borrowed> SampleBorrow<Borrowed> for Borrowed where
    Borrowed: SampleUniform
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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