BitField

Struct BitField 

Source
pub struct BitField { /* private fields */ }
Expand description

Represents a finite array of contiguous bits that supports several operations such as indexing, slicing, shifting, etc.

The BitField structure stores the bits packed in an array of bytes for memory efficiency, but attempts to make this mostly unapparant in the API. When a singular bit is accessed, it is returned as a u8 (either 0x00 or 0x01) to facilitate performing bitwise operations with the result.

§Examples

§Constructors

 use bitutils2::{BitField, BitIndex};

 // Returns a bitfield with 0001 0010 0011 0100 0100 0101 0000 0000 
 let bf1 = BitField::new(vec![0x12, 0x34, 0x45, 0x00], BitIndex::new(4, 0));
 // Returns a bitfield with 0001 0010 0011 0100 0100 0101 00. The bytes represented by the
 // vector are clipped to the bit index provided.
 let bf2 = BitField::new(vec![0x12, 0x34, 0x45, 0x00], BitIndex::new(3, 2));

 // Returns the same bitfield as 'bf1' but automatically infers the length from 
 // the provided vec. This constructor cannot be used to produce a bit field that 
 // does not end on a byte boundary like 'bf2'
 let bf3 = BitField::from_vec(vec![0x12, 0x34, 0x45, 0x00]);

 assert_eq!(bf1, bf3);
 assert_ne!(bf1, bf2); // bf1 and bf3 are not equal because they have different lengths

 
 // Returns a bitfield with 0001 0010 0011 0100 0100 0101 00 and an inferred length
 // of 3 bytes and 2 bits. This constructor can be used to create a bitfield of
 // any length. Spaces or underscores can be used anywhere in the input and are
 // ignored by the constructor.
 let bf4 = BitField::from_bin_str("0001 0010 0011 0100 0100 0101 00");

 // bf5 has the same contents as bf4 but is two bits longer
 let bf5 = BitField::from_bin_str("0001 0010 0011 0100 0100 0101 0000");

 assert_eq!(bf2, bf4); // bf2 and bf4 are equal in both length and contents
 assert_ne!(bf4, bf5); // bf4 and bf5 are not equal because they have different lengths

 // Returns a bitfield with 0001 0010 0011 0100 0100 0101 0000 and an inferred length
 // of 3 bytes and 4 bits. This constructor can only be used to create bitfields witg
 // lengths that end on nibble boundaries. Spaces or underscores can be used anywhere 
 // in the input and are ignored by the constructor.
 let bf6 = BitField::from_hex_str("12 34 45 0");

 // bf7 has the same contents as bf6 but is four bits (one nibble) longer
 let bf7 = BitField::from_hex_str("12 34 45 00");

 assert_eq!(bf5, bf6); // bf5 and bf6 are equal in both length and contents
 assert_eq!(bf3, bf7); // bf3 and bf7 are equal in both length and contents

 // Returns a bitfield with 5 bytes of 0x00
 let bf8 = BitField::zeros(BitIndex::new(5, 0));

 

§Bitwise AND, OR, XOR, and NOT

The common bitwise operations (AND, OR, XOR, NOT) are supported. If the two sides have the same length, then the operation is applied to every bit in the arguments and the resulting BitField is returned. If the two sides have different lengths, then The operation is only applied to bits up to the shortest length and the resulting BitField is returned.

 use bitutils2::{BitField, BitIndex};

 // Initializing two bitfields that have the same length, and one that is much shorter
 let bf1 = BitField::from_bin_str("0011 1010 0000 1111 1100 0101 0111");
 let bf2 = BitField::from_bin_str("0101 0000 1100 0111 1010 1111 0001");
 let bf3 = BitField::from_bin_str("0101 0000 1100 01");

 // Bitwise AND (&)
 assert_eq!(&bf1 & &bf2, BitField::from_bin_str("0001 0000 0000 0111 1000 0101 0001"));
 assert_eq!(&bf1 & &bf3, BitField::from_bin_str("0001 0000 0000 01"));

 // Bitwise OR (|)
 assert_eq!(&bf1 | &bf2, BitField::from_bin_str("0111 1010 1100 1111 1110 1111 0111"));
 assert_eq!(&bf1 | &bf3, BitField::from_bin_str("0111 1010 1100 11"));

 // Bitwise XOR (^)
 assert_eq!(&bf1 ^ &bf2, BitField::from_bin_str("0110 1010 1100 1000 0110 1010 0110"));
 assert_eq!(&bf1 ^ &bf3, BitField::from_bin_str("0110 1010 1100 10"));

 // Bitwise NOT (!)
 assert_eq!(!&bf1, BitField::from_bin_str("1100 0101 1111 0000 0011 1010 1000"));
 assert_eq!(!&bf2, BitField::from_bin_str("1010 1111 0011 1000 0101 0000 1110"));
 assert_eq!(!&bf3, BitField::from_bin_str("1010 1111 0011 10"));

Implementations§

Source§

impl BitField

Source

pub fn new(v: Vec<u8>, length: BitIndex) -> BitField

Returns a new BitField structure with the contents of the vector v interpreted as bytes and the length length in bits. If the length does not lie on a byte boundary, it is expected that the vector will have a final element that specifies the remaining bits.

Panics if length is negative

§Examples
 use bitutils2::{BitField, BitIndex};

 let bf = BitField::new(vec![0b11001100, 0b01010101, 0b00110000], BitIndex::new(2, 4));
 assert_eq!(bf, BitField::from_bin_str("1100 1100 0101 0101 0011"));

 let bf = BitField::new(vec![0b11001100, 0b01010101, 0b00110000], BitIndex::new(3, 0));
 assert_eq!(bf, BitField::from_bin_str("1100 1100 0101 0101 0011 0000"));
Source

pub fn from_vec(v: Vec<u8>) -> BitField

Returns a BitField structure with the contents of the vector v.

The length of the bitfield is automatically calculated as the number of bits in the input vector. To create a BitField that is not an integral number of bytes long, use the new constructor.

