Trait std::num::Int[src]

pub trait Int: Primitive + CheckedAdd + CheckedSub + CheckedMul + CheckedDiv + Bounded + Not<Self> + BitAnd<Self, Self> + BitOr<Self, Self> + BitXor<Self, Self> + Shl<Self, Self> + Shr<Self, Self> {
    fn count_ones(self) -> Self;
    fn leading_zeros(self) -> Self;
    fn trailing_zeros(self) -> Self;
    fn rotate_left(self, n: uint) -> Self;
    fn rotate_right(self, n: uint) -> Self;
    fn swap_bytes(self) -> Self;

    fn count_zeros(self) -> Self { ... }
    fn from_be(x: Self) -> Self { ... }
    fn from_le(x: Self) -> Self { ... }
    fn to_be(self) -> Self { ... }
    fn to_le(self) -> Self { ... }
}

A primitive signed or unsigned integer equipped with various bitwise operators, bit counting methods, and endian conversion functions.

Required Methods

fn count_ones(self) -> Self

Returns the number of ones in the binary representation of the integer.

Example

fn main() { let n = 0b01001100u8; assert_eq!(n.count_ones(), 3); }
let n = 0b01001100u8;

assert_eq!(n.count_ones(), 3);

fn leading_zeros(self) -> Self

Returns the number of leading zeros in the in the binary representation of the integer.

Example

fn main() { let n = 0b0101000u16; assert_eq!(n.leading_zeros(), 10); }
let n = 0b0101000u16;

assert_eq!(n.leading_zeros(), 10);

fn trailing_zeros(self) -> Self

Returns the number of trailing zeros in the in the binary representation of the integer.

Example

fn main() { let n = 0b0101000u16; assert_eq!(n.trailing_zeros(), 3); }
let n = 0b0101000u16;

assert_eq!(n.trailing_zeros(), 3);

fn rotate_left(self, n: uint) -> Self

Shifts the bits to the left by a specified amount amount, n, wrapping the truncated bits to the end of the resulting integer.

Example

fn main() { let n = 0x0123456789ABCDEFu64; let m = 0x3456789ABCDEF012u64; assert_eq!(n.rotate_left(12), m); }
let n = 0x0123456789ABCDEFu64;
let m = 0x3456789ABCDEF012u64;

assert_eq!(n.rotate_left(12), m);

fn rotate_right(self, n: uint) -> Self

Shifts the bits to the right by a specified amount amount, n, wrapping the truncated bits to the beginning of the resulting integer.

Example

fn main() { let n = 0x0123456789ABCDEFu64; let m = 0xDEF0123456789ABCu64; assert_eq!(n.rotate_right(12), m); }
let n = 0x0123456789ABCDEFu64;
let m = 0xDEF0123456789ABCu64;

assert_eq!(n.rotate_right(12), m);

fn swap_bytes(self) -> Self

Reverses the byte order of the integer.

Example

fn main() { let n = 0x0123456789ABCDEFu64; let m = 0xEFCDAB8967452301u64; assert_eq!(n.swap_bytes(), m); }
let n = 0x0123456789ABCDEFu64;
let m = 0xEFCDAB8967452301u64;

assert_eq!(n.swap_bytes(), m);

Provided Methods

fn count_zeros(self) -> Self

Returns the number of zeros in the binary representation of the integer.

Example

fn main() { let n = 0b01001100u8; assert_eq!(n.count_zeros(), 5); }
let n = 0b01001100u8;

assert_eq!(n.count_zeros(), 5);

fn from_be(x: Self) -> Self

Convert a integer from big endian to the target's endianness.

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

Example

fn main() { let n = 0x0123456789ABCDEFu64; if cfg!(target_endian = "big") { assert_eq!(Int::from_be(n), n) } else { assert_eq!(Int::from_be(n), n.swap_bytes()) } }
let n = 0x0123456789ABCDEFu64;

if cfg!(target_endian = "big") {
    assert_eq!(Int::from_be(n), n)
} else {
    assert_eq!(Int::from_be(n), n.swap_bytes())
}

fn from_le(x: Self) -> Self

Convert a integer from little endian to the target's endianness.

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

Example

fn main() { let n = 0x0123456789ABCDEFu64; if cfg!(target_endian = "little") { assert_eq!(Int::from_le(n), n) } else { assert_eq!(Int::from_le(n), n.swap_bytes()) } }
let n = 0x0123456789ABCDEFu64;

if cfg!(target_endian = "little") {
    assert_eq!(Int::from_le(n), n)
} else {
    assert_eq!(Int::from_le(n), n.swap_bytes())
}

fn to_be(self) -> Self

Convert the integer to big endian from the target's endianness.

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

Example

fn main() { let n = 0x0123456789ABCDEFu64; if cfg!(target_endian = "big") { assert_eq!(n.to_be(), n) } else { assert_eq!(n.to_be(), n.swap_bytes()) } }
let n = 0x0123456789ABCDEFu64;

if cfg!(target_endian = "big") {
    assert_eq!(n.to_be(), n)
} else {
    assert_eq!(n.to_be(), n.swap_bytes())
}

fn to_le(self) -> Self

Convert the integer to little endian from the target's endianness.

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

Example

fn main() { let n = 0x0123456789ABCDEFu64; if cfg!(target_endian = "little") { assert_eq!(n.to_le(), n) } else { assert_eq!(n.to_le(), n.swap_bytes()) } }
let n = 0x0123456789ABCDEFu64;

if cfg!(target_endian = "little") {
    assert_eq!(n.to_le(), n)
} else {
    assert_eq!(n.to_le(), n.swap_bytes())
}

Implementors