Skip to main content

u64

Primitive Type u64 

1.0.0
Expand description

The 64-bit unsigned integer type.

Implementations§

Source§

impl u64

1.43.0 · Source

pub const MIN: u64 = 0

The smallest value that can be represented by this integer type.

§Examples
assert_eq!(u64::MIN, 0);
1.43.0 · Source

pub const MAX: u64

The largest value that can be represented by this integer type (264 − 1).

§Examples
assert_eq!(u64::MAX, 18446744073709551615);
1.53.0 · Source

pub const BITS: u32

The size of this integer type in bits.

§Examples
assert_eq!(u64::BITS, 64);
1.0.0 (const: 1.32.0) · Source

pub const fn count_ones(self) -> u32

Returns the number of ones in the binary representation of self.

§Examples
let n = 0b01001100u64;
assert_eq!(n.count_ones(), 3);

let max = u64::MAX;
assert_eq!(max.count_ones(), 64);

let zero = 0u64;
assert_eq!(zero.count_ones(), 0);
1.0.0 (const: 1.32.0) · Source

pub const fn count_zeros(self) -> u32

Returns the number of zeros in the binary representation of self.

§Examples
let zero = 0u64;
assert_eq!(zero.count_zeros(), 64);

let max = u64::MAX;
assert_eq!(max.count_zeros(), 0);

This is heavily dependent on the width of the type, and thus might give surprising results depending on type inference:

let lucky = 7;
foo(lucky);
assert_eq!(lucky.count_zeros(), 5);
assert_eq!(lucky.count_ones(), 3);

let lucky = 7;
bar(lucky);
assert_eq!(lucky.count_zeros(), 13);
assert_eq!(lucky.count_ones(), 3);

You might want to use Self::count_ones instead, or emphasize the type you’re using in the call rather than method syntax:

let small = 1;
assert_eq!(u64::count_zeros(small), 63);
1.0.0 (const: 1.32.0) · Source

pub const fn leading_zeros(self) -> u32

Returns the number of leading zeros in the binary representation of self.

Depending on what you’re doing with the value, you might also be interested in the ilog2 function which returns a consistent number, even if the type widens.

§Examples
let n = u64::MAX >> 2;
assert_eq!(n.leading_zeros(), 2);

let zero = 0u64;
assert_eq!(zero.leading_zeros(), 64);

let max = u64::MAX;
assert_eq!(max.leading_zeros(), 0);
1.0.0 (const: 1.32.0) · Source

pub const fn trailing_zeros(self) -> u32

Returns the number of trailing zeros in the binary representation of self.

§Examples
let n = 0b0101000u64;
assert_eq!(n.trailing_zeros(), 3);

let zero = 0u64;
assert_eq!(zero.trailing_zeros(), 64);

let max = u64::MAX;
assert_eq!(max.trailing_zeros(), 0);
1.46.0 (const: 1.46.0) · Source

pub const fn leading_ones(self) -> u32

Returns the number of leading ones in the binary representation of self.

§Examples
let n = !(u64::MAX >> 2);
assert_eq!(n.leading_ones(), 2);

let zero = 0u64;
assert_eq!(zero.leading_ones(), 0);

let max = u64::MAX;
assert_eq!(max.leading_ones(), 64);
1.46.0 (const: 1.46.0) · Source

pub const fn trailing_ones(self) -> u32

Returns the number of trailing ones in the binary representation of self.

§Examples
let n = 0b1010111u64;
assert_eq!(n.trailing_ones(), 3);

let zero = 0u64;
assert_eq!(zero.trailing_ones(), 0);

let max = u64::MAX;
assert_eq!(max.trailing_ones(), 64);
1.97.0 (const: 1.97.0) · Source

pub const fn bit_width(self) -> u32

Returns the minimum number of bits required to represent self.

This method returns zero if self is zero.

§Examples
assert_eq!(0_u64.bit_width(), 0);
assert_eq!(0b111_u64.bit_width(), 3);
assert_eq!(0b1110_u64.bit_width(), 4);
assert_eq!(u64::MAX.bit_width(), 64);
1.97.0 (const: 1.97.0) · Source

pub const fn isolate_highest_one(self) -> u64

Returns self with only the most significant bit set, or 0 if the input is 0.

§Examples
let n: u64 = 0b_01100100;

assert_eq!(n.isolate_highest_one(), 0b_01000000);
assert_eq!(0_u64.isolate_highest_one(), 0);
1.97.0 (const: 1.97.0) · Source

pub const fn isolate_lowest_one(self) -> u64

Returns self with only the least significant bit set, or 0 if the input is 0.

§Examples
let n: u64 = 0b_01100100;

