Primitive Type u64
Expand description
The 64-bit unsigned integer type.
Implementations§
Source§impl u64
impl u64
1.43.0 · Sourcepub const MAX: u64
pub const MAX: u64
The largest value that can be represented by this integer type (264 − 1).
§Examples
1.0.0 (const: 1.32.0) · Sourcepub const fn count_ones(self) -> u32
pub const fn count_ones(self) -> u32
Returns the number of ones in the binary representation of self.
§Examples
1.0.0 (const: 1.32.0) · Sourcepub const fn count_zeros(self) -> u32
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:
1.0.0 (const: 1.32.0) · Sourcepub const fn leading_zeros(self) -> u32
pub const fn leading_zeros(self) -> u32
1.0.0 (const: 1.32.0) · Sourcepub const fn trailing_zeros(self) -> u32
pub const fn trailing_zeros(self) -> u32
Returns the number of trailing zeros in the binary representation
of self.
§Examples
1.46.0 (const: 1.46.0) · Sourcepub const fn leading_ones(self) -> u32
pub const fn leading_ones(self) -> u32
Returns the number of leading ones in the binary representation of self.
§Examples
1.46.0 (const: 1.46.0) · Sourcepub const fn trailing_ones(self) -> u32
pub const fn trailing_ones(self) -> u32
Returns the number of trailing ones in the binary representation
of self.
§Examples
1.97.0 (const: 1.97.0) · Sourcepub const fn bit_width(self) -> u32
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
1.97.0 (const: 1.97.0) · Sourcepub const fn isolate_highest_one(self) -> u64
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
1.97.0 (const: 1.97.0) · Sourcepub const fn isolate_lowest_one(self) -> u64
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
1.97.0 (const: 1.97.0) · Sourcepub const fn highest_one(self) -> Option<u32>
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
1.97.0 (const: 1.97.0) · Sourcepub const fn lowest_one(self) -> Option<u32>
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
1.87.0 (const: 1.87.0) · Sourcepub const fn cast_signed(self) -> i64
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
Sourcepub const fn saturating_cast_signed(self) -> i64
🔬This is a nightly-only experimental API. (integer_cast_extras #154650)
pub const fn saturating_cast_signed(self) -> i64
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
Sourcepub const fn checked_cast_signed(self) -> Option<i64>
🔬This is a nightly-only experimental API. (integer_cast_extras #154650)
pub const fn checked_cast_signed(self) -> Option<i64>
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
Sourcepub const fn strict_cast_signed(self) -> i64
🔬This is a nightly-only experimental API. (integer_cast_extras #154650)
pub const fn strict_cast_signed(self) -> i64
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
1.0.0 (const: 1.32.0) · Sourcepub const fn rotate_left(self, n: u32) -> u64
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
1.0.0 (const: 1.32.0) · Sourcepub const fn rotate_right(self, n: u32) -> u64
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
Sourcepub const fn funnel_shl(self, right: u64, n: u32) -> u64
🔬This is a nightly-only experimental API. (funnel_shifts #145686)
pub const fn funnel_shl(self, right: u64, n: u32) -> u64
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.