§Examples
 use bitutils2::{BitField, BitIndex};

 let bf = BitField::from_vec(vec![0x00, 0xab, 0xff]);
 assert_eq!(bf.len(), BitIndex::new(3, 0));
 assert_eq!(bf, BitField::from_bin_str("0000 0000 1010 1011 1111 1111"));
Source

pub fn zeros(length: BitIndex) -> BitField

Creates and returns a BitField of zeros (0x00) with the given length.

§Panics:

Panics if length is negative

Source

pub fn ones(length: BitIndex) -> BitField

Creates and returns a BitField of ones (0xFF) with the given length.

§Panics:

Panics if length is negative

Source

pub fn from_bin_str(s: &str) -> BitField

Parses a BitField from a str of ones and zeros. Underscores and spaces are allowed and are ignored. Panics if any other character is encountered

§Panics:

Panics if s contains any character other than 0, 1, _, or .

Source

pub fn from_hex_str(s: &str) -> BitField

Parses a BitField from a str of hex characters (0-9, a-f, or A-F). Underscores and spaces are allowed and are ignored. Panics if any other character is encountered.

§Panics:

Panics if s contains any character other than 0-9, a-f, A-F, _, or .

Source

pub fn to_bin_str(&self) -> String

Source

pub fn len(&self) -> BitIndex

Returns the length of self as a BitIndex.

Source

pub fn is_empty(&self) -> bool

Returns true if the length is zero and false otherwise

Source

pub fn clear(&mut self)

Deletes the contents of the BitField and sets the length to 0.

Source

pub fn truncate_be(&mut self, new_length: BitIndex)

Truncates the BitField to new_length by retaining the most-significant bits assuming a big-endian byte order if new_length is shorter than the current length. Does nothing otherwise.

Equivalent to truncate_le if new_length is on a byte boundary.

Panics if new_length is negative.

§Examples
 use bitutils2::{BitField, BitIndex};

 let mut bf = BitField::from_bin_str("0101 1100 1111 0000 1010 1100");
 //         Most significant 2B6b ->  ---- ---- ---- ---- ---- --
 bf.truncate_be(BitIndex::new(2, 6));

 assert_eq!(bf, BitField::from_bin_str("0101 1100 1111 0000 1010 11"));
 //           Most significant 2B3b ->  ---- ---- ---- ---- ---
 bf.truncate_be(BitIndex::new(2, 3));

 assert_eq!(bf, BitField::from_bin_str("0101 1100 1111 0000 101"));
 //           Most significant 0B4b ->  ----
 bf.truncate_be(BitIndex::new(0, 4));

 assert_eq!(bf, BitField::from_bin_str("0101"));

 // Does nothing if new_length >= length
 bf.truncate_be(BitIndex::new(1, 4));
 assert_eq!(bf, BitField::from_bin_str("0101"));
Source

pub fn truncate_le(&mut self, new_length: BitIndex)

Truncates the BitField to new_length by retaining the least-significant bits assuming a little-endian byte order if new_length is shorter than the current length. Does nothing otherwise.

Equivalent to truncate_be if new_length is on a byte boundary.

Panics if new_length is negative.

§Examples
 use bitutils2::{BitField, BitIndex};

 let mut bf = BitField::from_bin_str("0101 1100 1111 0000 1010 1100");
 //        Least significant 2B6b ->  ---- ---- ---- ----   -- ----
 bf.truncate_le(BitIndex::new(2, 6));

 assert_eq!(bf, BitField::from_bin_str("0101 1100 1111 0000 1011 00"));
 //          Least significant 2B3b ->  ---- ---- ---- ----    - --
 bf.truncate_le(BitIndex::new(2, 3));

 assert_eq!(bf, BitField::from_bin_str("0101 1100 1111 0000 100"));
 //               Least significant 0B4b ->  ----
 bf.truncate_le(BitIndex::new(0, 4));

 assert_eq!(bf, BitField::from_bin_str("1100"));

 // Does nothing if new_length >= length
 bf.truncate_le(BitIndex::new(1, 4));
 assert_eq!(bf, BitField::from_bin_str("1100"));
Source

pub fn extend(&mut self, other: &BitField, be: bool)

Concatenates the argument onto the end of the BitField, adjusting the length of the BitField accordingly.

Source

pub fn iter_bytes(&self, ms: bool) -> Box<dyn Iterator<Item = u8> + '_>

Iterates over the bytes of a BitField, from the least significant to the most significant assuming little endian byte order.

§Examples
 use bitutils2::{BitField, BitIndex};

 let bf1 = BitField::from_bin_str("0101 1100 11");
 let mut i = bf1.iter_bytes(false);
 assert_eq!(i.next(), Some(0b01011100));
 assert_eq!(i.next(), Some(0b11));
 assert_eq!(i.next(), None);
Source

pub fn extend_be(&mut self, other: &BitField)

Concatenates the argument onto the end of the BitField, adjusting the length of the BitField accordingly.

§Examples
 use bitutils2::{BitField, BitIndex};

 let mut bf1 = BitField::from_bin_str("0101 1100 11");
 bf1.extend_be(&BitField::from_bin_str("0011 1010 0001"));
 assert_eq!(bf1, BitField::from_bin_str("0101 1100 1100 1110 1000 01"));
Source

pub fn extend_le(&mut self, other: &BitField)

Concatenates the argument onto the end of the BitField, adjusting the length of the BitField accordingly.

§Examples
 use bitutils2::{BitField, BitIndex};

 let mut bf1 = BitField::from_bin_str("0101 1100 11");
 //                                    OPQR STUV MN
 bf1.extend_le(&BitField::from_bin_str("0011 1010 0001"));
 //                                     EFGH IJKL ABCD
 assert_eq!(bf1, BitField::from_bin_str("0101 1100 1110 1011 0001 00"));
 //                                      OPQR STUV GHIJ KLMN ABCD EF
Source

pub fn repeat_be(&mut self, n: usize)

Source

pub fn repeat_le(&mut self, n: usize)

Source

pub fn repeat_until_be(&mut self, new_length: BitIndex)

Extends self to the new provided length by repeating as many times as needed to fill the new length. Final repetition is truncated to the specified length. If the specified length is less than self’s current length, then self is truncated to the new length.

§Panics

Panics if the provided length is negative or if self is empty

