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
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>
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
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
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
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
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
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
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
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
impl AbiEncode for U128
impl AbiEncode for U128
fn abi_encode(self, buffer: Buffer) -> Buffer
impl AbiDecode for U128
impl AbiDecode for U128
fn abi_decode(refmut buffer: BufferReader) -> Self
impl From<u8> for U128
impl From<u8> for U128
fn from(val: u8) -> Self
fn from(val: u8) -> Self
Converts a u8
to a U128
.
Returns
- [U128] - The
U128
representation of theu8
value.
Examples
use std::u128::U128;
fn foo() {
let u128_value = U128::from(0u8);
}
impl From<u16> for U128
impl From<u16> for U128
fn from(val: u16) -> Self
fn from(val: u16) -> Self
Converts a u16
to a U128
.
Returns
- [U128] - The
U128
representation of theu16
value.
Examples
use std::u128::U128;
fn foo() {
let u128_value = U128::from(0u16);
}
impl From<u32> for U128
impl From<u32> for U128
fn from(val: u32) -> Self
fn from(val: u32) -> Self
Converts a u32
to a U128
.
Returns
- [U128] - The
U128
representation of theu32
value.
Examples
use std::u128::U128;
fn foo() {
let u128_value = U128::from(0u32);
}
impl From<u64> for U128
impl From<u64> for U128
fn from(val: u64) -> Self
fn from(val: u64) -> Self
Converts a u64
to a U128
.
Returns
- [U128] - The
U128
representation of theu64
value.
Examples
use std::u128::U128;
fn foo() {
let u128_value = U128::from(0u64);
}
impl Eq for U128
impl Eq for U128
fn eq(self, other: Self) -> bool
fn neq(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, otherwisefalse
.
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);
}
impl OrdEq for U128
impl OrdEq for U128
fn ge(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
ifself
is 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);
}
fn le(self, other: Self) -> bool
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
ifself
is 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
fn binary_and(self, other: Self) -> Self
impl Subtract for U128
impl Subtract for U128
fn subtract(self, other: Self) -> Self
fn subtract(self, other: Self) -> Self
Subtract a U128
from a U128
. Reverts on underflow.
impl Multiply for U128
impl Multiply for U128
fn multiply(self, other: Self) -> Self
fn multiply(self, other: Self) -> Self
Multiply a U128
with a U128
. Reverts of overflow.
impl Divide for U128
impl Divide for U128
fn divide(self, divisor: Self) -> Self
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
fn sqrt(self) -> Self
fn sqrt(self) -> Self
Integer square root using Newton’s Method.
impl BinaryLogarithm for U128
impl BinaryLogarithm for U128
fn log2(self) -> Self
fn log2(self) -> Self
log2
of x
is the largest n
such that 2^n <= x < 2^(n+1)
.
- If
x
is smaller than2^64
, we could just rely on thelog
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 theU128
, and then add 64.