Primitive std::u64

u64
Expand description
64-bit unsigned integer

Implementations

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

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

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

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

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

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

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

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

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

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

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

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

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

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

fn sqrt(self) -> Self

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

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

fn log2(self) -> Self

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

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

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

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

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

fn hash(
self,
refmut state: Hasher,
)