Struct std::u128::U128

pub struct U128 {
    /// The most significant 64 bits of the `U128`.
    upper: u64,
    /// The least significant 64 bits of the `U128`.
    lower: u64,
}
Expand description

The 128-bit unsigned integer type.

Additional Information

Represented as two 64-bit components: (upper, lower), where value = (upper << 64) + lower.

Fields

upper: u64

The most significant 64 bits of the U128.

lower: u64

The least significant 64 bits of the U128.

Implementations

fn new() -> Self

Initializes a new, zeroed U128.

Returns

  • [U128] - A new, zero value U128.

Examples

use std::u128::U128;

fn foo() {
    let new_u128 = U128::new();
    let zero_u128 = U128::from(0, 0);

    assert(new_u128 == zero_u128);
}

fn as_u64(self) -> Result<u64, U128Error>

Safely downcast to u64 without loss of precision.

Additional Information

If the U128 is larger than u64::max(), an error is returned.

Returns

  • [Result<u64, U128Error>] - The result of the downcast.

Examples

use std::u128::{U128, U128Error};

fn foo() {
    let zero_u128 = U128::from(0, 0);
    let zero_u64 = zero_u128.as_u64().unwrap();

    assert(zero_u64 == 0);

    let max_u128 = U128::max();
    let result = max_u128.as_u64();

    assert(result.is_err()));
}

fn min() -> Self

The smallest value that can be represented by this integer type.

Returns

  • [U128] - The smallest value that can be represented by this integer type, 0.

Examples

use std::u128::U128;

fn foo() {
    let min_u128 = U128::min();
    let zero_u128 = U128::from(0, 0);

    assert(min_u128 == zero_u128);
}

fn max() -> Self

The largest value that can be represented by this type,

Returns

  • [U128] - The largest value that can be represented by this type, 2<sup>128</sup> - 1.

Examples

use std::u128::U128;

fn foo() {
    let max_u128 = U128::max();
    let maxed_u128 = U128::from(u64::max(), u64::max());

    assert(max_u128 == maxed_u128);
}

fn bits() -> u32

The size of this type in bits.

Returns

  • [u32] - The size of this type in bits, 128.

Examples

use std::u128::U128;

fn foo() {
    let bits = U128::bits();

    assert(bits == 128);
}

fn upper(self) -> u64

Returns the underlying upper u64 representing the most significant 64 bits of the U128.

Returns

  • [u64] - The most significant 64 bits of the U128.

Examples

use std::u128::U128;

fn foo() {
    let maxed_u128 = U128::from(u64::max(), u64::min());

    assert(maxed_u128.upper() == u64::max());
}

fn lower(self) -> u64

Returns the underlying lower u64 representing the least significant 64 bits of the U128.

Returns

  • [u64] - The least significant 64 bits of the U128.

Examples

use std::u128::U128;

fn foo() {
    let maxed_u128 = U128::from(u64::max(), u64::min());

    assert(maxed_u128.lower() == u64::min());
}

fn zero() -> Self

Returns the zero value for the U128 type.

Returns

  • [U128] -> The zero value for the U128 type.

Examples

use std::u128::U128;

fn foo() {
    let zero_u128 = U128::zero();
    assert(zero_u128 == U128::from((0, 0)));
}

fn is_zero(self) -> bool

Returns whether a U128 is set to zero.

Returns

  • [bool] -> True if the U128 is zero, otherwise false.

Examples

use std::u128::U128;

fn foo() {
    let zero_u128 = u128::zero();
    assert(zero_u128.is_zero());
}

Trait Implementations

fn abi_encode(self, buffer: Buffer) -> Buffer

fn abi_decode(refmut buffer: BufferReader) -> Self

fn from(val: u8) -> Self

Converts a u8 to a U128.

Returns

  • [U128] - The U128 representation of the u8 value.

Examples

use std::u128::U128;

fn foo() {
    let u128_value = U128::from(0u8);
}

fn from(val: u16) -> Self

Converts a u16 to a U128.

Returns

  • [U128] - The U128 representation of the u16 value.

Examples

use std::u128::U128;

