Primitive std::u64

u64
Expand description
64-bit unsigned integer

Implementations

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

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

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

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

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

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>

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>

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>

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

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

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]

Converts the u64 to a sequence of little-endian bytes.

Returns

  • [[u8; 8]] - An array of 8 u8 bytes that compose the u64.

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

Converts a sequence of little-endian bytes to a u64.

Arguments

  • bytes: [[u8; 8]] - A sequence of 8 u8 bytes that represent a u64.

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]

Converts the u64 to a sequence of big-endian bytes.

Returns

  • [[u8; 8]] - An array of 8 u8 bytes that compose the u64.

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

Converts a sequence of big-endian bytes to a u64.

Arguments

  • bytes: [[u8; 8]] - A sequence of 8 u8 bytes that represent a u64.

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

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

Converts a sequence of little-endian bytes to a u64.

Arguments

  • bytes: [Bytes] - A Bytes object that represent a u64.

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

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

Converts a sequence of big-endian bytes to a u64.

Arguments

  • bytes: [Bytes] - A Bytes object that represent a u64.

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

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

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

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

pub fn divide(self, other: Self) -> Self

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

pub fn not(self) -> Self

pub fn eq(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, otherwise false.

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);
}

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

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

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

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

pub fn binary_xor(self, other: Self) -> Self

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 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);
}

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 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);
}

pub fn lsh(self, other: u64) -> Self

pub fn rsh(self, other: u64) -> Self

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

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

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

pub fn abi_decode(refmut buffer: BufferReader) -> u64

pub fn clone(self) -> Self

pub fn from(u: u8) -> Self

Casts a u8 to a u64.

Returns

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

Examples


fn foo() {
    let u64_value = u64::from(0u8);
}

pub fn from(u: u16) -> Self

Casts a u16 to a u64.

Returns

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

Examples


fn foo() {
    let u64_value = u64::from(0u16);
}

pub fn from(u: u32) -> Self

Casts a u32 to a u64.

Returns

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

Examples


fn foo() {
    let u64_value = u64::from(0u32);
}

pub fn try_from(u: u256) -> Option<Self>

pub fn sqrt(self) -> Self

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

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

pub fn log2(self) -> Self

pub fn try_from(u: U128) -> Option<Self>

pub fn hash(
self,
refmut state: Hasher,
)