Skip to main content

i64

Primitive Type i64 

1.0.0
Expand description

The 64-bit signed integer type.

Implementations§

Source§

impl i64

1.43.0 · Source

pub const MIN: i64

The smallest value that can be represented by this integer type (−263).

§Examples
assert_eq!(i64::MIN, -9223372036854775808);
1.43.0 · Source

pub const MAX: i64

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

§Examples
assert_eq!(i64::MAX, 9223372036854775807);
1.53.0 · Source

pub const BITS: u32 = u64::BITS

The size of this integer type in bits.

§Examples
assert_eq!(i64::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 = 0b100_0000i64;

assert_eq!(n.count_ones(), 1);
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
assert_eq!(i64::MAX.count_zeros(), 1);
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 = -1i64;

assert_eq!(n.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 = -4i64;

assert_eq!(n.trailing_zeros(), 2);
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 = -1i64;

assert_eq!(n.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 = 3i64;

assert_eq!(n.trailing_ones(), 2);
1.97.0 (const: 1.97.0) · Source

pub const fn isolate_highest_one(self) -> i64

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

§Examples
let n: i64 = 0b_01100100;

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

pub const fn isolate_lowest_one(self) -> i64

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

§Examples
let n: i64 = 0b_01100100;

assert_eq!(n.isolate_lowest_one(), 0b_00000100);
assert_eq!(0_i64.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.

§Examples
assert_eq!(0b0_i64.highest_one(), None);
assert_eq!(0b1_i64.highest_one(), Some(0));
assert_eq!(0b1_0000_i64.highest_one(), Some(4));
assert_eq!(0b1_1111_i64.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_i64.lowest_one(), None);
assert_eq!(0b1_i64.lowest_one(), Some(0));
assert_eq!(0b1_0000_i64.lowest_one(), Some(4));
assert_eq!(0b1_1111_i64.lowest_one(), Some(0));
1.87.0 (const: 1.87.0) · Source

pub const fn cast_unsigned(self) -> u64

Returns the bit pattern of self reinterpreted as an unsigned 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 = -1i64;

assert_eq!(n.cast_unsigned(), u64::MAX);
Source

pub const fn saturating_cast_unsigned(self) -> u64

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

Saturating conversion of self to an unsigned integer of the same size.

Negative values are clamped to 0.

For other kinds of unsigned integer casts, see cast_unsigned, checked_cast_unsigned, or strict_cast_unsigned.

§Examples
#![feature(integer_cast_extras)]
let n = i64::MIN;

assert_eq!(n.saturating_cast_unsigned(), 0u64);
assert_eq!(64i64.saturating_cast_unsigned(), 64u64);
Source

pub const fn checked_cast_unsigned(self) -> Option<u64>

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

Checked conversion of self to an unsigned integer of the same size, returning None if self is negative.

For other kinds of unsigned integer casts, see cast_unsigned, saturating_cast_unsigned, or strict_cast_unsigned.

§Examples
#![feature(integer_cast_extras)]
let n = i64::MIN;

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

pub const fn strict_cast_unsigned(self) -> u64

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

Strict conversion of self to an unsigned integer of the same size, which panics if self is negative.

For other kinds of unsigned integer casts, see cast_unsigned, checked_cast_unsigned, or saturating_cast_unsigned.

§Examples
ⓘ
#![feature(integer_cast_extras)]
let _ = i64::MIN.strict_cast_unsigned();
1.0.0 (const: 1.32.0) · Source

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

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 = 0xaa00000000006e1i64;
let m = 0x6e10aa;

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) -> i64

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 = 0x6e10aai64;
let m = 0xaa00000000006e1;

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

pub const fn swap_bytes(self) -> i64

Reverses the byte order of the integer.

§Examples
let n = 0x1234567890123456i64;

let m = n.swap_bytes();

assert_eq!(m, 0x5634129078563412);
1.37.0 (const: 1.37.0) · Source

pub const fn reverse_bits(self) -> i64

Reverses the order of bits in the integer. The least significant bit becomes the most significant bit, second least-significant bit becomes second most-significant bit, etc.

§Examples
let n = 0x1234567890123456i64;
let m = n.reverse_bits();

assert_eq!(m, 0x6a2c48091e6a2c48);
assert_eq!(0, 0i64.reverse_bits());
1.0.0 (const: 1.32.0) · Source

pub const fn from_be(x: i64) -> i64

Converts an integer from big endian to the target’s endianness.

On big endian this is a no-op. On little endian the bytes are swapped.

See also from_be_bytes().

§Examples
let n = 0x1Ai64;

if cfg!(target_endian = "big") {
    assert_eq!(i64::from_be(n), n)
} else {
    assert_eq!(i64::from_be(n), n.swap_bytes())
}
1.0.0 (const: 1.32.0) · Source

pub const fn from_le(x: i64) -> i64

Converts an integer from little endian to the target’s endianness.

On little endian this is a no-op. On big endian the bytes are swapped.

See also from_le_bytes().

§Examples
let n = 0x1Ai64;

if cfg!(target_endian = "little") {
    assert_eq!(i64::from_le(n), n)
} else {
    assert_eq!(i64::from_le(n), n.swap_bytes())
}
1.0.0 (const: 1.32.0) · Source

pub const fn to_be(self) -> i64

Swaps bytes of self on little endian targets.

On big endian this is a no-op.

The returned value has the same type as self, and will be interpreted as (a potentially different) value of a native-endian i64.

See to_be_bytes() for a type-safe alternative.

§Examples
let n = 0x1Ai64;

if cfg!(target_endian = "big") {
    assert_eq!(n.to_be(), n)
} else {
    assert_eq!(n.to_be(), n.swap_bytes())
}
1.0.0 (const: 1.32.0) · Source

pub const fn to_le(self) -> i64

Swaps bytes of self on big endian targets.

On little endian this is a no-op.

The returned value has the same type as self, and will be interpreted as (a potentially different) value of a native-endian i64.

See to_le_bytes() for a type-safe alternative.

§Examples
let n = 0x1Ai64;

if cfg!(target_endian = "little") {
    assert_eq!(n.to_le(), n)
} else {
    assert_eq!(n.to_le(), n.swap_bytes())
}
1.0.0 (const: 1.47.0) · Source

pub const fn checked_add(self, rhs: i64) -> Option<i64>

Checked integer addition. Computes self + rhs, returning None if overflow occurred.

§Examples
assert_eq!((i64::MAX - 2).checked_add(1), Some(i64::MAX - 1));
assert_eq!((i64::MAX - 2).checked_add(3), None);
1.91.0 (const: 1.91.0) · Source

pub const fn strict_add(self, rhs: i64) -> i64

Strict integer addition. Computes self + rhs, panicking if overflow occurred.

§Panics
§Overflow behavior

This function will always panic on overflow, regardless of whether overflow checks are enabled.

§Examples
assert_eq!((i64::MAX - 2).strict_add(1), i64::MAX - 1);

The following panics because of overflow:

ⓘ
let _ = (i64::MAX - 2).strict_add(3);
1.79.0 (const: 1.79.0) · Source

pub const unsafe fn unchecked_add(self, rhs: i64) -> i64

Unchecked integer addition. Computes self + rhs, assuming overflow cannot occur.

Calling x.unchecked_add(y) is semantically equivalent to calling x.checked_add(y).unwrap_unchecked().

If you’re just trying to avoid the panic in debug mode, then do not use this. Instead, you’re looking for wrapping_add.

§Safety

This results in undefined behavior when self + rhs > i64::MAX or self + rhs < i64::MIN, i.e. when checked_add would return None.

1.66.0 (const: 1.66.0) · Source

pub const fn checked_add_unsigned(self, rhs: u64) -> Option<i64>

Checked addition with an unsigned integer. Computes self + rhs, returning None if overflow occurred.

§Examples
assert_eq!(1i64.checked_add_unsigned(2), Some(3));
assert_eq!((i64::MAX - 2).checked_add_unsigned(3), None);
1.91.0 (const: 1.91.0) · Source

pub const fn strict_add_unsigned(self, rhs: u64) -> i64

Strict addition with an unsigned integer. Computes self + rhs, panicking if overflow occurred.

§Panics
§Overflow behavior

This function will always panic on overflow, regardless of whether overflow checks are enabled.

§Examples
assert_eq!(1i64.strict