§Examples
 use bitutils2::{BitField, BitIndex};

 let mut bf = BitField::from_bin_str("0101 1100 11");
 bf.repeat_until_be(BitIndex::new(5, 4));
 assert_eq!(bf, BitField::from_bin_str("0101 1100 1101 0111 0011 0101 1100 1101 0111 0011 0101"));

 // If the new length is less than the current length, then self is truncated.
 bf.repeat_until_be(BitIndex::new(0, 6));
 assert_eq!(bf, BitField::from_bin_str("0101 11"));
Source

pub fn repeat_until_le(&mut self, new_length: BitIndex)

Source

pub fn find_first_one(&self) -> Option<BitIndex>

Finds the index of the first 1 bit in self. Returns None if there are no zeros in self or if self is empty.

§Examples
 use bitutils2::{BitField, BitIndex};

 let bf = BitField::from_bin_str("0000 0101 1100 1100 0000");
 assert_eq!(bf.find_first_one().unwrap(), BitIndex::bits(5));
 let bf = BitField::from_bin_str("1000 0101 1100 1100 0000");
 assert_eq!(bf.find_first_one().unwrap(), BitIndex::bits(0));
 let bf = BitField::from_bin_str("0000 0000 0000 0000 0000 0000");
 assert!(bf.find_first_one().is_none());
 let bf = BitField::from_vec(vec![]);
 assert!(bf.find_first_one().is_none());
Source

pub fn find_next_one(&self, start: BitIndex) -> Option<BitIndex>

Finds the index of the next 1 bit in self, starting from and including the provided start index. Returns None if there are no zeros in self after or including start or if self is empty.

§Examples
 use bitutils2::{BitField, BitIndex};

 let mut bf = BitField::from_bin_str("0101 1100 1100 0000 0000 0010 0001 0000 0000");
 assert_eq!(bf.find_next_one(BitIndex::zero()).unwrap(), BitIndex::bits(1));
 assert_eq!(bf.find_next_one(BitIndex::bits(1)).unwrap(), BitIndex::bits(1));
 assert_eq!(bf.find_next_one(BitIndex::bits(11)).unwrap(), BitIndex::bits(22));
 assert!(bf.find_next_one(BitIndex::bits(29)).is_none());
 assert!(bf.find_next_one(BitIndex::bits(100)).is_none());
Source

pub fn shove_left(&mut self, new: &BitField)

Inserts the bits specified in new to the right of self, shifting the existing contents of self left to accommodate the new data without changing self’s length. If new is longer than self, then self will be overwritten with the rightmost data in new.

§Examples
 use bitutils2::{BitField, BitIndex};

 let mut bf = BitField::from_bin_str("0101 1100 1100 0011 1010");

 // When new is shorter than self, self's data is left shifted by new's length
 bf.shove_left(&BitField::from_bin_str("1101 00"));
 assert_eq!(bf, BitField::from_bin_str("0011 0000 1110 1011 0100"));

 // When new is the same length as self, self is overwritten with the data in new
 bf.shove_left(&BitField::from_bin_str("0101 1010 1101 0011 1100"));
 assert_eq!(bf, BitField::from_bin_str("0101 1010 1101 0011 1100"));

 // When new is longer than self, self is overwritten with the rightmost data in new
 bf.shove_left(&BitField::from_bin_str("1100 0011 1001 0110 0101 1100"));
 assert_eq!(bf, BitField::from_bin_str("0011 1001 0110 0101 1100"));
Source

pub fn shove_right(&mut self, new: BitField)

Inserts the bits specified in new to the left of self, shifting the existing contents of self right to accommodate the new data without changing self’s length. If new is longer than self, then self will be overwritten with the leftmost data in new.

§Examples
 use bitutils2::{BitField, BitIndex};

 let mut bf = BitField::from_bin_str("0101 1100 1100 0011 1010");

 // When new is shorter than self, self's data is right shifted by new's length
 bf.shove_right(BitField::from_bin_str("1101 00"));
 assert_eq!(bf, BitField::from_bin_str("1101 0001 0111 0011 0000"));

 // When new is the same length as self, self is overwritten with the data in new
 bf.shove_right(BitField::from_bin_str("0101 1010 1101 0011 1100"));
 assert_eq!(bf, BitField::from_bin_str("0101 1010 1101 0011 1100"));

 // When new is longer than self, self is overwritten with the leftmost data in new
 bf.shove_right(BitField::from_bin_str("1100 0011 1001 0110 0101 1100"));
 assert_eq!(bf, BitField::from_bin_str("1100 0011 1001 0110 0101"));
Source

pub fn slice_with_rpad( &self, start: &BitIndex, end: &BitIndex, pad: BitPad, ) -> BitField

Returns a slice of self that may extend beyond the length of self. The returned slice will be padded according to the provided pad parameter. If both start and end are less than self’s length, this is equivalant to bit_slice. If both start and end are greater than self’s length, then the return value is entirely comprised of padding.

#Panics Panics if end is less than start or if either is negative.

§Examples
 use bitutils2::{BitField, BitIndex, BitPad};

 let bf = BitField::from_bin_str("0101 1100 1100 0011 1010");

 // When the slice is contained within the bitfield, no padding is done.
 let slice_zeros = bf.slice_with_rpad(&BitIndex::new(0, 4), &BitIndex::new(2, 2), BitPad::Zeros);
 let slice_ones  = bf.slice_with_rpad(&BitIndex::new(0, 4), &BitIndex::new(2, 2), BitPad::Ones);
 assert_eq!(slice_zeros, BitField::from_bin_str("1100 1100 0011 10"));
 assert_eq!(slice_ones , BitField::from_bin_str("1100 1100 0011 10"));

 // When the slice is partially contained within the bitfield, then the remaining
 // portion will be filled in with padding.
 let slice_zeros = bf.slice_with_rpad(&BitIndex::new(1, 2), &BitIndex::new(3, 4), BitPad::Zeros);
 let slice_ones  = bf.slice_with_rpad(&BitIndex::new(1, 2), &BitIndex::new(3, 4), BitPad::Ones);
 assert_eq!(slice_zeros, BitField::from_bin_str("0000 1110 1000 0000 00"));
 assert_eq!(slice_ones , BitField::from_bin_str("0000 1110 1011 1111 11"));

 // When the slice is fully beyond the bitfield, then the entire slice will be filled in with padding.
 let slice_zeros = bf.slice_with_rpad(&BitIndex::new(2, 4), &BitIndex::new(3, 6), BitPad::Zeros);
 let slice_ones  = bf.slice_with_rpad(&BitIndex::new(2, 4), &BitIndex::new(3, 6), BitPad::Ones);
 assert_eq!(slice_zeros, BitField::from_bin_str("0000 0000 00"));
 assert_eq!(slice_ones , BitField::from_bin_str("1111 1111 11"));
