[−][src]Trait bitvec::store::BitStore
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 BITS: u8
The width, in bits, of this type.
const INDX: u8
The number of bits required to index a bit inside the type. This is always log2 of the type’s bit width.
const MASK: u8
The bitmask to turn an arbitrary number 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. This is only necessary until the compiler
stabilizes type_name()
.
Provided methods
fn load(&self) -> Self
Performs a synchronized load on the underlying element.
Parameters
&self
Returns
The element referred to by the self
reference, loaded synchronously
after any in-progress accesses have concluded.
fn set<C>(&mut self, place: BitIdx, value: bool) where
C: Cursor,
C: Cursor,
Sets a specific bit in an element to a given value.
Parameters
place
: A bit index in the element, from0
toSelf::MASK
. The bit under this index will be set according tovalue
.value
: A Boolean value, which sets the bit ontrue
and unsets it onfalse
.
Type Parameters
C: Cursor
: ACursor
implementation to translate the index into a position.
Panics
This function panics if place
is not less than T::BITS
, in order to
avoid index out of range errors.
Examples
This example sets and unsets bits in a byte.
use bitvec::prelude::{ BitStore, BigEndian, LittleEndian, }; let mut elt: u16 = 0; elt.set::<BigEndian>(1.into(), true); assert_eq!(elt, 0b0100_0000__0000_0000); elt.set::<LittleEndian>(1.into(), true); assert_eq!(elt, 0b0100_0000__0000_0010); elt.set::<BigEndian>(1.into(), false); assert_eq!(elt, 0b0000_0000__0000_0010); elt.set::<LittleEndian>(1.into(), false); assert_eq!(elt, 0);
This example overruns the index, and panics.
use bitvec::prelude::{BitStore, BigEndian}; let mut elt: u8 = 0; elt.set::<BigEndian>(8.into(), true);
fn set_at(&mut self, place: BitPos, value: bool)
Sets a specific bit in an element to a given value.
Parameters
place
: A bit position in the element, where0
is the LSbit andSelf::MASK
is the MSbit.value
: A Boolean value, which sets the bit high ontrue
and unsets it low onfalse
.
Panics
This function panics if place
is not less than T::BITS
, in order to
avoid index out of range errors.
Examples
This example sets and unsets bits in a byte.
use bitvec::prelude::BitStore; let mut elt: u8 = 0; elt.set_at(0.into(), true); assert_eq!(elt, 0b0000_0001); elt.set_at(7.into(), true); assert_eq!(elt, 0b1000_0001);
This example overshoots the width, and panics.
use bitvec::prelude::BitStore; let mut elt: u8 = 0; elt.set_at(8.into(), true);
fn get<C>(&self, place: BitIdx) -> bool where
C: Cursor,
C: Cursor,
Gets a specific bit in an element.
Parameters
place
: A bit index in the element, from0
toSelf::MASK
. The bit under this index will be retrieved as abool
.
Returns
The value of the bit under place
, as a bool
.
Type Parameters
C: Cursor
: ACursor
implementation to translate the index into a position.
Panics
This function panics if place
is not less than T::BITS
, in order to
avoid index out of range errors.
Examples
This example gets two bits from a byte.
use bitvec::prelude::{BitStore, BigEndian}; let elt: u8 = 0b0010_0000; assert!(!elt.get::<BigEndian>(1.into())); assert!(elt.get::<BigEndian>(2.into())); assert!(!elt.get::<BigEndian>(3.into()));
This example overruns the index, and panics.
use bitvec::prelude::{BitStore, BigEndian}; 0u8.get::<BigEndian>(8.into());
fn get_at(&self, place: BitPos) -> bool
Gets a specific bit in an element.
Parameters
place
: A bit position in the element, from0
at LSbit toSelf::MASK
at MSbit. The bit under this position will be retrieved as abool
.
Returns
The value of the bit under place
, as a bool
.
Panics
This function panics if place
is not less than T::BITS
, in order to
avoid index out of range errors.
Examples
This example gets two bits from a byte.
use bitvec::prelude::BitStore; let elt: u8 = 0b0010_0000; assert!(!elt.get_at(4.into())); assert!(elt.get_at(5.into())); assert!(!elt.get_at(6.into()));
This example overruns the index, and panics.
use bitvec::prelude::BitStore; 0u8.get_at(8.into());
fn mask_at(place: BitPos) -> Self
Produces the bit mask which selects only the bit at the requested position.
This mask must be inverted in order to clear the bit.
Parameters
place
: The bit position for which to create a bitmask.
Returns
The one-hot encoding of the bit position index.
Panics
This function panics if place
is not less than T::BITS
, in order to
avoid index out of range errors.
Examples
This example produces the one-hot encodings for indices.
use bitvec::prelude::BitStore; assert_eq!(u8::mask_at(0.into()), 0b0000_0001); assert_eq!(u8::mask_at(1.into()), 0b0000_0010); assert_eq!(u8::mask_at(2.into()), 0b0000_0100); assert_eq!(u8::mask_at(3.into()), 0b0000_1000); assert_eq!(u8::mask_at(4.into()), 0b0001_0000); assert_eq!(u8::mask_at(5.into()), 0b0010_0000); assert_eq!(u8::mask_at(6.into()), 0b0100_0000); assert_eq!(u8::mask_at(7.into()), 0b1000_0000); assert_eq!(u16::mask_at(8.into()), 0b0000_0001__0000_0000); assert_eq!(u16::mask_at(9.into()), 0b0000_0010__0000_0000); assert_eq!(u16::mask_at(10.into()), 0b0000_0100__0000_0000); assert_eq!(u16::mask_at(11.into()), 0b0000_1000__0000_0000); assert_eq!(u16::mask_at(12.into()), 0b0001_0000__0000_0000); assert_eq!(u16::mask_at(13.into()), 0b0010_0000__0000_0000); assert_eq!(u16::mask_at(14.into()), 0b0100_0000__0000_0000); assert_eq!(u16::mask_at(15.into()), 0b1000_0000__0000_0000); assert_eq!(u32::mask_at(16.into()), 1 << 16); assert_eq!(u32::mask_at(24.into()), 1 << 24); assert_eq!(u32::mask_at(31.into()), 1 << 31); assert_eq!(u64::mask_at(32.into()), 1 << 32); assert_eq!(u64::mask_at(48.into()), 1 << 48); assert_eq!(u64::mask_at(63.into()), 1 << 63);
These examples ensure that indices panic when out of bounds.
use bitvec::prelude::BitStore; u8::mask_at(8.into());
use bitvec::prelude::BitStore; u16::mask_at(16.into());
use bitvec::prelude::BitStore; u32::mask_at(32.into());
use bitvec::prelude::BitStore; u64::mask_at(64.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::prelude::BitStore; assert_eq!(BitStore::count_ones(&0u8), 0); assert_eq!(BitStore::count_ones(&128u8), 1); assert_eq!(BitStore::count_ones(&192u8), 2); assert_eq!(BitStore::count_ones(&224u8), 3); assert_eq!(BitStore::count_ones(&240u8), 4); assert_eq!(BitStore::count_ones(&248u8), 5); assert_eq!(BitStore::count_ones(&252u8), 6); assert_eq!(BitStore::count_ones(&254u8), 7); assert_eq!(BitStore::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::prelude::BitStore; assert_eq!(BitStore::count_zeros(&0u8), 8); assert_eq!(BitStore::count_zeros(&1u8), 7); assert_eq!(BitStore::count_zeros(&3u8), 6); assert_eq!(BitStore::count_zeros(&7u8), 5); assert_eq!(BitStore::count_zeros(&15u8), 4); assert_eq!(BitStore::count_zeros(&31u8), 3); assert_eq!(BitStore::count_zeros(&63u8), 2); assert_eq!(BitStore::count_zeros(&127u8), 1); assert_eq!(BitStore::count_zeros(&255u8), 0);
fn bits(bit: bool) -> Self
Extends a single bit to fill the entire element.
Parameters
bit
: The bit to extend.
Returns
An element with all bits set to the input.
Implementations on Foreign Types
impl BitStore for u8
[src]
const TYPENAME: &'static str
[src]
type Atom = AtomicU8
const BITS: u8
[src]
const INDX: u8
[src]
const MASK: u8
[src]
fn load(&self) -> Self
[src]
fn set<C>(&mut self, place: BitIdx, value: bool) where
C: Cursor,
[src]
C: Cursor,
fn set_at(&mut self, place: BitPos, value: bool)
[src]
fn get<C>(&self, place: BitIdx) -> bool where
C: Cursor,
[src]
C: Cursor,
fn get_at(&self, place: BitPos) -> bool
[src]
fn mask_at(place: BitPos) -> Self
[src]
fn count_ones(&self) -> usize
[src]
fn count_zeros(&self) -> usize
[src]
fn bits(bit: bool) -> Self
[src]
impl BitStore for u16
[src]
const TYPENAME: &'static str
[src]
type Atom = AtomicU16
const BITS: u8
[src]
const INDX: u8
[src]
const MASK: u8
[src]
fn load(&self) -> Self
[src]
fn set<C>(&mut self, place: BitIdx, value: bool) where
C: Cursor,
[src]
C: Cursor,
fn set_at(&mut self, place: BitPos, value: bool)
[src]
fn get<C>(&self, place: BitIdx) -> bool where
C: Cursor,
[src]
C: Cursor,
fn get_at(&self, place: BitPos) -> bool
[src]
fn mask_at(place: BitPos) -> Self
[src]
fn count_ones(&self) -> usize
[src]
fn count_zeros(&self) -> usize
[src]
fn bits(bit: bool) -> Self
[src]
impl BitStore for u32
[src]
const TYPENAME: &'static str
[src]
type Atom = AtomicU32
const BITS: u8
[src]
const INDX: u8
[src]
const MASK: u8
[src]
fn load(&self) -> Self
[src]
fn set<C>(&mut self, place: BitIdx, value: bool) where
C: Cursor,
[src]
C: Cursor,
fn set_at(&mut self, place: BitPos, value: bool)
[src]
fn get<C>(&self, place: BitIdx) -> bool where
C: Cursor,
[src]
C: Cursor,
fn get_at(&self, place: BitPos) -> bool
[src]
fn mask_at(place: BitPos) -> Self
[src]
fn count_ones(&self) -> usize
[src]
fn count_zeros(&self) -> usize
[src]
fn bits(bit: bool) -> Self
[src]
impl BitStore for u64
[src]
const TYPENAME: &'static str
[src]
type Atom = AtomicU64
const BITS: u8
[src]
const INDX: u8
[src]
const MASK: u8
[src]
fn load(&self) -> Self
[src]
fn set<C>(&mut self, place: BitIdx, value: bool) where
C: Cursor,
[src]
C: Cursor,
fn set_at(&mut self, place: BitPos, value: bool)
[src]
fn get<C>(&self, place: BitIdx) -> bool where
C: Cursor,
[src]
C: Cursor,