assert_eq!(n.isolate_lowest_one(), 0b_00000100);
assert_eq!(0_u64.isolate_lowest_one(), 0);
1.97.0 (const: 1.97.0) · Source

pub const fn highest_one(self) -> Option<u32>

Returns the index of the highest bit set to one in self, or None if self is 0.

Note that this is equivalent to checked_ilog2.

§Examples
assert_eq!(0b0_u64.highest_one(), None);
assert_eq!(0b1_u64.highest_one(), Some(0));
assert_eq!(0b1_0000_u64.highest_one(), Some(4));
assert_eq!(0b1_1111_u64.highest_one(), Some(4));
1.97.0 (const: 1.97.0) · Source

pub const fn lowest_one(self) -> Option<u32>

Returns the index of the lowest bit set to one in self, or None if self is 0.

§Examples
assert_eq!(0b0_u64.lowest_one(), None);
assert_eq!(0b1_u64.lowest_one(), Some(0));
assert_eq!(0b1_0000_u64.lowest_one(), Some(4));
assert_eq!(0b1_1111_u64.lowest_one(), Some(0));
1.87.0 (const: 1.87.0) · Source

pub const fn cast_signed(self) -> i64

Returns the bit pattern of self reinterpreted as a signed integer of the same size.

This produces the same result as an as cast, but ensures that the bit-width remains the same.

§Examples
let n = u64::MAX;

assert_eq!(n.cast_signed(), -1i64);
Source

pub const fn saturating_cast_signed(self) -> i64

🔬This is a nightly-only experimental API. (integer_cast_extras #154650)

Saturating conversion of self to a signed integer of the same size.

The signed integer’s maximum value is returned if self is larger than the maximum positive value representable by the signed integer.

For other kinds of signed integer casts, see cast_signed, checked_cast_signed, or strict_cast_signed.

§Examples
#![feature(integer_cast_extras)]
let n = u64::MAX;

assert_eq!(n.saturating_cast_signed(), i64::MAX);
assert_eq!(64u64.saturating_cast_signed(), 64i64);
Source

pub const fn checked_cast_signed(self) -> Option<i64>

🔬This is a nightly-only experimental API. (integer_cast_extras #154650)

Checked conversion of self to a signed integer of the same size, returning None if self is larger than the signed integer’s maximum value.

For other kinds of signed integer casts, see cast_signed, saturating_cast_signed, or strict_cast_signed.

§Examples
#![feature(integer_cast_extras)]
let n = u64::MAX;

assert_eq!(n.checked_cast_signed(), None);
assert_eq!(64u64.checked_cast_signed(), Some(64i64));
Source

pub const fn strict_cast_signed(self) -> i64

🔬This is a nightly-only experimental API. (integer_cast_extras #154650)

Strict conversion of self to a signed integer of the same size, which panics if self is larger than the signed integer’s maximum value.

For other kinds of signed integer casts, see cast_signed, checked_cast_signed, or saturating_cast_signed.

§Examples
#![feature(integer_cast_extras)]
let _ = u64::MAX.strict_cast_signed();
1.0.0 (const: 1.32.0) · Source

pub const fn rotate_left(self, n: u32) -> u64

Shifts the bits to the left by a specified amount, n, wrapping the truncated bits to the end of the resulting integer.

rotate_left(n) is equivalent to applying rotate_left(1) a total of n times. In particular, a rotation by the number of bits in self returns the input value unchanged.

Please note this isn’t the same operation as the << shifting operator!

§Examples
let n = 0x0aa00000000006e1u64;
let m = 0x00000000006e10aa;

assert_eq!(n.rotate_left(12), m);
assert_eq!(n.rotate_left(1024), n);
1.0.0 (const: 1.32.0) · Source

pub const fn rotate_right(self, n: u32) -> u64

Shifts the bits to the right by a specified amount, n, wrapping the truncated bits to the beginning of the resulting integer.

rotate_right(n) is equivalent to applying rotate_right(1) a total of n times. In particular, a rotation by the number of bits in self returns the input value unchanged.

Please note this isn’t the same operation as the >> shifting operator!

§Examples
let n = 0x00000000006e10aau64;
let m = 0x0aa00000000006e1;

assert_eq!(n.rotate_right(12), m);
assert_eq!(n.rotate_right(1024), n);
Source

pub const fn funnel_shl(self, right: u64, n: u32) -> u64

🔬This is a nightly-only experimental API. (funnel_shifts #145686)

Performs a left funnel shift.

This operation can be thought of as concatenating self and right into an integer twice the size of u64, performing a left shift by n, and returning the left half of the result.

The name comes from “funneling” a wider integer to a narrower integer.

§Panics