Source

pub fn slice_with_pad( &self, start: &BitIndex, end: &BitIndex, lpad: BitPad, rpad: BitPad, ) -> BitField

Returns a slice of self that may extend beyond the contents of self in the positive and/or negative directions. The returned slice will be padded to the left (in the case of a negative start index) according to the lpad parameter, and to the right (in the case of a end index that is greater than self’s length) according to the provided rpad parameter. If neither start nor end are negative, then this is equivalent in function to slice_with_rpad

§Panics

Panics if end is less than start.

§Examples
 use bitutils2::{BitField, BitIndex, BitPad};

 let bf = BitField::from_bin_str("0101 1100 1100 0011 1010");

 // When the slice is contained within the bitfield, no padding is done.
 let slice_l0_r1 = bf.slice_with_pad(&BitIndex::new(0, 4), &BitIndex::new(2, 2), BitPad::Zeros, BitPad::Ones);
 let slice_r0_l1 = bf.slice_with_pad(&BitIndex::new(0, 4), &BitIndex::new(2, 2), BitPad::Ones, BitPad::Zeros);
 assert_eq!(slice_l0_r1, BitField::from_bin_str("1100 1100 0011 10"));
 assert_eq!(slice_r0_l1, BitField::from_bin_str("1100 1100 0011 10"));

 // When the slice is partially contained within the bitfield, then the remaining
 // portion will be filled in with padding.
 let slice_l0_r1 = bf.slice_with_pad(&BitIndex::new(1, 2), &BitIndex::new(3, 4), BitPad::Zeros, BitPad::Ones);
 let slice_r0_l1 = bf.slice_with_pad(&BitIndex::new(1, 2), &BitIndex::new(3, 4), BitPad::Ones, BitPad::Zeros);

 assert_eq!(slice_l0_r1, BitField::from_bin_str("0000 1110 10 11 1111 11"));
 assert_eq!(slice_r0_l1, BitField::from_bin_str("0000 1110 10 00 0000 00"));
 //                                              \----------/ \--------/
 //                                                 from bf    from rpad

 // When the slice starts at a negative index, then that portion will be filled in with padding.
 let slice_l0_r1 = bf.slice_with_pad(&-BitIndex::new(1, 2), &BitIndex::new(3, 4), BitPad::Zeros, BitPad::Ones);
 let slice_r0_l1 = bf.slice_with_pad(&-BitIndex::new(1, 2), &BitIndex::new(3, 4), BitPad::Ones, BitPad::Zeros);

 assert_eq!(slice_l0_r1, BitField::from_bin_str("00 0000 0000 0101 1100 1100 0011 1010 1111 1111"));
 assert_eq!(slice_r0_l1, BitField::from_bin_str("11 1111 1111 0101 1100 1100 0011 1010 0000 0000"));
 //                                              \----------/ \----------------------/ \--------/
 //                                                from lpad          from bf           from rpad

 // When the slice is fully negative, then it will be filled in completely with rpad
 let slice_l0_r1 = bf.slice_with_pad(&-BitIndex::new(1, 2), &-BitIndex::new(0, 2), BitPad::Zeros, BitPad::Ones);
 let slice_r0_l1 = bf.slice_with_pad(&-BitIndex::new(1, 2), &-BitIndex::new(0, 2), BitPad::Ones, BitPad::Zeros);

 assert_eq!(slice_l0_r1, BitField::from_bin_str("0000 0000"));
 assert_eq!(slice_r0_l1, BitField::from_bin_str("1111 1111"));
 //                                              \-------/
 //                                              from lpad
Source

pub fn crc_be(&self, initial: BitField, polynomial: BitField) -> BitField

Calculates the CRC of self using the provided initial value and polynomial assuming big-endian bit order. Note that the polynomial paramter must not include the leading 1.

Panics if the initial and polynomial parameters are different lengths.

Source

pub fn swap_le_to_be(&mut self)

Swaps the byte order of self from litte-endian to big-endian. Does nothing if self is less than or equal to one byte long. Note that in the case of a BitField that does not end on a byte coundary, this method is not equivalent to swap_be_to_le. This method will always undo the effect of swap_be_to_le and vice-versa.

§Examples
 use bitutils2::{BitField, BitIndex};

 let mut bf = BitField::from_bin_str("0101 1100 0110 1001 011");
 bf.swap_le_to_be();
 assert_eq!(bf, BitField::from_bin_str("0110 1101 0010 1011 100"));
Source

pub fn swap_be_to_le(&mut self)

Swaps the byte order of self from big-endian to little-endian. Does nothing if self is less than or equal to one byte long. Note that in the case of a BitField that does not end on a byte coundary, this method is not equivalent to swap_le_to_be. This method will always undo the effect of swap_le_to_be and vice-versa.

§Examples
 use bitutils2::{BitField, BitIndex};

 let mut bf = BitField::from_bin_str("0101 1100 0110 1001 011");
 
 bf.swap_be_to_le();    //             "0101 1100 0110 1001 011"
 assert_eq!(bf, BitField::from_bin_str("0100 1011 1110 0011 010"));
Source

pub fn map_be_to_le(&self, index: &BitIndex) -> BitIndex

Maps a BitIndex that corresponds to a big-endian location in self to one that corresponds to the same bit if self were little-endian.

