u64
Expand description
Implementations
pub fn min() -> Self
pub fn min() -> Self
The smallest value that can be represented by this integer type.
Returns
- [u64] - The smallest
u64
value.
Examples
fn foo() {
let val = u64::min();
assert(val == 0);
pub fn max() -> Self
pub fn max() -> Self
The largest value that can be represented by this integer type,
264 - 1.
Returns
- [u64] - The largest
u64
value.
Examples
fn foo() {
let val = u64::max();
assert(val == 18446744073709551615);
}
pub fn bits() -> u64
pub fn bits() -> u64
The size of this integer type in bits.
Returns
- [u32] - The number of bits for a
u64
.
Examples
fn foo() {
let bits = u64::bits();
assert(bits == 64);
}
pub fn zero() -> Self
pub fn zero() -> Self
Returns the zero value for the u64
type.
Returns
- [u64] -> The zero value for the
u64
type.
Examples
fn foo() {
let zero_u64 = u64::zero();
assert(zero_u64 == 0u64);
}
pub fn is_zero(self) -> bool
pub fn is_zero(self) -> bool
Returns whether a u64
is set to zero.
Returns
- [bool] -> True if the
u64
is zero, otherwise false.
Examples
fn foo() {
let zero_u64 = u64::zero();
assert(zero_u64.is_zero());
}
pub fn as_u256(self) -> u256
pub fn as_u256(self) -> u256
Extends a u64
to a u256
.
Returns
- [u256] - The converted
u64
value.
Examples
fn foo() {
let val = 2;
let result = val.as_u256();
assert(result == 0x0000000000000000000000000000000000000000000000000000000000000002u256);
}
pub fn try_as_u8(self) -> Option<u8>
pub fn try_as_u8(self) -> Option<u8>
Attempts to convert the u64 value into a u8 value.
Additional Information
The max value a u8 can represent is 255.
Returns
[Option] - Some(u8)
if the u64 is less than or equal to the max u8 value. Else None
.
Examples
fn foo() {
let val = 255_u64.try_as_u8();
assert(val == Some(255_u8));
// Conversion fails as value is above the max a u8 can represent.
let val2 = 256_u64.try_as_u8();
assert(val == None);
}
pub fn try_as_u16(self) -> Option<u16>
pub fn try_as_u16(self) -> Option<u16>
Attempts to convert the u64 value into a u16 value.
Additional Information
The max value a u16 can represent is 65_535.
Returns
[Option] - Some(u16)
if the u64 is less than or equal to the max u16 value. Else None
.
Examples
fn foo() {
let val = 65_535_u64.try_as_u16();
assert(val == Some(65_535_u16));
// Conversion fails as value is above the max a u16 can represent.
let val2 = 65_536_u64.try_as_u16();
assert(val == None);
}
pub fn try_as_u32(self) -> Option<u32>
pub fn try_as_u32(self) -> Option<u32>
Attempts to convert the u64 value into a u32 value.
Additional Information
The max value a u32 can represent is 4_294_967_295.
Returns
[Option] - Some(u32)
if the u64 is less than or equal to the max u32 value. Else None
.
Examples
fn foo() {
let val = 4_294_967_295_u64.try_as_u32();
assert(val == Some(4_294_967_295_u32));
// Conversion fails as value is above the max a u32 can represent.
let val2 = 4_294_967_296_u64.try_as_u32();
assert(val == None);
}
pub fn overflowing_add(self, right: Self) -> U128
pub fn overflowing_add(self, right: Self) -> U128
Performs addition between two u64
values, returning a U128
.
Additional Information
Allows for addition between two u64
values that would otherwise overflow.
Arguments
right
: [u64] - The right-hand side of the addition.
Returns
- [U128] - The result of the addition.
Examples
use std::u128::U128;
fn foo() {
let x = u64::max();
let y = u64::max();
let z = x.overflowing_add(y);
assert(z == U128::from(1, 18446744073709551614));
}
pub fn overflowing_mul(self, right: Self) -> U128
pub fn overflowing_mul(self, right: Self) -> U128
Performs multiplication between two u64
values, returning a U128
.
Additional Information
Allows for multiplication between two u64
values that would otherwise overflow.
Arguments
right
: [u64] - The right-hand side of the multiplication.
Returns
- [U128] - The result of the multiplication.
Examples
use std::u128::U128;
fn foo() {
let x = u64::max();
let y = u64::max();
let z = x.overflowing_mul(y);
assert(z == U128::from(18446744073709551615, 1));
}
pub fn to_le_bytes(self) -> [u8; 8]
pub fn to_le_bytes(self) -> [u8; 8]
Converts the u64
to a sequence of little-endian bytes.
Returns
- [[u8; 8]] - An array of 8
u8
bytes that compose theu64
.
Examples
fn foo() {
let x: u64 = 578437695752307201;
let result = x.to_le_bytes();
assert(result[0] == 1_u8);
assert(result[1] == 2_u8);
assert(result[2] == 3_u8);
assert(result[3] == 4_u8);
assert(result[4] == 5_u8);
assert(result[5] == 6_u8);
assert(result[6] == 7_u8);
assert(result[7] == 8_u8);
}
pub fn from_le_bytes(bytes: [u8; 8]) -> Self
pub fn from_le_bytes(bytes: [u8; 8]) -> Self
Converts a sequence of little-endian bytes to a u64
.
Arguments
bytes
: [[u8; 8]] - A sequence of 8u8
bytes that represent au64
.
Returns
- [u64] - The resulting
u64
value.
Examples
fn foo() {
let bytes = [1_u8, 2_u8, 3_u8, 4_u8, 5_u8, 6_u8, 7_u8, 8_u8];
let result = u64::from_le_bytes(bytes);
assert(result == 578437695752307201);
}
pub fn to_be_bytes(self) -> [u8; 8]
pub fn to_be_bytes(self) -> [u8; 8]
Converts the u64
to a sequence of big-endian bytes.
Returns
- [[u8; 8]] - An array of 8
u8
bytes that compose theu64
.
Examples
fn foo() {
let x: u64 = 578437695752307201;
let result = x.to_be_bytes();
assert(result[0] == 8_u8);
assert(result[1] == 7_u8);
assert(result[2] == 6_u8);
assert(result[3] == 5_u8);
assert(result[4] == 4_u8);
assert(result[5] == 3_u8);
assert(result[6] == 2_u8);
assert(result[7] == 1_u8);
}
pub fn from_be_bytes(bytes: [u8; 8]) -> Self
pub fn from_be_bytes(bytes: [u8; 8]) -> Self
Converts a sequence of big-endian bytes to a u64
.
Arguments
bytes
: [[u8; 8]] - A sequence of 8u8
bytes that represent au64
.
Returns
- [u64] - The resulting
u64
value.
Examples
fn foo() {
let bytes = [8_u8, 7_u8, 6_u8, 5_u8, 4_u8, 3_u8, 2_u8, 1_u8];
let result = u64::from_be_bytes(bytes);
assert(result == 578437695752307201);
}
pub fn to_le_bytes(self) -> Bytes
pub fn to_le_bytes(self) -> Bytes
Converts the u64
to a sequence of little-endian bytes.
Returns
- [Bytes] - The bytes that compose the
u64
.
Examples
use std::bytes_conversions::u64::*;
fn foo() {
let x: u64 = 578437695752307201;
let result = x.to_le_bytes();
assert(result.get(0).unwrap() == 1_u8);
assert(result.get(1).unwrap() == 2_u8);
assert(result.get(2).unwrap() == 3_u8);
assert(result.get(3).unwrap() == 4_u8);
assert(result.get(4).unwrap() == 5_u8);
assert(result.get(5).unwrap() == 6_u8);
assert(result.get(6).unwrap() == 7_u8);
assert(result.get(7).unwrap() == 8_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 u64
.
Arguments
bytes
: [Bytes] - ABytes
object that represent au64
.
Returns
- [u64] - The resulting
u64
value.
Examples
use std::{bytes::Bytes, bytes_conversions::u64::*};
fn foo() {
let mut bytes = Bytes::new();
bytes.push(1_u8);
bytes.push(2_u8);
bytes.push(3_u8);
bytes.push(4_u8);
bytes.push(5_u8);
bytes.push(6_u8);
bytes.push(7_u8);
bytes.push(8_u8);
let result = u64::from_le_bytes(bytes);
assert(result == 578437695752307201);
}
pub fn to_be_bytes(self) -> Bytes
pub fn to_be_bytes(self) -> Bytes
Converts the u64
to a sequence of big-endian bytes.
Returns
- [Bytes] - The bytes that compose the
u64
.
Examples
use std::bytes_conversions::u64::*;
fn foo() {
let x: u64 = 578437695752307201;
let result = x.to_be_bytes();
assert(result.get(0).unwrap() == 8_u8);
assert(result.get(1).unwrap() == 7_u8);
assert(result.get(2).unwrap() == 6_u8);
assert(result.get(3).unwrap() == 5_u8);
assert(result.get(4).unwrap() == 4_u8);
assert(result.get(5).unwrap() == 3_u8);
assert(result.get(6).unwrap() == 2_u8);
assert(result.get(7).unwrap() == 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 u64
.
Arguments
bytes
: [Bytes] - ABytes
object that represent au64
.
Returns
- [u64] - The resulting
u64
value.
Examples
use std::{bytes::Bytes, bytes_conversions::u64::*};
fn foo() {
let mut bytes = Bytes::new();
bytes.push(8_u8);
bytes.push(7_u8);
bytes.push(6_u8);
bytes.push(5_u8);
bytes.push(4_u8);
bytes.push(3_u8);
bytes.push(2_u8);
bytes.push(1_u8);
let result = u64::from_be_bytes(bytes);
assert(result == 578437695752307201);
}
Trait Implementations
impl PartialEq for u64
impl PartialEq for u64
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 u64
impl BitwiseAnd for u64
impl BitwiseAnd for u64
pub fn binary_and(self, other: Self) -> Self
impl BitwiseXor for u64
impl BitwiseXor for u64
pub fn binary_xor(self, other: Self) -> Self
impl OrdEq for u64
impl OrdEq for u64
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 u64
impl AbiEncode for u64
pub fn abi_encode(self, buffer: Buffer) -> Buffer
impl AbiDecode for u64
impl AbiDecode for u64
pub fn abi_decode(refmut buffer: BufferReader) -> u64
impl From<u8> for u64
impl From<u8> for u64
pub fn from(u: u8) -> Self
pub fn from(u: u8) -> Self
Casts a u8
to a u64
.
Returns
- [u64] - The
u64
representation of theu8
value.
Examples
fn foo() {
let u64_value = u64::from(0u8);
}
impl From<u16> for u64
impl From<u16> for u64
pub fn from(u: u16) -> Self
pub fn from(u: u16) -> Self
Casts a u16
to a u64
.
Returns
- [u64] - The
u64
representation of theu16
value.
Examples
fn foo() {
let u64_value = u64::from(0u16);
}
impl From<u32> for u64
impl From<u32> for u64
pub fn from(u: u32) -> Self
pub fn from(u: u32) -> Self
Casts a u32
to a u64
.
Returns
- [u64] - The
u64
representation of theu32
value.
Examples
fn foo() {
let u64_value = u64::from(0u32);
}