BitIndex

Struct BitIndex 

Source
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

Source

pub const MAX: BitIndex

Source

pub const MIN: BitIndex

Source

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.

Source

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.

Source

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.

Source

pub const fn zero() -> BitIndex

Constructs a new BitIndex with byte and bit both equal to zero.

Source

pub const fn is_zero(&self) -> bool

Returns true if the byte and bit indices are both zero, regardless of sign.

Source

pub const fn is_positive(&self) -> bool

Returns true if self is positive and false if self is zero or negative

Source

pub const fn is_negative(&self) -> bool

Returns true if self is negative and false if self is zero or positive

Source

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

Source

pub const fn byte(&self) -> usize

Returns the bit portion of this BitIndex.

Source

pub const fn bit(&self) -> u8

Returns the bit portion of this BitIndex.

Source

pub const fn cbit(&self) -> u8

Returns the compliment of the bit portion of this BitIndex (8 - self.bit()).

Source

pub const fn total_bits(&self) -> i128

Source

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));
Source

pub const fn abs(&self) -> BitIndex

Returns the absolute value of self

Source

pub fn set_bit(&mut self, bit: u8)

Sets the bit portion of this BitIndex. bit must be less than 8.

Source

pub fn set_byte(&mut self, byte: usize)

Sets the byte portion of this BitIndex

Source

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

Source

pub const fn from_i64_bytes(byte: i64) -> BitIndex

Source

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<'a, 'b> Add<&'a BitIndex> for &'b BitIndex

Source§

type Output = BitIndex

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &'a BitIndex) -> Self::Output

Performs the + operation. Read more
Source§

impl Add<&usize> for BitIndex

Source§

type Output = BitIndex

The resulting type after applying the + operator.
Source§

fn add(self, other: &usize) -> BitIndex

Performs the + operation. Read more
Source§

impl Add<usize> for BitIndex

Source§

type Output = BitIndex

The resulting type after applying the + operator.
Source§

fn add(self, other: usize) -> BitIndex

Performs the + operation. Read more
Source§

impl Add for BitIndex

Source§

type Output = BitIndex

The resulting type after applying the + operator.
Source§

fn add(self, rhs: BitIndex) -> Self::Output

Performs the + operation. Read more
Source§

impl AddAssign<&BitIndex> for BitIndex

Source§

fn add_assign(&mut self, other: &BitIndex)

Performs the += operation. Read more
Source§

impl AddAssign<&usize> for BitIndex

Source§

fn add_assign(&mut self, other: &usize)

Performs the += operation. Read more
Source§

impl AddAssign<usize> for BitIndex

Source§

fn add_assign(&mut self, other: usize)

Performs the += operation. Read more
Source§

impl AddAssign for BitIndex

Source§

fn add_assign(&mut self, other: BitIndex)

Performs the += operation. Read more
Source§

impl Clone for BitIndex

Source§

fn clone(&self) -> BitIndex

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BitIndex

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for BitIndex

Source§

fn default() -> BitIndex

Returns the “default value” for a type. Read more
Source§

impl Display for BitIndex

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Div<&BitIndex> for BitIndex

Source§

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§

type Output = i128

The resulting type after applying the / operator.
Source§

impl FromStr for BitIndex

Source§

type Err = ParseBitIndexError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Mul<i128> for BitIndex

Source§

type Output = BitIndex

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: i128) -> Self::Output

Performs the * operation. Read more
Source§

impl Neg for &BitIndex

Source§

type Output = BitIndex

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl Neg for BitIndex

Source§

type Output = BitIndex

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl Ord for BitIndex

Source§

fn cmp(&self, other: &BitIndex) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for BitIndex

Source§

fn eq(&self, other: &BitIndex) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for BitIndex

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Shl<BitIndex> for BitField

Source§

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§

type Output = BitField

The resulting type after applying the << operator.
Source§

impl ShlAssign<BitIndex> for BitField

Source§

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

Source§

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§

type Output = BitField

The resulting type after applying the >> operator.
Source§

impl ShrAssign<BitIndex> for BitField

Source§

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"));
Source§

impl<'a, 'b> Sub<&'a BitIndex> for &'b BitIndex

Source§

type Output = BitIndex

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &'a BitIndex) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub for BitIndex

Source§

type Output = BitIndex

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: BitIndex) -> Self::Output

Performs the - operation. Read more
Source§

impl Copy for BitIndex

Source§

impl Eq for BitIndex

Source§

impl StructuralPartialEq for BitIndex

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.