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: u64The most significant 64 bits of the U128.
lower: u64The least significant 64 bits of the U128.
Implementations
pub fn new() -> Self
pub 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);
}
pub fn try_as_u64(self) -> Result<u64, U128Error>
pub fn try_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.try_as_u64().unwrap();
assert(zero_u64 == 0);
let max_u128 = U128::max();
let result = max_u128.try_as_u64();
assert(result.is_err()));
}
pub fn as_u64(self) -> Result<u64, U128Error>
pub 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.
Deprecated: Use try_as_u64 instead, for consistency with other downcast functions.
Returns
- [Result<u64, U128Error>] - The result of the downcast.
pub fn as_u256(self) -> u256
pub fn as_u256(self) -> u256
Upcasts a U128 to a u256.
Returns
- [u256] - The
u256representation of theU128value.
Examples
use std::u128::U128;
fn foo() {
let u128_value = U128::from(0u64);
let u256_value = u128_value.as_u256();
}
pub fn min() -> Self
pub 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);
}
pub fn max() -> Self
pub 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);
}
pub fn bits() -> u32
pub 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);
}
pub fn upper(self) -> u64
pub 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());
}
pub fn lower(self) -> u64
pub 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());
}
pub fn zero() -> Self
pub fn zero() -> Self
Returns the zero value for the U128 type.
Returns
- [U128] -> The zero value for the
U128type.
Examples
use std::u128::U128;
fn foo() {
let zero_u128 = U128::zero();
assert(zero_u128 == U128::from((0, 0)));
}
pub fn is_zero(self) -> bool
pub fn is_zero(self) -> bool
Returns whether a U128 is set to zero.
Returns
- [bool] -> True if the
U128is zero, otherwise false.
Examples
use std::u128::U128;
fn foo() {
let zero_u128 = u128::zero();
assert(zero_u128.is_zero());
}
Trait Implementations
impl AbiEncode for U128<>
impl AbiEncode for U128<>
pub fn abi_encode(self, buffer: Buffer) -> Buffer
impl AbiDecode for U128<>
impl AbiDecode for U128<>
pub fn abi_decode(refmut buffer: BufferReader) -> Self
impl From<u8> for U128
impl From<u8> for U128
pub fn from(val: u8) -> Self
pub fn from(val: u8) -> Self
Converts a u8 to a U128.
Returns
- [U128] - The
U128representation of theu8value.
Examples
use std::u128::U128;
fn foo() {
let u128_value = U128::from(0u8);
}
impl From<u16> for U128
impl From<u16> for U128
pub fn from(val: u16) -> Self
pub fn from(val: u16) -> Self
Converts a u16 to a U128.
Returns
- [U128] - The
U128representation of theu16value.
Examples
use std::u128::U128;
fn foo() {
let u128_value = U128::from(0u16);
}
impl From<u32> for U128
impl From<u32> for U128
pub fn from(val: u32) -> Self
pub fn from(val: u32) -> Self
Converts a u32 to a U128.
Returns
- [U128] - The
U128representation of theu32value.
Examples
use std::u128::U128;
fn foo() {
let u128_value = U128::from(0u32);
}
impl From<u64> for U128
impl From<u64> for U128
pub fn from(val: u64) -> Self
pub fn from(val: u64) -> Self
Converts a u64 to a U128.
Returns
- [U128] - The
U128representation of theu64value.
Examples
use std::u128::U128;
fn foo() {
let u128_value = U128::from(0u64);
}
impl PartialEq for U128
impl PartialEq for U128
pub fn eq(self, other: Self) -> bool
pub fn neq(self, other: Self) -> bool
pub 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] -
trueif the two values are not equal, otherwisefalse.
Examples
struct MyStruct {
val: u64,
}
impl PartialEq 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);
}
impl Eq for U128
impl OrdEq for U128
impl OrdEq for U128
pub fn ge(self, other: Self) -> bool
pub 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] -
trueifselfis greater than or equal toother, otherwisefalse.
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);
}
pub fn le(self, other: Self) -> bool
pub 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] -
trueifselfis less than or equal toother, otherwisefalse.
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);
}
impl BitwiseAnd for U128
impl BitwiseAnd for U128
pub fn binary_and(self, other: Self) -> Self
impl Subtract for U128
impl Subtract for U128
pub fn subtract(self, other: Self) -> Self
pub fn subtract(self, other: Self) -> Self
Subtract a U128 from a U128. Reverts on underflow.
impl Multiply for U128
impl Multiply for U128
pub fn multiply(self, other: Self) -> Self
pub fn multiply(self, other: Self) -> Self
Multiply a U128 with a U128. Reverts of overflow.
impl Divide for U128
impl Divide for U128
pub fn divide(self, divisor: Self) -> Self
pub fn divide(self, divisor: Self) -> Self
Divide a U128 by a U128. Reverts if divisor is zero.
impl Root for U128
impl Root for U128
pub fn sqrt(self) -> Self
pub fn sqrt(self) -> Self
Integer square root using Newton’s Method.
impl BinaryLogarithm for U128
impl BinaryLogarithm for U128
pub fn log2(self) -> Self
pub fn log2(self) -> Self
log2 of x is the largest n such that 2^n <= x < 2^(n+1).
- If
xis smaller than2^64, we could just rely on thelogmethod 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 theU128, and then add 64.