§Examples
 use bitutils2::{BitField, BitIndex, BitIndexable};

 let mut bf = BitField::from_bin_str("0101 1100 0110 1001 011");
 let mut bf2 = bf.clone();
 
 bf2.swap_be_to_le();
 for i in 0..19 {
     let bi1 = BitIndex::bits(i);
     let bi2 = bf.map_be_to_le(&bi1);
     println!("New Index: {:?}", bi2);
    assert_eq!(bf.bit_at(&bi1), bf2.bit_at(&bi2));
 }

 let mut bf = BitField::from_bin_str("0101 1100 0110 1001 0110 1001");
 let mut bf2 = bf.clone();
 
 bf2.swap_be_to_le();
 for i in 0..24 {
     let bi1 = BitIndex::bits(i);
     let bi2 = bf.map_be_to_le(&bi1);
     println!("New Index: {:?}", bi2);
    assert_eq!(bf.bit_at(&bi1), bf2.bit_at(&bi2));
 }
Source

pub fn extract_u8(&self, start: BitIndex) -> u8

Source

pub fn extract_u8_cyclical(&self, start: BitIndex) -> u8

Source

pub fn into_boxed_slice(self) -> Result<Box<[u8]>, ()>

Converts self into a boxed slice, where each element represents a byte. Returns Err if self does not end on a byte boundary and Ok otherwise. This function is helpful in converting from a BitField to other variable-length types such as str or Vec.

§Example:
 use bitutils2::{BitField, BitIndex, BitIndexable};

 // If self ends on a byte boundary, then into_boxed_slice is a useful way
 // to convert to other variable-length types such as str.
 let bf = BitField::from_hex_str("48 65 6C 6C 6F 20 57 6F 72 6C 64 21");
 let boxed_slice = bf.into_boxed_slice().unwrap();
 let s = std::str::from_utf8(&boxed_slice).unwrap();
 assert_eq!(s, "Hello World!");

 // If self does not end on a byte boundary, into_boxed_slice returns Err.
 let bf = BitField::from_bin_str("0101 1100 1111");
 assert!(bf.into_boxed_slice().is_err());
 
Source

pub fn into_array<const N: usize>(self) -> Result<[u8; N], ()>

Converts self into a byte array. Returns Err if self does not end on a byte boundary or is the wrong length for the destination type and Ok otherwise. This function is helpful in converting from a BitField to fixed-length types such as integers or floats.

§Example:
 use bitutils2::{BitField, BitIndex, BitIndexable};

 // If self ends on a byte boundary, then into_array is a useful way
 // to convert to fixed-length types such as u16.
 let bf = BitField::from_hex_str("AB CD");
 let arr = bf.into_array().unwrap();
 let n = u16::from_be_bytes(arr);
 assert_eq!(n, 0xABCD);

 // If self does not end on a byte boundary, into_array returns Err.
 let bf = BitField::from_bin_str("0101 1100 1111");
 assert!(bf.into_array::<2>().is_err());
 
 // If self is the wrong length for the destination type, into_array returns Err.
 let bf = BitField::from_bin_str("0101 1100 1111 0000");
 assert!(bf.into_array::<4>().is_err());
 
Source

pub fn slice_be(&self, start: &BitIndex, end: &BitIndex) -> BitField

Returns the specified slice of self assuming that self is in a big-endian format. The bits in the slice are arranged in such a way that their relative magnitude is preserved when interpreted as a big-endian byte array. Equivalent to the bit_slice implementation for this type.

Source

pub fn slice_le(&self, start: &BitIndex, end: &BitIndex) -> BitField

Returns the specified slice of self assuming that self is in a little-endian format. The bits in the slice are arranged in such a way that their relative magnitude is preserved when interpreted as a little-endian byte array. If start is not byte-aligned, then the most significant bits from the start byte are included in the slice, and if end is not byte-aligned, then the least significant bits from the end byte are included in the slice.

§Panics

Panics if start or end is negative, start is greater than end, or end is greater than self’s length.

§Example
 use bitutils2::{BitField, BitIndex};
 
 let mut bf = BitField::from_bin_str("0101 1100 1010 0110 0101 1001 1011 0000");
 //                                   CBA       KJIH GFED  RQP ONML                                  
 let bf2 = bf.slice_le(&BitIndex::new(0, 5), &BitIndex::new(2, 7));
 
 //                                      HGFE DCBA PONM LKJI RQ
 assert_eq!(bf2, BitField::from_bin_str("0011 0010 1100 1101 10"));

 let mut bf = BitField::from_bin_str("0101 1100 1010 0110 0101 1001 1011 0");
 let bf2 = bf.slice_le(&BitIndex::new(0, 5), &BitIndex::new(3, 3));

 assert_eq!(bf2, BitField::from_bin_str("0011 0010 1100 1101 1100 10"));

 let mut bf = BitField::from_bin_str("0101 1100 1010 0110 0101 1001 1011 01");
 let bf2 = bf.slice_le(&BitIndex::new(0, 5), &BitIndex::new(3, 5));
 assert_eq!(bf2, BitField::from_bin_str("0011 0010 1100 1101 0110 1010"));
Source

pub fn slice_le2(&self, start: &BitIndex, end: &BitIndex) -> BitField

Returns the specified slice of self assuming that self is in a little-endian format, but uses the least significant portion of the first byte and the most-significant portion of the last byte. The bits in the slice are arranged in such a way that their relative magnitude is preserved when interpreted as a little-endian byte array.

§Example
 use bitutils2::{BitField, BitIndex};
 //                                        0B5b                 2B7b
 //                                         v                     v
 let mut bf = BitField::from_bin_str("0101 1100 1010 0110 0101 1001 1111 0000");
 //                                  |____ _AAA|CCCB BBBB|EEDD DDD_|
 //                                  |MSB-->LSB|MSB-->LSB|MSB-->LSB|
 //                                     Byte 2    Byte 1    Byte 0
 //
 // AAA   = Least significant bits of least significant byte in slice
 // BBBBB = Least significant bits of second-to-least significant byte in slice
 // CCC   = Most significant bits of second-to-least significant byte in slice
 // DDDDD = Least significant bits of most significant byte in slice
 // EE    = Most signiciant bits of most significant byte in slice
 //                                     
 let bf2 = bf.slice_le2(&BitIndex::new(0, 5), &BitIndex::new(2, 7));
 
 //                                      BBBB BAAA|DDDD DCCC|EE
 assert_eq!(bf2, BitField::from_bin_str("0011 0100 0110 0101 01"));
