u256
Expand description
Implementations
pub fn min() -> Self
pub fn min() -> Self
The smallest value that can be represented by this integer type.
Returns
- [u256] - The smallest
u256
value.
Examples
fn foo() {
let val = u256::min();
assert(val == 0x0000000000000000000000000000000000000000000000000000000000000000u256);
pub fn max() -> Self
pub fn max() -> Self
The largest value that can be represented by this integer type,
2256 - 1.
Returns
- [u256] - The largest
u256
value.
Examples
fn foo() {
let val = u256::max();
assert(val == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFu256);
}
pub fn bits() -> u64
pub fn bits() -> u64
The size of this integer type in bits.
Returns
- [u32] - The number of bits for a
u256
.
Examples
fn foo() {
let bits = u256::bits();
assert(bits == 256);
}
pub fn zero() -> Self
pub fn zero() -> Self
Returns the zero value for the u256
type.
Returns
- [u256] -> The zero value for the
u256
type.
Examples
fn foo() {
let zero_u256 = u256::zero();
assert(zero_u256 == 0x00u256);
}
pub fn is_zero(self) -> bool
pub fn is_zero(self) -> bool
Returns whether a u256
is set to zero.
Returns
- [bool] -> True if the
u256
is zero, otherwise false.
Examples
fn foo() {
let zero_u256 = u256::zero();
assert(zero_u256.is_zero());
}
pub fn as_b256(self) -> b256
pub fn as_b256(self) -> b256
Converts a u256
to a b256
.
Returns
- [b256] - The converted
u256
value.
Examples
fn foo() {
let val: u256 = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20u256;
let result = val.as_b256();
assert(result == 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20);
}
pub fn to_le_bytes(self) -> [u8; 32]
pub fn to_le_bytes(self) -> [u8; 32]
Converts the u256
to a sequence of little-endian bytes.
Returns
- [[u8; 32]] - An array of 32
u8
bytes that compose theu256
.
Examples
fn foo() {
let bytes = [32_u8, 31_u8, 30_u8, 29_u8, 28_u8, 27_u8, 26_u8, 25_u8, 24_u8, 23_u8,
22_u8, 21_u8, 20_u8, 19_u8, 18_u8, 17_u8, 16_u8, 15_u8, 14_u8, 13_u8,
12_u8, 11_u8, 10_u8, 9_u8, 8_u8, 7_u8, 6_u8, 5_u8, 4_u8, 3_u8,
2_u8, 1_u8];
let x = u256::from_le_bytes(bytes);
assert(x == 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20u256);
}
pub fn from_le_bytes(bytes: [u8; 32]) -> Self
pub fn from_le_bytes(bytes: [u8; 32]) -> Self
Converts a sequence of little-endian bytes to a u256
.
Arguments
bytes
: [[u8; 32]] - A sequence of 32u8
bytes that represent au256
.
Returns
- [u256] - The resulting
u256
value.
Examples
fn foo() {
let bytes = [32_u8, 31_u8, 30_u8, 29_u8, 28_u8, 27_u8, 26_u8, 25_u8, 24_u8, 23_u8,
22_u8, 21_u8, 20_u8, 19_u8, 18_u8, 17_u8, 16_u8, 15_u8, 14_u8, 13_u8,
12_u8, 11_u8, 10_u8, 9_u8, 8_u8, 7_u8, 6_u8, 5_u8, 4_u8, 3_u8,
2_u8, 1_u8];
let x = u256::from_le_bytes(bytes);
assert(x == 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20u256;
pub fn to_be_bytes(self) -> [u8; 32]
pub fn to_be_bytes(self) -> [u8; 32]
Converts the u256
to a sequence of big-endian bytes.
Returns
- [[u8; 32]] - An array of 32
u8
bytes that compose theu256
.
Examples
fn foo() {
let x: u256 = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20u256;
let bytes = x.to_be_bytes();
let mut i: u8 = 0;
while i < 32_u8 {
assert(bytes[i.as_u64()] == i + 1_u8);
i += 1_u8;
}
}
pub fn from_be_bytes(bytes: [u8; 32]) -> Self
pub fn from_be_bytes(bytes: [u8; 32]) -> Self
Converts a sequence of big-endian bytes to a u256
.
Arguments
bytes
: [[u8; 32]] - A sequence of 32u8
bytes that represent au256
.
Returns
- [u256] - The resulting
u256
value.
Examples
fn foo() {
let bytes = [1_u8, 2_u8, 3_u8, 4_u8, 5_u8, 6_u8, 7_u8, 8_u8, 9_u8, 10_u8,
11_u8, 12_u8, 13_u8, 14_u8, 15_u8, 16_u8, 17_u8, 18_u8, 19_u8, 20_u8,
21_u8, 22_u8, 23_u8, 24_u8, 25_u8, 26_u8, 27_u8, 28_u8, 29_u8, 30_u8,
31_u8, 32_u8];
let x = u256::from_be_bytes(bytes);
assert(x == 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20u256);
}
pub fn to_le_bytes(self) -> Bytes
pub fn to_le_bytes(self) -> Bytes
Converts the u256
to a sequence of little-endian bytes.
Returns
- [Bytes] - The 32 bytes that compose the
u256
.
Examples
fn foo() {
let x: u256 = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20_u256;
let bytes = x.to_le_bytes();
let mut i: u8 = 0;
while i < 32_u8 {
assert(bytes.get(i.as_u64()).unwrap() == 32_u8 - i);
i += 1_u8;
}
}
pub fn from_le_bytes(bytes: Bytes) -> Self
pub fn from_le_bytes(bytes: Bytes) -> Self
Converts a sequence of little-endian bytes to a u256
.
Arguments
bytes
: [Bytes] - The 32 bytes that compose theu256
.
Returns
- [u256] - The resulting
u256
value.
Examples
fn foo() {
let mut bytes = Bytes::with_capacity(32);
let mut i: u8 = 0;
while i < 32_u8 {
bytes.push(32_u8 - i);
i += 1_u8;
}
let x = u256::from_le_bytes(bytes);
assert(x == 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20_u256);
}
pub fn to_be_bytes(self) -> Bytes
pub fn to_be_bytes(self) -> Bytes
Converts the u256
to a sequence of big-endian bytes.
Returns
- [Bytes] - The 32 bytes that compose the
u256
.
Examples
fn foo() {
let x: u256 = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20_u256;
let bytes = x.to_be_bytes();
let mut i: u8 = 0;
while i < 32_u8 {
assert(bytes.get(i.as_u64()).unwrap() == i + 1_u8);
i += 1_u8;
}
}
pub fn from_be_bytes(bytes: Bytes) -> Self
pub fn from_be_bytes(bytes: Bytes) -> Self
Converts a sequence of big-endian bytes to a u256
.
Arguments
bytes
: [Bytes] - The 32 bytes that compose theu256
.
Returns
- [u256] - The resulting
u256
value.
Examples
fn foo() {
let mut bytes = Bytes::with_capacity(32);
let mut i: u8 = 0;
while i < 32_u8 {
bytes.push(i + 1);
i += 1_u8;
}
let x = u256::from_be_bytes(bytes);
assert(x == 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20_u256);
}
Trait Implementations
impl PartialEq for u256
impl PartialEq for u256
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] -
true
if 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 u256
impl BitwiseAnd for u256
impl BitwiseAnd for u256
pub fn binary_and(self, other: Self) -> Self
impl BitwiseXor for u256
impl BitwiseXor for u256
pub fn binary_xor(self, other: Self) -> Self
impl OrdEq for u256
impl OrdEq for u256
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] -
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);
}
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] -
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 AbiEncode for u256
impl AbiEncode for u256
pub fn abi_encode(self, buffer: Buffer) -> Buffer
impl AbiDecode for u256
impl AbiDecode for u256
pub fn abi_decode(refmut buffer: BufferReader) -> u256
impl TryFrom<B512> for u256
impl TryFrom<B512> for u256
pub fn try_from(val: B512) -> Option<Self>
pub fn try_from(val: B512) -> Option<Self>
Attempts conversion from a B512
to a u256
.
Additional Information
If the high bits of the B512
are not zero, the conversion will fail.
Arguments
val
: [B512] - TheB512
to be converted.
Returns
- [Option] - The
u256
representation of theB512
value.
Examples
use std::b512::B512;
fn foo() {
let b512_value = B512::new();
let u256_value = u256::try_from(b512_value).unwrap();
}
impl From<u8> for u256
impl From<u8> for u256
pub fn from(num: u8) -> Self
pub fn from(num: u8) -> Self
Casts a u8
to a u256
.
Arguments
num
: [u8] - Theu8
to be casted.
Returns
- [u256] - The
u256
representation of theu8
value.
Examples
fn foo() {
let u256_value = u256::from(255_u8);
}
impl From<u16> for u256
impl From<u16> for u256
pub fn from(num: u16) -> Self
pub fn from(num: u16) -> Self
Casts a u16
to a u256
.
Arguments
num
: [u16] - Theu16
to be casted.
Returns
- [u256] - The
u256
representation of theu16
value.
Examples
fn foo() {
let u256_value = u256::from(65535_u16);
}
impl From<u32> for u256
impl From<u32> for u256
pub fn from(num: u32) -> Self
pub fn from(num: u32) -> Self
Casts a u32
to a u256
.
Arguments
num
: [u32] - Theu32
to be casted.
Returns
- [u256] - The
u256
representation of theu32
value.
Examples
fn foo() {
let u256_value = u256::from(4294967295_u32);
}
impl From<u64> for u256
impl From<u64> for u256
pub fn from(num: u64) -> Self
pub fn from(num: u64) -> Self
Casts a u64
to a u256
.
Arguments
num
: [u64] - Theu64
to be casted.
Returns
- [u256] - The
u256
representation of theu64
value.
Examples
fn foo() {
let u256_value = u256::from(18446744073709551615_u64);
}
impl From<b256> for u256
impl From<b256> for u256
pub fn from(bits: b256) -> Self
pub fn from(bits: b256) -> Self
Casts raw b256
data to a u256
.
Arguments
bits
: [b256] - The rawb256
data to be casted.
Returns
- [u256] - The
u256
representation of the rawb256
.
Examples
fn foo() {
let u256_value = u256::zero();
}
impl From<(u64, u64, u64, u64)> for u256
impl From<(u64, u64, u64, u64)> for u256
pub fn from(nums: (u64, u64, u64, u64)) -> Self
pub fn from(nums: (u64, u64, u64, u64)) -> Self
Casts a tuple of 4 u64
values to a u256
.
Arguments
nums
: (u64, u64, u64, u64) - The tuple ofu64
values to be casted.
Returns
- [u256] - The
u256
representation of the tuple ofu64
values.
Examples
fn foo() {
let u256_value = u256::from((1, 2, 3, 4));
}
impl Power for u256
impl Power for u256
pub fn pow(self, exponent: u32) -> Self
pub fn pow(self, exponent: u32) -> Self
Raises self to the power of exponent
, using exponentiation by squaring.
Additional Information
- If panic on overflow is disabled, and the result overflows, the return value will be 0.
Reverts
- Reverts if the result overflows the type, if panic on overflow is enabled.
impl BinaryLogarithm for u256
impl BinaryLogarithm for u256
pub fn log2(self) -> Self
impl From<U128> for u256
impl From<U128> for u256
pub fn from(num: U128) -> Self
pub fn from(num: U128) -> Self
Converts a U128
to a u256
.
Arguments
num
: [U128] - TheU128
to be converted.
Returns
- [u256] - The
u256
representation of theU128
value.
Examples
use std::u128::U128;
fn foo() {
let u128_value = U128::from((18446744073709551615_u64, 18446744073709551615_u64));
let u256_value = u256::from(u128_value);
}