::core::

Primitive int

Expand

A signed number

Also: #int Type Generic

Implementations

impl<#uint N> int<N>
pub fn as_uint(self) -> uint<N>

Casts a signed integer to an unsigned integer by reinterpreting its bits.

This does not preserve the original numeric value if the value is negative.

Examples

assert 13i8.as_uint() == 13u8;
assert (-5i8).as_uint() == 251u8;
pub fn as_bits(self) -> [bool; N]

Casts a signed integer to an array of bool by reinterpreting its bits.

Bits are laid out from LSB to MSB, so the first element of the array corresponds to the LSB of the input number and the last element corresponds to its MSB.

Note that bits in the literal binary representation of the number are written right-to-left, while items in the literal representation of the array are written left-to-right.

Examples

assert 0i0.as_bits() == [];
assert 0b0111i4.as_bits() == [true, true, true, false];
assert (-0b0001i4).as_bits() == [true, true, true, true];
pub fn to_bits(self) -> [bool; N]

Casts a signed integer to an array of bool by reinterpreting its bits.

Bits are laid out from LSB to MSB, so the first element of the array corresponds to the LSB of the input number and the last element corresponds to its MSB.

Note that bits in the literal binary representation of the number are written right-to-left, while items in the literal representation of the array are written left-to-right.

Examples

assert 0i0.to_bits() == [];
assert 0b0111i4.to_bits() == [true, true, true, false];
assert (-0b0001i4).to_bits() == [true, true, true, true];
impl<#uint N> int<N>
pub fn abs(self) -> uint<N>

Computes the absolute value of a signed integer.

Examples

assert 0i8.abs() == 0u8;
assert 15i8.abs() == 15u8;
assert (-7i8).abs() == 7u8;
pub fn concat<#uint M>(self, other: int<M>) -> int<{ N + M }>

Creates an integer by concatenating the bits of two existing ones.

The result is an integer whose higher bits correspond to the first input integer, and its lower bits correspond to the second one. In other words, given two numbers 0x123 and 0x456, the result is 0x123456.

Note that the result has the same sign as the first argument provided, as it is its most significant bit which becomes the sign bit of the concatenated whole.

Examples

assert 0x1234i16.concat(0x5678i16) == 0x1234_5678i32;
assert 0i16.concat(-1i16) == 65_535i32;
assert 0i0.concat(0i0) == 0i0;
pub fn max(self, other: Self) -> Self

Calculates the maximum value of two integers.

Examples

assert 2i8.max(10i8) == 10i8;
assert 15i8.max(3i8) == 15i8;
pub fn min(self, other: Self) -> Self

Calculates the minimum value of two integers.

Examples

assert 2i8.min(10i8) == 2i8;
assert 15i8.min(3i8) == 3i8;
pub fn trunc<#uint M>(self) -> int<M>

Reduces the size of an integer by truncating away the excess highest significant bits.

Note that removing those highest significant bits will change the numeric value of the integer if it cannot fit into the target bit width. In particular, negative numbers may change their sign and values whose numeric value does not fit in the result will wrap around.

Examples

assert 7i8.trunc() == 7i4;
assert 7i8.trunc() == -1i3;
assert 7i8.trunc() == -1i2;
assert (-8i8).trunc() == -8i4;
assert (-8i8).trunc() == 0i3;
impl<#uint N> int<N>
where
N > 0,
pub fn msb(self) -> bool

Returns the value of the most significant bit i.e. the sign bit.

Equivalent to int<N>::sign_bit.

Examples

assert 0i8.msb() == false; // 0b0000_0000
assert 15i8.msb() == false; // 0b0000_1111
assert (-7i8).msb() == true; // 0b1111_1001
pub fn lsb(self) -> bool

Returns the value of the least significant bit.

Examples

assert 0i8.lsb() == false; // 0b0000_0000
assert 1i8.lsb() == true; // 0b0000_0001
assert 16i8.lsb() == false; // 0b0001_0000
assert (-1i8).lsb() == true; // 0b1111_1111
assert (-8i8).lsb() == false; // 0b1111_1000
pub fn sign_bit(self) -> bool

Returns the value of the sign bit (also the most significant bit).

Equivalent to int<N>::msb.

Examples

assert 0i8.sign_bit() == false; // 0b0000_0000
assert 15i8.sign_bit() == false; // 0b0000_1111
assert (-7i8).sign_bit() == true; // 0b1111_1001

Trait Implementations

impl<#uint N> Number<N> for int<N>
impl<#uint N> PartialEq for int<N>
fn eq(self, rhs: Self) -> bool
fn ne(self, rhs: Self) -> bool
impl<#uint N> WrappingNeg for int<N>
fn wrapping_neg(self) -> Self
impl<#uint N> WrappingAdd for int<N>
fn wrapping_add(self, rhs: Self) -> Self
impl<#uint N> WrappingSub for int<N>
fn wrapping_sub(self, rhs: Self) -> Self
impl<#uint N> WrappingMul for int<N>
fn wrapping_mul(self, rhs: Self) -> Self
impl<#uint N> WrappingShl for int<N>
fn wrapping_shl(self, rhs: Self) -> Self
impl<#uint N> WrappingShr for int<N>
fn wrapping_shr(self, rhs: Self) -> Self
impl<#uint N> BitNot for int<N>
fn bit_not(self) -> Self
impl<#uint N> BitAnd for int<N>
fn bit_and(self, rhs: Self) -> Self
impl<#uint N> BitOr for int<N>
fn bit_or(self, rhs: Self) -> Self
impl<#uint N> BitXor for int<N>
fn bit_xor(self, rhs: Self) -> Self
impl<#uint N> PartialOrd for int<N>
fn lt(self, rhs: Self) -> bool
fn le(self, rhs: Self) -> bool
fn gt(self, rhs: Self) -> bool
fn ge(self, rhs: Self) -> bool