Source

pub fn convert_unsigned_be(&mut self, src_format: IntFormat) -> bool

Converts the data contained within self to a big-endian unsigned integer by removing the sign information according to the source format provided. Returns true if the sign was negative before being removed, even if the magnitude is 0. Returns false if the sign was positive, even if the magnitude is 0.

If the source format is Unsigned, then self is not mutated and false is returned.

Source

pub fn convert_unsigned_le(&mut self, src_format: IntFormat) -> bool

Converts the data contained within self to a little-endian unsigned integer by removing the sign information according to the source format provided. Returns true if the sign was negative before being removed, even if the magnitude is 0. Returns false if the sign was positive, even if the magnitude is 0.

If the source format is Unsigned, then self is not mutated and false is returned.

Source

pub fn pad_unsigned_be(&mut self, new_length: BitIndex)

Pads self to the specified length in such a way that when interpreted as an unsigned big-endian integer, the value is unchanged. More specifically, self is extended to the new length by padding the left side with zeros.

This also works for base -2 numbers

Does nothing if the provided length is less than or equal to self’s current length.

§Examples
 use bitutils2::{BitField, BitIndex};

 let mut bf = BitField::from_vec(vec![0x30, 0x39]);
 assert_eq!(u16::from_be_bytes(bf.clone().into_array().unwrap()), 12345);

 bf.pad_unsigned_be(BitIndex::bits(32));
 assert_eq!(u32::from_be_bytes(bf.clone().into_array().unwrap()), 12345);

 bf.pad_unsigned_be(BitIndex::bits(64));
 assert_eq!(u64::from_be_bytes(bf.clone().into_array().unwrap()), 12345);
Source

pub fn pad_unsigned_le(&mut self, new_length: BitIndex)

Pads self to the specified length in such a way that when interpreted as an unsigned little-endian integer, the value is unchanged. More specifically, self is extended to the new length by padding the right side with zeros and, if self doesn’t currently end on a byte boundary, shifting the contents of the last partial byte so that they retain the same significance

This also works for base -2 numbers

Does nothing if the provided length is less than or equal to self’s current length.

§Examples
 use bitutils2::{BitField, BitIndex};

 let mut bf = BitField::from_vec(vec![0x39, 0x30]);
 assert_eq!(u16::from_le_bytes(bf.clone().into_array().unwrap()), 12345);

 bf.pad_unsigned_le(BitIndex::bits(32));
 assert_eq!(u32::from_le_bytes(bf.clone().into_array().unwrap()), 12345);

 bf.pad_unsigned_le(BitIndex::bits(64));
 assert_eq!(u64::from_le_bytes(bf.clone().into_array().unwrap()), 12345);
Source

pub fn pad_twos_compliment_be(&mut self, new_length: BitIndex)

Pads self to the specified length in such a way that when interpreted as an signed twos-compliment big-endian integer, the value is unchanged. More specifically, self is extended to the new length by padding the left side with either zeros or ones depending on the value of the most significant bit.

This also works for one’s compliment numbers.

Does nothing if the provided length is less than or equal to self’s current length.

§Examples
 use bitutils2::{BitField, BitIndex};

 let mut bf = BitField::from_vec(vec![0x30, 0x39]);
 assert_eq!(i16::from_be_bytes(bf.clone().into_array().unwrap()), 12345);

 bf.pad_twos_compliment_be(BitIndex::bits(32));
 assert_eq!(i32::from_be_bytes(bf.clone().into_array().unwrap()), 12345);

 bf.pad_twos_compliment_be(BitIndex::bits(64));
 assert_eq!(i64::from_be_bytes(bf.clone().into_array().unwrap()), 12345);

 let mut bf = BitField::from_vec(vec![0xcf, 0xc7]);
 assert_eq!(i16::from_be_bytes(bf.clone().into_array().unwrap()), -12345);

 bf.pad_twos_compliment_be(BitIndex::bits(32));
 assert_eq!(i32::from_be_bytes(bf.clone().into_array().unwrap()), -12345);

 bf.pad_twos_compliment_be(BitIndex::bits(64));
 assert_eq!(i64::from_be_bytes(bf.clone().into_array().unwrap()), -12345);
Source

pub fn pad_twos_compliment_le(&mut self, new_length: BitIndex)

Pads self to the specified length in such a way that when interpreted as an signed twos-compliment little-endian integer, the value is unchanged. More specifically, self is extended to the new length by padding the right side with either zeros or ones depending on the value of the most significant byte. In addition, if self doesn’t currently end on a byte boundary, shifting the contents of the last partial byte so that they retain the same significance.

This also works for one’s compliment numbers.

Does nothing if the provided length is less than or equal to self’s current length.

§Examples
 use bitutils2::{BitField, BitIndex};

 let mut bf = BitField::from_vec(vec![0x39, 0x30]);
 assert_eq!(i16::from_le_bytes(bf.clone().into_array().unwrap()), 12345);

 bf.pad_twos_compliment_le(BitIndex::bits(32));
 assert_eq!(i32::from_le_bytes(bf.clone().into_array().unwrap()), 12345);

 bf.pad_twos_compliment_le(BitIndex::bits(64));
 assert_eq!(i64::from_le_bytes(bf.clone().into_array().unwrap()), 12345);

 let mut bf = BitField::from_vec(vec![0xc7, 0xcf]);
 assert_eq!(i16::from_le_bytes(bf.clone().into_array().unwrap()), -12345);

 bf.pad_twos_compliment_le(BitIndex::bits(32));
 assert_eq!(i32::from_le_bytes(bf.clone().into_array().unwrap()), -12345);

 bf.pad_twos_compliment_le(BitIndex::bits(64));
 assert_eq!(i64::from_le_bytes(bf.clone().into_array().unwrap()), -12345);
Source

pub fn pad_sign_magnitude_be(&mut self, new_length: BitIndex)

