pub struct BitIndex { /* private fields */ }Expand description
Structure for accessing individual bits in any structure that implements BitIndexable.
A BitIndex can be used to address any singular bit in an array of up to usize bytes.
This structure uses a usize to index the byte portion, and a separate u8 to index the bit of the
selected byte. There is also a sign associated with this structure for arithmetic purposes. Attempting to
access a bit at a negative position will cause a panic.
§Examples
use bitutils2::{BitIndex, BitIndexable};
let v = vec![0b01011100, 0b11001100];
// ^ ^
// (0,3) (1,7)
let i = BitIndex::new(0, 3);
assert_eq!(v.bit_at(&i), 1);
let i = BitIndex::new(1, 7);
assert_eq!(v.bit_at(&i), 0);Implementations§
Source§impl BitIndex
impl BitIndex
pub const MAX: BitIndex
pub const MIN: BitIndex
Sourcepub const fn new(byte: usize, bit: u8) -> BitIndex
pub const fn new(byte: usize, bit: u8) -> BitIndex
Constructs a new BitIndex with the specified byte and bit
values. The sign of the result is positive.
Sourcepub const fn bytes(byte: usize) -> BitIndex
pub const fn bytes(byte: usize) -> BitIndex
Constructs a new BitIndex with the specified byte value and bit
equal to zero. The sign of the result is positive.
Sourcepub const fn bits(bits: usize) -> BitIndex
pub const fn bits(bits: usize) -> BitIndex
Constructs a new BitIndex with the specified bit value and byte
equal to zero. The sign of the result is positive.
Sourcepub const fn zero() -> BitIndex
pub const fn zero() -> BitIndex
Constructs a new BitIndex with byte and bit both equal to zero.
Sourcepub const fn is_zero(&self) -> bool
pub const fn is_zero(&self) -> bool
Returns true if the byte and bit indices are both zero, regardless of sign.
Sourcepub const fn is_positive(&self) -> bool
pub const fn is_positive(&self) -> bool
Returns true if self is positive and false if self is zero or negative
Sourcepub const fn is_negative(&self) -> bool
pub const fn is_negative(&self) -> bool
Returns true if self is negative and false if self is zero or positive
Sourcepub const fn is_byte_boundary(&self) -> bool
pub const fn is_byte_boundary(&self) -> bool
Returns true if self lies on a byte boundary and false otherwise. Functionally
equivalent to self.bit() == 0
Sourcepub const fn cbit(&self) -> u8
pub const fn cbit(&self) -> u8
Returns the compliment of the bit portion of this BitIndex (8 - self.bit()).
pub const fn total_bits(&self) -> i128
Sourcepub fn ceil(&self) -> BitIndex
pub fn ceil(&self) -> BitIndex
Returns a new BitIndex the corresponds to
the nearest byte boundary greater than or equal to self.
§Example
use bitutils2::BitIndex;
assert_eq!(BitIndex::new(4, 2).ceil(), BitIndex::new(5, 0));
assert_eq!(BitIndex::new(4, 0).ceil(), BitIndex::new(4, 0));
// Negative case
assert_eq!((-BitIndex::new(1, 2)).ceil(), -BitIndex::new(1, 0));
Sourcepub fn set_bit(&mut self, bit: u8)
pub fn set_bit(&mut self, bit: u8)
Sets the bit portion of this BitIndex. bit must be less than 8.
Sourcepub fn add_bits(&mut self, nbits: usize)
pub fn add_bits(&mut self, nbits: usize)
Adds nbits bits to this BitIndex. The bit and byte portions will
be adjusted accordingly to ensure that the bit portion is always less than 8
pub const fn from_i64_bytes(byte: i64) -> BitIndex
Sourcepub fn rem_euclid(&self, rhs: &BitIndex) -> BitIndex
pub fn rem_euclid(&self, rhs: &BitIndex) -> BitIndex
Calculates the smallest non-negative value b such that self + b = n * rhs where n
is an integer.
Trait Implementations§
Source§impl AddAssign<&BitIndex> for BitIndex
impl AddAssign<&BitIndex> for BitIndex
Source§fn add_assign(&mut self, other: &BitIndex)
fn add_assign(&mut self, other: &BitIndex)
+= operation. Read moreSource§impl AddAssign<&usize> for BitIndex
impl AddAssign<&usize> for BitIndex
Source§fn add_assign(&mut self, other: &usize)
fn add_assign(&mut self, other: &usize)
+= operation. Read moreSource§impl AddAssign<usize> for BitIndex
impl AddAssign<usize> for BitIndex
Source§fn add_assign(&mut self, other: usize)
fn add_assign(&mut self, other: usize)
+= operation. Read moreSource§impl AddAssign for BitIndex
impl AddAssign for BitIndex
Source§fn add_assign(&mut self, other: BitIndex)
fn add_assign(&mut self, other: BitIndex)
+= operation. Read moreSource§impl Div<&BitIndex> for BitIndex
impl Div<&BitIndex> for BitIndex
Source§fn div(self, rhs: &BitIndex) -> Self::Output
fn div(self, rhs: &BitIndex) -> Self::Output
Performs integer division between self and rhs. Returns the largest integer
by which self can be multiplied such that self <= rhs. Output is negative if
exactly one of the inputs is negative. Panics if rhs is zero.
§Example
use bitutils2::BitIndex;
assert_eq!(BitIndex::new(4, 2) / &BitIndex::new(2, 1), 2);
assert_eq!(BitIndex::new(4, 1) / &BitIndex::new(2, 1), 1);
assert_eq!(BitIndex::new(403, 2) / &BitIndex::new(100, 0), 4);
// Negative case
assert_eq!(-BitIndex::new(4, 2) / &BitIndex::new(2, 1), -2);
assert_eq!(-BitIndex::new(4, 2) / &-BitIndex::new(2, 1), 2);
Source§impl Ord for BitIndex
impl Ord for BitIndex
Source§impl PartialOrd for BitIndex
impl PartialOrd for BitIndex
Source§impl Shl<BitIndex> for BitField
impl Shl<BitIndex> for BitField
Source§fn shl(self, rhs: BitIndex) -> Self::Output
fn shl(self, rhs: BitIndex) -> Self::Output
Returns a BitField with the bits shifted left by the magnitude of
rhs if rhs is positive or shifted right if rhs is negative. Bits that are dropped
off one side are wrapped around to fill the other side.
§Examples
use bitutils2::{BitField, BitIndex, bx};
let bf = BitField::from_bin_str("1100 0000 1111 00");
let bf = bf << bx!(,2);
assert_eq!(bf, BitField::from_bin_str("0000 0011 1100 11"));
let bf = bf << bx!(1 ,4);
assert_eq!(bf, BitField::from_bin_str("1100 0000 1111 00"));
// Negative bit indexes will result in right shifts
let bf = bf << -bx!(1 ,4);
assert_eq!(bf, BitField::from_bin_str("0000 0011 1100 11"));Source§impl ShlAssign<BitIndex> for BitField
impl ShlAssign<BitIndex> for BitField
Source§fn shl_assign(&mut self, rhs: BitIndex)
fn shl_assign(&mut self, rhs: BitIndex)
Transforms self by shifting all bits to the left by the magnitude of
rhs if rhs is positive or shifted right if rhs is negative. Bits that are dropped
off one side are wrapped around to fill the other side.
§Examples
use bitutils2::{BitField, BitIndex, bx};
let mut bf = BitField::from_bin_str("1100 0000 1111 00");
bf <<= bx!(,2);
assert_eq!(bf, BitField::from_bin_str("0000 0011 1100 11"));
bf <<= bx!(1, 4);
assert_eq!(bf, BitField::from_bin_str("1100 0000 1111 00"));
// Negative bit indexes will result in right shifts
bf <<= -bx!(1, 4);
assert_eq!(bf, BitField::from_bin_str("0000 0011 1100 11"));Source§impl Shr<BitIndex> for BitField
impl Shr<BitIndex> for BitField
Source§fn shr(self, rhs: BitIndex) -> Self::Output
fn shr(self, rhs: BitIndex) -> Self::Output
Returns a BitField with the bits shifted right by the magnitude of
rhs if rhs is positive or shifted left if rhs is negative. Bits that are dropped
off one side are wrapped around to fill the other side.
§Examples
use bitutils2::{BitField, BitIndex, bx};
let bf = BitField::from_bin_str("1100 0000 1111 00");
let bf = bf >> bx!(,2);
assert_eq!(bf, BitField::from_bin_str("0011 0000 0011 11"));
let bf = bf >> bx!(1 ,4);
assert_eq!(bf, BitField::from_bin_str("1100 0000 1111 00"));
// Negative bit indexes will result in left shifts
let bf = bf >> -bx!(1 ,4);
assert_eq!(bf, BitField::from_bin_str("0011 0000 0011 11"));Source§impl ShrAssign<BitIndex> for BitField
impl ShrAssign<BitIndex> for BitField
Source§fn shr_assign(&mut self, rhs: BitIndex)
fn shr_assign(&mut self, rhs: BitIndex)
Transforms self by shifting all bits to the right by the magnitude of
rhs if rhs is positive or shifted left if rhs is negative. Bits that are dropped
off one side are wrapped around to fill the other side.
§Examples
use bitutils2::{BitField, BitIndex, bx};
let mut bf = BitField::from_bin_str("1100 0000 1111 00");
bf >>= bx!(,2);
assert_eq!(bf, BitField::from_bin_str("0011 0000 0011 11"));
bf >>= bx!(1, 4);
assert_eq!(bf, BitField::from_bin_str("1100 0000 1111 00"));
// Negative bit indexes will result in left shifts
bf >>= -bx!(1, 4);
assert_eq!(bf, BitField::from_bin_str("0011 0000 0011 11"));