[][src]Trait bitvec::Bits

pub trait Bits: Sealed + Binary + BitAnd<Self, Output = Self> + BitAndAssign<Self> + BitOrAssign<Self> + Copy + Debug + Display + Eq + From<u8> + Into<u64> + LowerHex + Not<Output = Self> + Shl<u8, Output = Self> + ShlAssign<u8> + Shr<u8, Output = Self> + ShrAssign<u8> + Sized + UpperHex {
    const SIZE: u8;
    const BITS: u8;
    const MASK: u8;
    const TYPENAME: &'static str;
    fn set<C: Cursor>(&mut self, place: BitIdx, value: bool) { ... }
fn get<C: Cursor>(&self, place: BitIdx) -> bool { ... }
fn count_ones(&self) -> usize { ... }
fn count_zeros(&self) -> usize { ... } }

Generalizes over the fundamental types for use in bitvec data structures.

This trait must only be implemented on unsigned integer primitives with full alignment. It cannot be implemented on u128 on any architecture, or on u64 on 32-bit systems.

The Sealed supertrait ensures that this can only be implemented locally, and will never be implemented by downstream crates on new types.

Associated Constants

const SIZE: u8

The size, in bits, of this type.

const BITS: u8

The number of bits required to index the type. This is always log2 of the type’s bit size.

Incidentally, this can be computed as Self::SIZE.trailing_zeros() once that becomes a valid constexpr.

const MASK: u8

The bitmask to turn an arbitrary usize into a bit index. Bit indices are always stored in the lowest bits of an index value.

const TYPENAME: &'static str

Name of the implementing type.

Loading content...

Provided methods

fn set<C: Cursor>(&mut self, place: BitIdx, value: bool)

Sets a specific bit in an element to a given value.

Parameters

  • place: A bit index in the element, from 0 at LSb to Self::MASK at MSb. The bit under this index will be set according to value.
  • value: A Boolean value, which sets the bit on true and unsets it on false.

Type Parameters

  • C: Cursor: A Cursor implementation to translate the index into a position.

Panics

This function panics if place is not less than T::SIZE, in order to avoid index out of range errors.

Examples

This example sets and unsets bits in a byte.

use bitvec::{Bits, LittleEndian};
let mut elt: u8 = 0;
elt.set::<LittleEndian>(0.into(), true);
assert_eq!(elt, 0b0000_0001);
elt.set::<LittleEndian>(4.into(), true);
assert_eq!(elt, 0b0001_0001);
elt.set::<LittleEndian>(0.into(), false);
assert_eq!(elt, 0b0001_0000);

This example overruns the index, and panics.

use bitvec::{Bits, LittleEndian};
let mut elt: u8 = 0;
elt.set::<LittleEndian>(8.into(), true);

fn get<C: Cursor>(&self, place: BitIdx) -> bool

Gets a specific bit in an element.

Parameters

  • place: A bit index in the element, from 0 at LSb to Self::MASK at MSb. The bit under this index will be retrieved as a bool.

Returns

The value of the bit under place, as a bool.

Type Parameters

  • C: Cursor: A Cursor implementation to translate the index into a position.

Panics

This function panics if place is not less than T::SIZE, in order to avoid index out of range errors.

Examples

This example gets two bits from a byte.

use bitvec::{Bits, LittleEndian};
let elt: u8 = 0b0000_0100;
assert!(!elt.get::<LittleEndian>(1.into()));
assert!(elt.get::<LittleEndian>(2.into()));
assert!(!elt.get::<LittleEndian>(3.into()));

This example overruns the index, and panics.

use bitvec::{Bits, LittleEndian};
0u8.get::<LittleEndian>(8.into());

fn count_ones(&self) -> usize

Counts how many bits in self are set to 1.

This zero-extends self to u64, and uses the u64::count_ones inherent method.

Parameters

  • &self

Returns

The number of bits in self set to 1. This is a usize instead of a u32 in order to ease arithmetic throughout the crate.

Examples

use bitvec::Bits;
assert_eq!(Bits::count_ones(&0u8), 0);
assert_eq!(Bits::count_ones(&128u8), 1);
assert_eq!(Bits::count_ones(&192u8), 2);
assert_eq!(Bits::count_ones(&224u8), 3);
assert_eq!(Bits::count_ones(&240u8), 4);
assert_eq!(Bits::count_ones(&248u8), 5);
assert_eq!(Bits::count_ones(&252u8), 6);
assert_eq!(Bits::count_ones(&254u8), 7);
assert_eq!(Bits::count_ones(&255u8), 8);

fn count_zeros(&self) -> usize

Counts how many bits in self are set to 0.

This inverts self, so all 0 bits are 1 and all 1 bits are 0, then zero-extends self to u64 and uses the u64::count_ones inherent method.

Parameters

  • &self

Returns

The number of bits in self set to 0. This is a usize instead of a u32 in order to ease arithmetic throughout the crate.

Examples

use bitvec::Bits;
assert_eq!(Bits::count_zeros(&0u8), 8);
assert_eq!(Bits::count_zeros(&1u8), 7);
assert_eq!(Bits::count_zeros(&3u8), 6);
assert_eq!(Bits::count_zeros(&7u8), 5);
assert_eq!(Bits::count_zeros(&15u8), 4);
assert_eq!(Bits::count_zeros(&31u8), 3);
assert_eq!(Bits::count_zeros(&63u8), 2);
assert_eq!(Bits::count_zeros(&127u8), 1);
assert_eq!(Bits::count_zeros(&255u8), 0);
Loading content...

Implementations on Foreign Types

impl Bits for u8[src]

const SIZE: u8[src]

const MASK: u8[src]

fn set<C: Cursor>(&mut self, place: BitIdx, value: bool)[src]

fn get<C: Cursor>(&self, place: BitIdx) -> bool[src]

fn count_ones(&self) -> usize[src]

fn count_zeros(&self) -> usize[src]

impl Bits for u16[src]

const SIZE: u8[src]

const MASK: u8[src]

fn set<C: Cursor>(&mut self, place: BitIdx, value: bool)[src]

fn get<C: Cursor>(&self, place: BitIdx) -> bool[src]

fn count_ones(&self) -> usize[src]

fn count_zeros(&self) -> usize[src]

impl Bits for u32[src]

const SIZE: u8[src]

const MASK: u8[src]

fn set<C: Cursor>(&mut self, place: BitIdx, value: bool)[src]

fn get<C: Cursor>(&self, place: BitIdx) -> bool[src]

fn count_ones(&self) -> usize[src]

fn count_zeros(&self) -> usize[src]

Loading content...

Implementors

Loading content...