Pads self to the specified length in such a way that when interpreted as an sign-magnitude big-endian integer, the value is unchanged. More specifically, the sign bit is removed, self is extended to the new length by padding the left side with zeros, and the sign bit is reinserted at the new MSB location.

Does nothing if the provided length is less than or equal to self’s current length.

§Examples
 use bitutils2::{BitField, BitIndex};

 let mut bf = BitField::from_vec(vec![0x30, 0x39]);
 let u = u16::from_be_bytes(bf.clone().into_array().unwrap());
 assert_eq!(u & 0x7fff, 12345); // Magnitude = 12345
 assert_eq!((u & 0x8000) >> 15, 0); // Sign = positive

 bf.pad_sign_magnitude_be(BitIndex::bits(32));
 let u = u32::from_be_bytes(bf.clone().into_array().unwrap());
 assert_eq!(u & 0x7fffffff, 12345); // Magnitude = 12345
 assert_eq!((u & 0x80000000) >> 31, 0); // Sign = positive

 bf.pad_sign_magnitude_be(BitIndex::bits(64));
 let u = u64::from_be_bytes(bf.clone().into_array().unwrap());
 assert_eq!(u & 0x7fffffffffffffff, 12345); // Magnitude = 12345
 assert_eq!((u & 0x8000000000000000) >> 63, 0); // Sign = positive

 let mut bf = BitField::from_vec(vec![0xb0, 0x39]);
 let u = u16::from_be_bytes(bf.clone().into_array().unwrap());
 assert_eq!(u & 0x7fff, 12345); // Magnitude = 12345
 assert_eq!((u & 0x8000) >> 15, 1); // Sign = negative

 bf.pad_sign_magnitude_be(BitIndex::bits(32));
 let u = u32::from_be_bytes(bf.clone().into_array().unwrap());
 assert_eq!(u & 0x7fffffff, 12345); // Magnitude = 12345
 assert_eq!((u & 0x80000000) >> 31, 1); // Sign = negative

 bf.pad_sign_magnitude_be(BitIndex::bits(64));
 let u = u64::from_be_bytes(bf.clone().into_array().unwrap());
 assert_eq!(u & 0x7fffffffffffffff, 12345); // Magnitude = 12345
 assert_eq!((u & 0x8000000000000000) >> 63, 1); // Sign = negative
Source

pub fn pad_sign_magnitude_le(&mut self, new_length: BitIndex)

Pads self to the specified length in such a way that when interpreted as an sign-magnitude little-endian integer, the value is unchanged. More specifically, the sign bit is removed, self is extended to the new length using pad_unsigned_le, and the sign bit is reinserted at the new MSB location.

Does nothing if the provided length is less than or equal to self’s current length.

§Examples
 use bitutils2::{BitField, BitIndex};

 let mut bf = BitField::from_vec(vec![0x39, 0x30]);
 let u = u16::from_le_bytes(bf.clone().into_array().unwrap());
 assert_eq!(u & 0x7fff, 12345); // Magnitude = 12345
 assert_eq!((u & 0x8000) >> 15, 0); // Sign = positive

 bf.pad_sign_magnitude_le(BitIndex::bits(32));
 let u = u32::from_le_bytes(bf.clone().into_array().unwrap());
 assert_eq!(u & 0x7fffffff, 12345); // Magnitude = 12345
 assert_eq!((u & 0x80000000) >> 31, 0); // Sign = positive

 bf.pad_sign_magnitude_le(BitIndex::bits(64));
 let u = u64::from_le_bytes(bf.clone().into_array().unwrap());
 assert_eq!(u & 0x7fffffffffffffff, 12345); // Magnitude = 12345
 assert_eq!((u & 0x8000000000000000) >> 63, 0); // Sign = positive

 let mut bf = BitField::from_vec(vec![0x39, 0xb0]);
 let u = u16::from_le_bytes(bf.clone().into_array().unwrap());
 assert_eq!(u & 0x7fff, 12345); // Magnitude = 12345
 assert_eq!((u & 0x8000) >> 15, 1); // Sign = negative

 bf.pad_sign_magnitude_le(BitIndex::bits(32));
 let u = u32::from_le_bytes(bf.clone().into_array().unwrap());
 assert_eq!(u & 0x7fffffff, 12345); // Magnitude = 12345
 assert_eq!((u & 0x80000000) >> 31, 1); // Sign = negative

 bf.pad_sign_magnitude_le(BitIndex::bits(64));
 let u = u64::from_le_bytes(bf.clone().into_array().unwrap());
 assert_eq!(u & 0x7fffffffffffffff, 12345); // Magnitude = 12345
 assert_eq!((u & 0x8000000000000000) >> 63, 1); // Sign = negative

Trait Implementations§

Source§

impl BitAnd for &BitField

Source§

fn bitand(self, rhs: &BitField) -> BitField

Returns the bitwise & operation on the two inputs. If the two inputs have different lengths, then the returned value will have the length of the shortest input.

§Example
 use bitutils2::{BitField, BitIndex};

 let bf1 = BitField::from_bin_str("0011 1010 0000 1111 1100 0101 0111");
 let bf2 = BitField::from_bin_str("0101 0000 1100 0111 1010 1111 0001");
 assert_eq!(&bf1 & &bf2, BitField::from_bin_str("0001 0000 0000 0111 1000 0101 0001"));
Source§

type Output = BitField

The resulting type after applying the & operator.
Source§

impl BitAndAssign<&BitField> for BitField

Source§

fn bitand_assign(&mut self, rhs: &BitField)

Transforms self to have the value of self & rhs. If the two inputs have different lengths, then the resulting value will have the length of the shortest input.

§Example
 use bitutils2::{BitField, BitIndex};

 let mut bf1 = BitField::from_bin_str("0011 1010 0000 1111 1100 0101 0111");
 bf1 &= &BitField::from_bin_str("0101 0000 1100 0111 1010 1111 0001");
 assert_eq!(bf1, BitField::from_bin_str("0001 0000 0000 0111 1000 0101 0001"));
Source§

impl BitIndexable for BitField

Source§

fn bit_at(&self, index: &BitIndex) -> u8