fn foo() {
    let u128_value = U128::from(0u16);
}

fn from(val: u32) -> Self

Converts a u32 to a U128.

Returns

  • [U128] - The U128 representation of the u32 value.

Examples

use std::u128::U128;

fn foo() {
    let u128_value = U128::from(0u32);
}

fn from(val: u64) -> Self

Converts a u64 to a U128.

Returns

  • [U128] - The U128 representation of the u64 value.

Examples

use std::u128::U128;

fn foo() {
    let u128_value = U128::from(0u64);
}

fn from(components: (u64, u64)) -> Self

fn eq(self, other: Self) -> bool

fn neq(self, other: Self) -> bool

Evaluates if two values of the same type are not equal.

Additional Information

This function is inherited when eq() is implemented.

Arguments

  • other: [Self] - The value of the same type.

Returns

  • [bool] - true if the two values are not equal, otherwise false.

Examples

struct MyStruct {
    val: u64,
}

impl Eq for MyStruct {
    fn eq(self, other: Self) -> bool {
         self.val == other.val
    }
}

fn foo() {
    let struct1 = MyStruct { val: 10 };
    let struct2 = MyStruct { val: 2 };
    let result = struct1 != struct2;
    assert(result);
}

fn gt(self, other: Self) -> bool

fn lt(self, other: Self) -> bool

fn ge(self, other: Self) -> bool

Evaluates if one value of the same type is greater or equal to than another.

Additional Information

This trait requires that the Ord and Eq traits are implemented.

Arguments

  • other: [Self] - The value of the same type.

Returns

  • [bool] - true if self is greater than or equal to other, otherwise false.

Examples

struct MyStruct {
    val: u64,
}

impl Eq for MyStruct {
    fn eq(self, other: Self) -> bool {
        self.val == other.val
    }
}

impl Ord for MyStruct {
    fn gt(self, other: Self) -> bool {
        self.val > other.val
    }
}

impl OrdEq for MyStruct {}

fn foo() {
    let struct1 = MyStruct { val: 10 };
    let struct2 = MyStruct { val: 10 };
    let result = struct1 >= struct2;
    assert(result);
}

fn le(self, other: Self) -> bool

Evaluates if one value of the same type is less or equal to than another.

Additional Information

This trait requires that the Ord and Eq traits are implemented.

Arguments

  • other: [Self] - The value of the same type.

Returns

  • [bool] - true if self is less than or equal to other, otherwise false.

Examples

struct MyStruct {
    val: u64,
}

impl Eq for MyStruct {
    fn eq(self, other: Self) -> bool {
        self.val == other.val
    }
}

impl Ord for MyStruct {
    fn lt(self, other: Self) -> bool {
        self.val < other.val
    }
}

impl OrdEq for MyStruct {}

fn foo() {
    let struct1 = MyStruct { val: 10 };
    let struct2 = MyStruct { val: 10 };
    let result = struct1 <= struct2;
    assert(result);
}

fn binary_and(self, other: Self) -> Self

fn binary_or(self, other: Self) -> Self

fn lsh(self, rhs: u64) -> Self

fn rsh(self, rhs: u64) -> Self

fn not(self) -> Self

fn add(self, other: Self) -> Self

Add a U128 to a U128. Reverts on overflow.

fn subtract(self, other: Self) -> Self

Subtract a U128 from a U128. Reverts on underflow.

fn multiply(self, other: Self) -> Self

Multiply a U128 with a U128. Reverts of overflow.

fn divide(self, divisor: Self) -> Self

Divide a U128 by a U128. Reverts if divisor is zero.

fn modulo(self, other: Self) -> Self

fn pow(self, exponent: u32) -> Self

fn sqrt(self) -> Self

Integer square root using Newton’s Method.

fn log2(self) -> Self

log2 of x is the largest n such that 2^n <= x < 2^(n+1).

  • If x is smaller than 2^64, we could just rely on the log method by setting
    the base to 2.
  • Otherwise, we can find the highest non-zero bit by taking the regular log of the upper
    part of the U128, and then add 64.

fn log(self, base: Self) -> Self

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

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