::core::

Primitive uint

Expand

An unsigned number

Also: #uint Type Generic

Implementations

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

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

Note that the numeric value is not preserved. A value that is greater than 2^N will result in a negative value. To preserve the numeric representation, use to_int.

Examples

assert 42u8.as_int() == 42i8;
assert 255u8.as_int() == -1i8;
pub fn as_bits(self) -> [bool; N]

Casts an unsigned 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 0u0.as_bits() == [];
assert 0b1101u4.as_bits() == [true, false, true, true];
pub fn to_int(self) -> int<{ N + 1 }>

Convert an unsigned integer to a signed integer.

The output has one additional bits in order to ensure that the original value fits. If you want to re-interpret the bits instead, use as_int.

Examples

assert 42u8.to_int() == 42i9;
assert 255u8.to_int() == 255i9;
pub fn to_bits(self) -> [bool; N]

Casts an unsigned 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 0u0.to_bits() == [];
assert 0b1101u4.to_bits() == [true, false, true, true];
impl<#uint N> uint<N>
where
N == N / 8 * 8 else "Integer cannot be divided into bytes",
fn to_le_bytes(self) -> [uint<8>; { N / 8 }]

Converts self into an array of bytes in little-endian order.

Examples

let bytes = 0x12345678u32.to_le_bytes();
assert bytes == [0x78, 0x56, 0x34, 0x12];
fn to_be_bytes(self) -> [uint<8>; { N / 8 }]

Converts self into an array of bytes in big-endian order.

Examples

let bytes = 0x12345678u32.to_be_bytes();
assert bytes == [0x12, 0x34, 0x56, 0x78];
fn to_german_endian_bytes(self) -> [uint<8>; { N / 8 }]

Converts self into an array of bytes in the order that germans say digits.

Examples

assert 0x12u8.to_german_endian_bytes() == [0x12];
assert 0x1234u16.to_german_endian_bytes() == [0x34, 0x12];
assert 0x12345678u32.to_german_endian_bytes() == [0x12, 0x34, 0x78, 0x56];
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> 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

Trait Implementations

impl<#uint N> Number<N> for uint<N>
impl<#uint N> PartialEq for uint<N>
fn eq(self, rhs: Self) -> bool
fn ne(self, rhs: Self) -> bool
impl<#uint N> WrappingAdd for uint<N>
fn wrapping_add(self, rhs: Self) -> Self
impl<#uint N> WrappingSub for uint<N>
fn wrapping_sub(self, rhs: Self) -> Self
impl<#uint N> WrappingMul for uint<N>
fn wrapping_mul(self, rhs: Self) -> Self
impl<#uint N> WrappingShl for uint<N>
fn wrapping_shl(self, rhs: Self) -> Self
impl<#uint N> WrappingShr for uint<N>
fn wrapping_shr(self, rhs: Self) -> Self
impl<#uint N> BitNot for uint<N>
fn bit_not(self) -> Self
impl<#uint N> BitAnd for uint<N>
fn bit_and(self, rhs: Self) -> Self
impl<#uint N> BitOr for uint<N>
fn bit_or(self, rhs: Self) -> Self
impl<#uint N> BitXor for uint<N>
fn bit_xor(self, rhs: Self) -> Self
impl<#uint N> PartialOrd for uint<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