Get the bit at the given bit index. Returns a u8 instead of a bool to accommodate operations such as bitshifts on the result, but this function should always return either 0 or 1.
Source§

fn bit_slice(&self, start: &BitIndex, end: &BitIndex) -> BitField

Source§

fn max_index(&self) -> BitIndex

Get the bit index that is equivalent to the length of the structure (accessing at this index is out of bounds, but any index below this is allowed)
Source§

impl BitOr for &BitField

Source§

fn bitor(self, rhs: &BitField) -> BitField

Returns the bitwise | operation on the two inputs. If the two inputs have different lengths, then the returned value will have the length of the shortest input.

§Example
 use bitutils2::{BitField, BitIndex};

 let bf1 = BitField::from_bin_str("0011 1010 0000 1111 1100 0101 0111");
 let bf2 = BitField::from_bin_str("0101 0000 1100 0111 1010 1111 0001");
 assert_eq!(&bf1 | &bf2, BitField::from_bin_str("0111 1010 1100 1111 1110 1111 0111"));
Source§

type Output = BitField

The resulting type after applying the | operator.
Source§

impl BitOrAssign<&BitField> for BitField

Source§

fn bitor_assign(&mut self, rhs: &BitField)

Transforms self to have the value of self | rhs. If the two inputs have different lengths, then the resulting value will have the length of the shortest input.

§Example
 use bitutils2::{BitField, BitIndex};

 let mut bf1 = BitField::from_bin_str("0011 1010 0000 1111 1100 0101 0111");
 bf1 |= &BitField::from_bin_str("0101 0000 1100 0111 1010 1111 0001");
 assert_eq!(bf1, BitField::from_bin_str("0111 1010 1100 1111 1110 1111 0111"));
Source§

impl BitXor for &BitField

Source§

fn bitxor(self, rhs: &BitField) -> BitField

Returns the bitwise ^ operation on the two inputs. If the two inputs have different lengths, then the returned value will have the length of the shortest input.

§Example
 use bitutils2::{BitField, BitIndex};

 let bf1 = BitField::from_bin_str("0011 1010 0000 1111 1100 0101 0111");
 let bf2 = BitField::from_bin_str("0101 0000 1100 0111 1010 1111 0001");
 assert_eq!(&bf1 ^ &bf2, BitField::from_bin_str("0110 1010 1100 1000 0110 1010 0110"));
Source§

type Output = BitField

The resulting type after applying the ^ operator.
Source§

impl BitXorAssign<&BitField> for BitField

Source§

fn bitxor_assign(&mut self, rhs: &BitField)

Transforms self to have the value of self ^ rhs. If the two inputs have different lengths, then the resulting value will have the length of the shortest input.

§Example
 use bitutils2::{BitField, BitIndex};

 let mut bf1 = BitField::from_bin_str("0011 1010 0000 1111 1100 0101 0111");
 bf1 ^= &BitField::from_bin_str("0101 0000 1100 0111 1010 1111 0001");
 assert_eq!(bf1, BitField::from_bin_str("0110 1010 1100 1000 0110 1010 0110"));
Source§

impl Clone for BitField

Source§

fn clone(&self) -> BitField

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 BitField

Source§

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

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

impl Default for BitField

Source§

fn default() -> BitField

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

impl Not for &BitField

Source§

fn not(self) -> BitField

Returns the bitwise ! operation on the input.

§Example
 use bitutils2::{BitField, BitIndex};

 let bf = BitField::from_bin_str("0011 1010 0000 1111 1100 0101 0111");
 assert_eq!(!&bf, BitField::from_bin_str("1100 0101 1111 0000 0011 1010 1000"));
Source§

type Output = BitField

The resulting type after applying the ! operator.
Source§

impl PartialEq for BitField

Source§

fn eq(&self, other: &Self) -> 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 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 Shl<usize> for BitField

Source§

fn shl(self, rhs: usize) -> Self::Output

Returns a BitField with the bits shifted left by rhs bits. Bits that are dropped off the left side are wrapped around to fill the right side.

§Examples
 use bitutils2::{BitField, BitIndex};

 let bf = BitField::from_bin_str("1100 0000 1111 00");
 let bf = bf << 2;
 assert_eq!(bf, BitField::from_bin_str("0000 0011 1100 11"));
 let bf = bf << 4;
 assert_eq!(bf, BitField::from_bin_str("0011 1100 1100 00"));
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 ShlAssign<usize> for BitField

Source§

fn shl_assign(&mut self, rhs: usize)

Transforms self by shifting all bits to the left by rhs bits. Bits that are dropped off the left side are wrapped around to fill the right side.

§Examples
 use bitutils2::{BitField, BitIndex};

 let mut bf = BitField::from_bin_str("1100 0000 1111 00");
 bf <<= 2;
 assert_eq!(bf, BitField::from_bin_str("0000 0011 1100 11"));
 bf <<= 4;
 assert_eq!(bf, BitField::from_bin_str("0011 1100 1100 00"));
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 Shr<usize> for BitField

Source§

fn shr(self, rhs: usize) -> Self::Output

Returns a BitField with the bits shifted right by rhs bits. Bits that are dropped off the right side are wrapped around to fill the left side.

§Examples
 use bitutils2::{BitField, BitIndex};

 let bf = BitField::from_bin_str("1100 0000 1111 00");
 let bf = bf >> 2;
 assert_eq!(bf, BitField::from_bin_str("0011 0000 0011 11"));
 let bf = bf >> 4;
 assert_eq!(bf, BitField::from_bin_str("1111 0011 0000 00"));
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 ShrAssign<usize> for BitField

Source§

fn shr_assign(&mut self, rhs: usize)

Transforms self by shifting all bits to the right by rhs bits. Bits that are dropped off the right side are wrapped around to fill the right side.

§Examples
 use bitutils2::{BitField, BitIndex};

 let mut bf = BitField::from_bin_str("1100 0000 1111 00");
 bf >>= 2;
 assert_eq!(bf, BitField::from_bin_str("0011 0000 0011 11"));
 bf >>= 4;
 assert_eq!(bf, BitField::from_bin_str("1111 0011 0000 00"));
Source§

impl Eq for BitField

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, 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.