[][src]Trait intbits::Bits

pub trait Bits {
    type Bits;

    const N_BITS: u32;

    fn bit<I>(self, i: I) -> bool
    where
        I: BitsIndex<Self>,
        Self: Sized
;
fn bits<I, R>(self, range: R) -> Self::Bits
    where
        I: BitsIndex<Self>,
        R: RangeBounds<I>,
        Self: Sized
;
fn set_bit<I>(&mut self, i: I, bit: bool)
    where
        I: BitsIndex<Self>,
        Self: Sized
;
fn set_bits<I, R>(&mut self, range: R, bits: Self::Bits)
    where
        I: BitsIndex<Self>,
        R: RangeBounds<I>,
        Self: Sized
;
fn with_bit<I>(self, i: I, bit: bool) -> Self
    where
        I: BitsIndex<Self>,
        Self: Sized
;
fn with_bits<I, R>(self, range: R, bits: Self::Bits) -> Self
    where
        I: BitsIndex<Self>,
        R: RangeBounds<I>,
        Self: Sized
; }

Extension trait to provide access to individual bits of integers.

Associated Types

type Bits

The (unsigned) type used to represent bits of this type.

For unsigned integers, this is Self. For signed integers, this is the unsigned variant of Self.

Example

assert_eq!(0 as <u8 as Bits>::Bits, 0u8);
assert_eq!(0 as <i64 as Bits>::Bits, 0u64);
assert_eq!(0 as <usize as Bits>::Bits, 0usize);
assert_eq!(0 as <isize as Bits>::Bits, 0usize);
Loading content...

Associated Constants

const N_BITS: u32

The number of bits this type has.

Example

assert_eq!(u8::N_BITS, 8);
assert_eq!(i64::N_BITS, 64);
Loading content...

Required methods

fn bit<I>(self, i: I) -> bool where
    I: BitsIndex<Self>,
    Self: Sized

Get a specific bit.

Panics if the index is out of range.

Example

assert_eq!(2u8.bit(0), false);
assert_eq!(2u8.bit(1), true);
assert_eq!(2u8.bit(2), false);

fn bits<I, R>(self, range: R) -> Self::Bits where
    I: BitsIndex<Self>,
    R: RangeBounds<I>,
    Self: Sized

Get a range of bits.

The bits are returned in the least significant bits of the return value. The other bits, if any, will be 0.

Empty ranges are allowed, and will result in 0.

Panics when the range bounds are out of range.

Example

assert_eq!(0x45u8.bits(0..4), 5);
assert_eq!(0x45u8.bits(4..8), 4);
assert_eq!(0xF1u8.bits(1..), 0x78);
assert_eq!(0xF1u8.bits(..7), 0x71);
assert_eq!(0xF1u8.bits(8..), 0);
assert_eq!(0xF1u8.bits(..0), 0);

fn set_bit<I>(&mut self, i: I, bit: bool) where
    I: BitsIndex<Self>,
    Self: Sized

Set a specific bit.

Panics if the index is out of range.

Example

let mut a = 0xFFu8;
a.set_bit(3, false);
assert_eq!(a, 0xF7);

fn set_bits<I, R>(&mut self, range: R, bits: Self::Bits) where
    I: BitsIndex<Self>,
    R: RangeBounds<I>,
    Self: Sized

Set a range of bits.

The bits should be given in the least significant bits of the second argument. The other bits should be 0.

Panics when the range bounds are out of range or when the irrelevant bits of the second argument are not 0.

Example

let mut a = 0xFFu8;
a.set_bits(4..8, 3);
assert_eq!(a, 0x3F);

fn with_bit<I>(self, i: I, bit: bool) -> Self where
    I: BitsIndex<Self>,
    Self: Sized

Get a new integer with one bit set to a specific value.

Panics if the index is out of range.

Example

assert_eq!(0xFFu8.with_bit(3, false), 0xF7);

fn with_bits<I, R>(self, range: R, bits: Self::Bits) -> Self where
    I: BitsIndex<Self>,
    R: RangeBounds<I>,
    Self: Sized

Get a new integer with a range of bits set to specific values.

The bits should be given in the least significant bits of the second argument. The other bits should be 0.

Panics when the range bounds are out of range or when the irrelevant bits of the second argument are not 0.

Example

assert_eq!(0xFFu8.with_bits(4..8, 3), 0x3F);
Loading content...

Implementors

impl Bits for i8[src]

type Bits = u8

impl Bits for i16[src]

type Bits = u16

impl Bits for i32[src]

type Bits = u32

impl Bits for i64[src]

type Bits = u64

impl Bits for i128[src]

type Bits = u128

impl Bits for isize[src]

type Bits = usize

impl Bits for u8[src]

type Bits = u8

impl Bits for u16[src]

type Bits = u16

impl Bits for u32[src]

type Bits = u32

impl Bits for u64[src]

type Bits = u64

impl Bits for u128[src]

type Bits = u128

impl Bits for usize[src]

type Bits = usize

Loading content...