::std::

Module num

Structs

Gray

A number encoded into Gray code.

Functions

abs

Computes the absolute value of a signed integer.

concat

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

gray_to_bin_impl

Returns the binary value of the given gray code value

sext

Increases the size of a signed integer by replicating its sign bit.

trunc

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

zext

Increases the size of an unsigned integer by extending it with zeros on the most significant side.

Implementations

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> uint<N>
pub fn concat<#uint M>(self, other: uint<M>) -> uint<{ 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.

Examples

assert 0xABADu16.concat(0x1DEAu16) == 0xABAD_1DEAu32;
assert 0u0.concat(0u0) == 0u0;
pub fn interleave(self, other: Self) -> uint<{ 2 * N }>

Interleaves the bits of two numbers.

A number containing bits from both input numbers is built, with even positions occupied by bits from the first number and odd positions occupied by those from the second number. In other words, given two numbers 0b011 and 0b101, the output would be 0b100111.

Examples

assert 0b011u3.interleave(0b101u3) == 0b100111u6;
pub fn max(self, other: Self) -> Self

Calculates the maximum value of two integers.

Examples

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

Calculates the minimum value of two integers.

Examples

assert 2u8.min(10u8) == 2u8;
assert 15u8.min(3u8) == 3u8;
pub fn trunc<#uint M>(self) -> uint<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.

Examples

assert 7u8.trunc() == 7u4;
assert 7u8.trunc() == 7u3;
assert 7u8.trunc() == 3u2;
pub fn to_gray(self) -> Gray<N>

Encodes the input number into Gray code.

Examples

assert 0u2.to_gray() == std::num::Gray(0b00);
assert 1u2.to_gray() == std::num::Gray(0b01);
assert 2u2.to_gray() == std::num::Gray(0b11);
assert 3u2.to_gray() == std::num::Gray(0b10);
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
impl<#uint N> uint<N>
where
    N > 0,
pub fn msb(self) -> bool

Returns the value of the most significant bit.

Examples

assert 0u8.msb() == false; // 0b0000_0000
assert 15u8.msb() == false; // 0b0000_1111
assert 192u8.msb() == true; // 0b1100_0000
pub fn lsb(self) -> bool

Returns the value of the least significant bit.

Examples

assert 0u8.lsb() == false; // 0b0000_0000
assert 1u8.lsb() == true; // 0b0000_0001
assert 16u8.lsb() == false; // 0b0001_0000
impl<#uint N> Default for int<N>
fn hacky_default(self) -> Self
impl<#uint N> Default for uint<N>
fn hacky_default(self) -> Self