pub struct Bits { /* private fields */ }Implementations§
Source§impl Bits
impl Bits
Sourcepub fn new<I: IntoIterator<Item = u8>>(bits: I) -> Self
pub fn new<I: IntoIterator<Item = u8>>(bits: I) -> Self
Creates a bit string from a sequence of bytes.
§Examples
let mut x = Bits::new([0x0A, 0x0B, 0x0C, 0x00]);
assert_eq!(x.len(), 32);Sourcepub fn aligned<I: IntoIterator<Item = u8>>(units: usize, bits: I) -> Self
pub fn aligned<I: IntoIterator<Item = u8>>(units: usize, bits: I) -> Self
Creates a bit string with an aligned length.
§Examples
let mut x = Bits::aligned(17, [0x0A, 0x0B, 0x0C]);
assert_eq!(x.len(), 34);
assert_eq!(x, Bits::slice(&[0x0A, 0x0B, 0x0C, 0x00, 0x00], 34));Sourcepub fn one() -> Self
pub fn one() -> Self
Creates a single bit string set to 1.
§Examples
let mut x = Bits::one();
assert_eq!(x.len(), 1);
assert_eq!(x, Bits::from([1]));Sourcepub fn ones(length: usize) -> Self
pub fn ones(length: usize) -> Self
Creates a bit string of ones.
§Examples
let mut x = Bits::ones(4);
assert_eq!(x.len(), 4);
assert_eq!(x, Bits::from([0x0F]));Sourcepub fn packed<I: IntoIterator<Item = u8>>(bytes: I) -> Self
pub fn packed<I: IntoIterator<Item = u8>>(bytes: I) -> Self
Creates a bit string by concatenating a slice of bytes removing leading zeros.
§Examples
let x1 = Bits::packed([0x00, 0x00, 0x00]);
assert_eq!(x1, Bits::empty());
let x2 = Bits::packed([0x0A, 0x0B, 0x0C]);
assert_eq!(x2, Bits::from([0xBA, 0x0C]));
assert_eq!(x2.len(), 12);Sourcepub fn slice(src: &[u8], length: usize) -> Self
pub fn slice(src: &[u8], length: usize) -> Self
Creates a bit string by copying a given number of bits from a slice of bytes.
When the length is greater than the available number of bits in the source, the result is padded with zeros.
§Examples
let x1 = Bits::slice(&[], 17);
assert_eq!(x1, Bits::zeros(17));
let x2 = Bits::slice(&[0x0A, 0x0B, 0x0C], 0);
assert_eq!(x2, Bits::empty());
let x3 = Bits::slice(&[0x0A, 0x0B, 0x0C], 19);
assert_eq!(x3, Bits::from([0x0A, 0x0B, 0x04]));
let x4 = Bits::slice(&[0x00, 0x00, 0x00], 19);
assert_eq!(x4, Bits::zeros(19));Sourcepub fn take(src: &[u8], start: usize, length: usize) -> Self
pub fn take(src: &[u8], start: usize, length: usize) -> Self
Creates a bit string by copying a given range of bits from a slice of bytes.
When the length is greater than the available number of bits in the source, the result is padded with zeros.
§Examples
let x1 = Bits::take(&[], 1, 17);
assert_eq!(x1, Bits::zeros(17));
let x2 = Bits::take(&[0x0A, 0x0B, 0x0C], 1, 0);
assert_eq!(x2, Bits::empty());
let x3 = Bits::take(&[0x0A, 0x0B, 0x0C], 1, 1);
assert_eq!(x3, Bits::from([0x01]));
let x4 = Bits::take(&[0x0A, 0x0B, 0x0C], 25, 17);
assert_eq!(x4, Bits::zeros(17));
let x5 = Bits::take(&[0x0A, 0x0B, 0x0C], 1, 17);
assert_eq!(x5.bytes().to_vec(), vec![0x85, 0x05, 0x00]);
let x6 = Bits::take(&[0x0A, 0x0B, 0x0C], 1, 16);
assert_eq!(x6.bytes().to_vec(), vec![0x85, 0x05]);
let x7 = Bits::take(&[0x00, 0x00, 0x00], 1, 17);
assert_eq!(x7, Bits::zeros(17));Sourcepub fn zero() -> Self
pub fn zero() -> Self
Creates a single bit string set to 0.
§Examples
let mut x = Bits::zero();
assert_eq!(x.len(), 1);
assert_eq!(x, Bits::slice(&[0], 1));Sourcepub fn zeros(length: usize) -> Self
pub fn zeros(length: usize) -> Self
Creates a bit string of zeros.
§Examples
let mut x = Bits::zeros(4);
assert_eq!(x.len(), 4);
assert_eq!(x, Bits::from([false, false, false, false]));Sourcepub fn align(&mut self, unit: usize)
pub fn align(&mut self, unit: usize)
Pads a bit string until the length is a multiple of the given unit.
§Examples
let mut x = Bits::from([0x0A, 0x0B, 0x0C]);
x.align(16);
assert_eq!(x, Bits::new([0x0A, 0x0B, 0x0C, 0x00]));
assert_eq!(x.len(), 32);Sourcepub fn all(&self) -> bool
pub fn all(&self) -> bool
Whether all bits are one.
§Examples
let x1 = Bits::empty();
assert!(x1.all());
let x2 = Bits::from([0x0F]);
assert!(x2.all());
let x3 = Bits::new([0x0F]);
assert!(!x3.all());Sourcepub fn and(&self, rhs: &Bits) -> Bits
pub fn and(&self, rhs: &Bits) -> Bits
Bitwise and of two bit strings.
§Examples
let x1 = Bits::from([0x20, 0x30, 0x40]);
let y1 = Bits::from([0xA0, 0xB0, 0xC0]);
assert_eq!(x1.and(&y1), Bits::new([0x20, 0x30, 0x40]));
let x2 = Bits::from([0x20, 0x30, 0x40]);
let y2 = Bits::from([0x0A, 0xB0, 0xC0]);
assert_eq!(x2.len(), 23);
assert_eq!(y2.len(), 24);
let z = x2.and(&y2);
assert_eq!(z.len(), 24);Sourcepub fn and_mut(&mut self, rhs: &Bits)
pub fn and_mut(&mut self, rhs: &Bits)
Bitwise and of two bit strings.
§Examples
let mut x1 = Bits::from([0x20, 0x30, 0x40]);
let y1 = Bits::from([0xA0, 0xB0, 0xC0]);
x1.and_mut(&y1);
assert_eq!(x1, Bits::new([0x20, 0x30, 0x40]));
let mut x2 = Bits::from([0x20, 0x30, 0x40]);
let y2 = Bits::from([0x0A, 0xB0, 0xC0]);
assert_eq!(x2.len(), 23);
assert_eq!(y2.len(), 24);
x2.and_mut(&y2);
assert_eq!(x2.len(), 24);Sourcepub fn any(&self) -> bool
pub fn any(&self) -> bool
Whether at least one bit is one.
§Examples
let x1 = Bits::empty();
assert!(!x1.any());
let x2 = Bits::from([0x0A, 0x0B, 0x0C]);
assert!(x2.any());
let x3 = Bits::zeros(20);
assert!(!x3.any());Sourcepub fn bytes(&self) -> &[u8] ⓘ
pub fn bytes(&self) -> &[u8] ⓘ
Underlying bytes
§Example
let x = Bits::new([0xAB, 0xCD, 0xEF]);
let y = x.bytes().iter().cloned().collect::<Vec<u8>>();
assert_eq!(y, vec![0xAB, 0xCD, 0xEF]);Sourcepub fn complement(&self) -> Bits
pub fn complement(&self) -> Bits
Flips every bit.
§Examples
let x = Bits::from([0x0A, 0x0B, 0x0C]);
assert_eq!(x.complement(), Bits::slice(&[0xF5, 0xF4, 0x03], 20));Sourcepub fn complement_mut(&mut self)
pub fn complement_mut(&mut self)
Flips every bit.
§Examples
let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]);
x1.complement_mut();
assert_eq!(x1, Bits::slice(&[0xF5, 0xF4, 0x03], 20));Sourcepub fn consume_left(&mut self, count: usize)
pub fn consume_left(&mut self, count: usize)
Shifts out upper bits.
§Examples
let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]);
x1.consume_left(4);
assert_eq!(x1.len(), 16);
assert_eq!(x1, Bits::new([0x0A, 0x0B]));
let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]);
x2.consume_left(4);
assert_eq!(x2.len(), 20);
assert_eq!(x2, Bits::from([0x0A, 0x0B, 0x0C]));Sourcepub fn consume_right(&mut self, count: usize)
pub fn consume_right(&mut self, count: usize)
Shifts out lower bits.
§Examples
let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]);
x1.consume_right(4);
assert_eq!(x1, Bits::from([0xB0, 0xC0]));
assert_eq!(x1.len(), 16);
let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]);
x2.consume_right(4);
assert_eq!(x2, Bits::slice(&[0xB0, 0xC0, 0x00], 20));
assert_eq!(x2.len(), 20);Sourcepub fn count_zeros(&self) -> usize
pub fn count_zeros(&self) -> usize
The number of zeros.
§Examples
let mut x = Bits::from([15, 15, 15]);
assert_eq!(x.count_zeros(), 8);
x = Bits::new([15, 15, 15]);
assert_eq!(x.count_zeros(), 12);Sourcepub fn copy_range<R: RangeBounds<usize>>(&self, bounds: R) -> Self
pub fn copy_range<R: RangeBounds<usize>>(&self, bounds: R) -> Self
Creates a bit string by copying a range of bits.
The range is truncated to take no more than the number of available bits from the starting bound. For alternative behavior, consider Bits::take, which pads zeros when the length exceeds the number of available bits.
§Examples
let x1 = Bits::empty();
let x1_r1 = x1.copy_range(1..18);
assert_eq!(x1_r1, Bits::empty());
let x2 = Bits::zeros(24);
let x2_r1 = x2.copy_range(8..24);
let x2_r2 = x2.copy_range(8..=23);
assert_eq!(x2_r1, Bits::zeros(16));
assert_eq!(x2_r2, x2_r1);
let x3 = Bits::from([0x0A, 0x0B, 0x0C]);
let x3_r1 = x3.copy_range(1..2);
assert_eq!(x3_r1, Bits::from([0x01]));
let x3_r2 = x3.copy_range(0..);
let x3_r3 = x3.copy_range(..);
assert_eq!(x3_r2, x3);
assert_eq!(x3_r3, x3_r2);
let x3_r4 = x3.copy_range(..32);
assert_eq!(x3_r4, Bits::from([0x0A, 0x0B, 0x0C]));
assert_eq!(x3_r4.len(), 20);
let x3_r5 = x3.copy_range(18..32);
assert_eq!(x3_r5, Bits::from([0x3]));
assert_eq!(x3_r5.len(), 2);
let x3_r6 = x3.copy_range(8..0);
assert_eq!(x3_r6, Bits::empty());Sourcepub fn extend_left(&mut self, bytes: &[u8])
pub fn extend_left(&mut self, bytes: &[u8])
Adds upper bit padding to a bit string.
§Examples
let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]);
x1.extend_left(&[0x0D, 0x0E, 0x0F]);
assert_eq!(x1.len(), 44);
assert_eq!(x1, Bits::slice(&[0x0A, 0x0B, 0xDC, 0xE0, 0xF0, 0x00], 44));
let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]);
x2.extend_left(&[0x0D, 0x0E, 0x0F]);
assert_eq!(x2.len(), 48);
assert_eq!(x2, Bits::new([0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F]));Sourcepub fn extend_right(&mut self, bytes: &[u8])
pub fn extend_right(&mut self, bytes: &[u8])
Adds lower bit padding to a bit string.
§Examples
let mut x1 = Bits::empty();
x1.extend_right(&[0x0A, 0x0B, 0x0C]);
assert_eq!(x1.len(), 24);
assert_eq!(x1, Bits::new([0x0A, 0x0B, 0x0C]));
let mut x2 = Bits::from([0x0A, 0x0B, 0x0C]);
x2.extend_right(&[0x0D, 0x0E, 0x0F]);
assert_eq!(x2.len(), 44);
assert_eq!(x2, Bits::from([0x0D, 0x0E, 0x0F, 0x0A, 0x0B, 0x0C]));Sourcepub fn hamming_weight(&self) -> usize
pub fn hamming_weight(&self) -> usize
The number of ones.
§Examples
let mut x = Bits::from([15, 15, 15]);
assert_eq!(x.hamming_weight(), 12);Sourcepub fn into_bytes(self) -> IntoBytes ⓘ
pub fn into_bytes(self) -> IntoBytes ⓘ
An iterator over the individual bits
§Examples
let x = Bits::new([0xAB, 0xCD, 0xEF]);
let y = x.into_bytes().collect::<Vec<u8>>();
assert_eq!(y, vec![0xAB, 0xCD, 0xEF]);Sourcepub fn iter(&self) -> Iter<'_> ⓘ
pub fn iter(&self) -> Iter<'_> ⓘ
An iterator over the individual bits
§Examples
let x = Bits::new([0xAB, 0xCD, 0xEF]);
let mut ones = 0;
for bit in x.iter() { if bit == 1 { ones += 1; } }
assert_eq!(ones, 17);Sourcepub fn iter_from(&self, index: usize) -> Iter<'_> ⓘ
pub fn iter_from(&self, index: usize) -> Iter<'_> ⓘ
An iterator over the individual bits
§Examples
let x = Bits::new([0xAB, 0xCD, 0xEF]);
let mut ones = 0;
for bit in x.iter_from(0) { if bit == 1 { ones += 1; } }
assert_eq!(ones, 17);
ones = 0;
for bit in x.iter_from(13) { if bit == 1 { ones += 1; } }
assert_eq!(ones, 9);Sourcepub fn leading_ones(&self) -> usize
pub fn leading_ones(&self) -> usize
The number of leading ones.
§Examples
let x1 = Bits::from([0x0A, 0xFF, 0x03]);
assert_eq!(x1.leading_ones(), 10);
let x2 = Bits::new([0x0A, 0xFF, 0x0B]);
assert_eq!(x2.leading_ones(), 0);Sourcepub fn leading_zeros(&self) -> usize
pub fn leading_zeros(&self) -> usize
The number of leading zeros.
§Examples
let x1 = Bits::from([0x0A, 0x00, 0x00]);
assert_eq!(x1.leading_zeros(), 0);
let x2 = Bits::zeros(20);
assert_eq!(x2.leading_zeros(), 20);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
The number of bits.
§Examples
let x1 = Bits::empty();
assert_eq!(x1.len(), 0);
let x2 = Bits::from([0x0A, 0x0B, 0x0C]);
assert_eq!(x2.len(), 20);
let x3 = Bits::new([0x0A, 0x0B, 0x0C]);
assert_eq!(x3.len(), 24);
let x4 = Bits::slice(&[0x0A, 0x0B, 0x0C], 17);
assert_eq!(x4.len(), 17);Sourcepub fn none(&self) -> bool
pub fn none(&self) -> bool
Whether all bits are zero.
§Examples
let x1 = Bits::empty();
assert!(!x1.any());
let x2 = Bits::from([0x0A, 0x0B, 0x0C]);
assert!(x2.any());
let x3 = Bits::zeros(20);
assert!(!x3.any());Sourcepub fn or(&self, rhs: &Bits) -> Bits
pub fn or(&self, rhs: &Bits) -> Bits
Bitwise or of two bit strings.
§Examples
let x1 = Bits::from([0x20, 0x30, 0x40]);
let y1 = Bits::from([0xA0, 0xB0, 0xC0]);
assert_eq!(x1.or(&y1), Bits::from([0xA0, 0xB0, 0xC0]));
let x2 = Bits::from([0x20, 0x30, 0x40]);
let y2 = Bits::from([0x0A, 0xB0, 0xC0]);
assert_eq!(x2.len(), 23);
assert_eq!(y2.len(), 24);
let z = x2.or(&y2);
assert_eq!(z.len(), 24);Sourcepub fn or_mut(&mut self, rhs: &Bits)
pub fn or_mut(&mut self, rhs: &Bits)
Bitwise or of two bit strings.
§Examples
let mut x1 = Bits::from([0x20, 0x30, 0x40]);
let y1 = Bits::from([0xA0, 0xB0, 0xC0]);
x1.or_mut(&y1);
assert_eq!(x1, Bits::from([0xA0, 0xB0, 0xC0]));
let mut x2 = Bits::from([0x20, 0x30, 0x40]);
let y2 = Bits::from([0x0A, 0xB0, 0xC0]);
assert_eq!(x2.len(), 23);
assert_eq!(y2.len(), 24);
x2.or_mut(&y2);
assert_eq!(x2.len(), 24);Sourcepub fn pop_left(&mut self) -> u8
pub fn pop_left(&mut self) -> u8
Shifts out the most significant bit.
§Examples
let mut x1 = Bits::empty();
assert_eq!(x1.pop_left(), 0u8);
let mut x2 = Bits::from([0x0A, 0x0B, 0x0C]);
assert_eq!(x2.pop_left(), 1u8);
assert_eq!(x2.len(), 19);
let mut x3 = Bits::new([0x0A, 0x0B, 0x0C]);
assert_eq!(x3.pop_left(), 0u8);
assert_eq!(x3.len(), 23);Sourcepub fn pop_right(&mut self) -> u8
pub fn pop_right(&mut self) -> u8
Shifts out the least significant bit.
§Examples
let mut x1 = Bits::empty();
assert_eq!(x1.pop_right(), 0u8);
let mut x2 = Bits::from([0x0A, 0x0B, 0x0C]);
assert_eq!(x2.pop_right(), 0u8);
assert_eq!(x2.len(), 19);
assert_eq!(x2.pop_right(), 1u8);Sourcepub fn push_left(&mut self, bit: bool, count: usize)
pub fn push_left(&mut self, bit: bool, count: usize)
Adds upper bit padding to a bit string.
§Examples
let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]);
x1.push_left(true, 17);
assert_eq!(x1.len(), 37);
assert_eq!(x1, Bits::from([0x0A, 0x0B, 0xFC, 0xFF, 0x1F]));
let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]);
x2.push_left(false, 17);
assert_eq!(x2.len(), 41);
assert_eq!(x2, Bits::slice(&[0x0A, 0x0B, 0x0C, 0x00, 0x00, 0x00], 41));Sourcepub fn push_byte_left(&mut self, word: u8, count: usize)
pub fn push_byte_left(&mut self, word: u8, count: usize)
Adds upper bit padding to a bit string.
§Examples
let mut x = Bits::from([15, 15, 15]);
x.push_byte_left(15, 2);
assert_eq!(x.len(), 36);
assert_eq!(x, Bits::slice(&[15, 15, 255, 240, 0], 36));
x = Bits::new([15, 15, 15]);
x.push_byte_left(31, 2);
assert_eq!(x.len(), 40);
assert_eq!(x, Bits::new([15, 15, 15, 31, 31]));Sourcepub fn push_right(&mut self, bit: bool, count: usize)
pub fn push_right(&mut self, bit: bool, count: usize)
Adds lower bit padding to a bit string.
§Examples
let mut x = Bits::from([0x0A, 0x0B, 0x0C]);
x.push_right(true, 4);
assert_eq!(x.len(), 24);
assert_eq!(x, Bits::from([0xAF, 0xB0, 0xC0]));Sourcepub fn push_byte_right(&mut self, word: u8, count: usize)
pub fn push_byte_right(&mut self, word: u8, count: usize)
Adds lower bit padding to a bit string.
§Examples
let mut x = Bits::from([15, 15, 15]);
x.push_byte_right(31, 2);
assert_eq!(x.len(), 36);
assert_eq!(x, Bits::from([31, 31, 15, 15, 15]));Sourcepub fn reset(&mut self, i: usize)
pub fn reset(&mut self, i: usize)
Sets a bit to zero.
§Examples
let mut x = Bits::new([15]);
assert_eq!(x.i(3), 1u8);
x.reset(3);
assert_eq!(x.i(3), 0u8);Sourcepub fn reset_bits(&mut self, start: usize, count: usize)
pub fn reset_bits(&mut self, start: usize, count: usize)
Sets the state of a range of bits to zero.
§Examples
let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]);
x1.reset_bits(7, 10);
assert_eq!(x1, Bits::from([0x0A, 0x00, 0x0C]));
let mut x2 = Bits::from([0x0A, 0x0B, 0x0C]);
x2.reset_bits(8, 12);
assert_eq!(x2, Bits::slice(&[0x0A, 0x00, 0x00], 20));let mut x = Bits::from([0x0A, 0x0B, 0x0C]);
x.reset_bits(21, 14);let mut x = Bits::from([0x0A, 0x0B, 0x0C]);
x.reset_bits(7, 14);Sourcepub fn reset_byte(&mut self, i: usize)
pub fn reset_byte(&mut self, i: usize)
Sourcepub fn reset_bytes(&mut self, start: usize, count: usize)
pub fn reset_bytes(&mut self, start: usize, count: usize)
Sets the state of a range of bytes to zero.
§Examples
let mut x = Bits::from([0x0A, 0x0B, 0x0C]);
x.reset_bytes(1, 2);
assert_eq!(x, Bits::slice(&[0x0A, 0x00, 0x00], 20));let mut x = Bits::from([0x0A, 0x0B, 0x0C]);
x.reset_bytes(3, 2);let mut x = Bits::from([0x0A, 0x0B, 0x0C]);
x.reset_bytes(0, 4);Sourcepub fn reverse(&mut self)
pub fn reverse(&mut self)
Reverses the order of the bits.
§Examples
let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]);
x1.reverse();
assert_eq!(x1, Bits::slice(&[0x03, 0x0D, 0x05], 20));
let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]);
x2.reverse();
assert_eq!(x2, Bits::new([0x30, 0xD0, 0x50]));Sourcepub fn reversed(&self) -> Self
pub fn reversed(&self) -> Self
Reverses the order of the bits.
§Examples
let x1 = Bits::from([0x0A, 0x0B, 0x0C]);
assert_eq!(x1.reversed(), Bits::slice(&[0x03, 0x0D, 0x05], 20));
let x2 = Bits::new([0x0A, 0x0B, 0x0C]);
assert_eq!(x2.reversed(), Bits::new([0x30, 0xD0, 0x50]));Sourcepub fn rotate_left(&mut self, count: usize)
pub fn rotate_left(&mut self, count: usize)
Rotates bits to the left.
§Examples
let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]);
x1.rotate_left(4);
assert_eq!(x1, Bits::slice(&[0xAC, 0xB0, 0x00], 20));
assert_eq!(x1.len(), 20);
let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]);
x2.rotate_left(4);
assert_eq!(x2, Bits::new([0xA0, 0xB0, 0xC0]));
assert_eq!(x2.len(), 24);Sourcepub fn rotated_left(&self, count: usize) -> Self
pub fn rotated_left(&self, count: usize) -> Self
Rotates bits to the left.
§Examples
let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]);
assert_eq!(x1.rotated_left(4), Bits::slice(&[0xAC, 0xB0, 0x00], 20));
assert_eq!(x1.len(), 20);
let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]);
assert_eq!(x2.rotated_left(4), Bits::new([0xA0, 0xB0, 0xC0]));
assert_eq!(x2.len(), 24);Sourcepub fn rotate_right(&mut self, count: usize)
pub fn rotate_right(&mut self, count: usize)
Rotates bits to the right.
§Examples
let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]);
x1.rotate_right(4);
assert_eq!(x1, Bits::slice(&[0xB0, 0xC0, 0x0A], 20));
assert_eq!(x1.len(), 20);
let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]);
x2.rotate_right(4);
assert_eq!(x2, Bits::new([0xB0, 0xC0, 0xA0]));
assert_eq!(x2.len(), 24);Sourcepub fn rotated_right(&self, count: usize) -> Self
pub fn rotated_right(&self, count: usize) -> Self
Rotates bits to the right.
§Examples
let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]);
assert_eq!(x1.rotated_right(4), Bits::slice(&[0xB0, 0xC0, 0x0A], 20));
assert_eq!(x1.len(), 20);
let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]);
assert_eq!(x2.rotated_right(4), Bits::new([0xB0, 0xC0, 0xA0]));
assert_eq!(x2.len(), 24);Sourcepub fn set(&mut self, i: usize)
pub fn set(&mut self, i: usize)
Sets a bit to one.
§Examples
let mut x = Bits::new([15]);
assert_eq!(x.i(4), 0u8);
x.set(4);
assert_eq!(x.i(4), 1u8);Sourcepub fn set_bits(&mut self, start: usize, count: usize)
pub fn set_bits(&mut self, start: usize, count: usize)
Sets the state of a range of bits to one.
§Examples
let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]);
x1.set_bits(7, 10);
assert_eq!(x1, Bits::from([0x8A, 0xFF, 0x0D]));
let mut x2 = Bits::from([0x0A, 0x0B, 0x0C]);
x2.set_bits(8, 12);
assert_eq!(x2, Bits::slice(&[0x0A, 0xFF, 0x0F], 20));let mut x = Bits::from([0x0A, 0x0B, 0x0C]);
x.set_bits(21, 14);let mut x = Bits::from([0x0A, 0x0B, 0x0C]);
x.set_bits(7, 14);Sourcepub fn set_bytes(&mut self, start: usize, count: usize)
pub fn set_bytes(&mut self, start: usize, count: usize)
Sets the state of a range of bytes to all ones.
§Examples
let mut x = Bits::from([0x0A, 0x0B, 0x0C]);
x.set_bytes(1, 2);
assert_eq!(x, Bits::slice(&[0x0A, 0xFF, 0x0F], 20));let mut x = Bits::from([0x0A, 0x0B, 0x0C]);
x.set_bytes(3, 2);let mut x = Bits::from([0x0A, 0x0B, 0x0C]);
x.set_bytes(0, 4);Sourcepub fn shift_left(&mut self, count: usize)
pub fn shift_left(&mut self, count: usize)
Shifts out upper bits, shifting in zeros on the lower end.
§Examples
let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]);
x1.shift_left(17);
assert_eq!(x1, Bits::slice(&[0x00, 0x00, 0x04], 20));
assert_eq!(x1.len(), 20);
let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]);
x2.shift_left(4);
assert_eq!(x2, Bits::new([0xA0, 0xB0, 0xC0]));
assert_eq!(x2.len(), 24);Sourcepub fn shift_left_with(&mut self, count: usize, bit: bool)
pub fn shift_left_with(&mut self, count: usize, bit: bool)
Shifts out upper bits, shifting in zeros or ones on the lower end.
§Examples
let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]);
x1.shift_left_with(17, false);
assert_eq!(x1, Bits::slice(&[0x00, 0x00, 0x04], 20));
assert_eq!(x1.len(), 20);
let mut x2 = Bits::from([0x0A, 0x0B, 0x0C]);
// 0101 11111111 11111111
//
x2.shift_left_with(17, true);
assert_eq!(x2, Bits::slice(&[0xFF, 0xFF, 0x05], 20));
assert_eq!(x2.len(), 20);Sourcepub fn shifted_left(&self, count: usize) -> Self
pub fn shifted_left(&self, count: usize) -> Self
Shifts out upper bits, shifting in zeros on the lower end.
§Examples
let x1 = Bits::from([0x0A, 0x0B, 0x0C]);
let s1 = x1.shifted_left(17);
assert_eq!(s1, Bits::slice(&[0x00, 0x00, 0x04], 20));
assert_eq!(s1.len(), 20);
let x2 = Bits::new([0x0A, 0x0B, 0x0C]);
let s2 = x2.shifted_left(4);
assert_eq!(s2, Bits::new([0xA0, 0xB0, 0xC0]));
assert_eq!(s2.len(), 24);Sourcepub fn shift_right(&mut self, count: usize)
pub fn shift_right(&mut self, count: usize)
Shifts out lower bits, shifting zeros into the upper bits.
§Examples
let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]);
x1.shift_right(17);
assert_eq!(x1.len(), 20);
assert_eq!(x1, Bits::slice(&[0x06, 0x00, 0x00], 20));
let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]);
x2.shift_right(4);
assert_eq!(x2.len(), 24);
assert_eq!(x2, Bits::new([0xB0, 0xC0, 0x00]));Sourcepub fn shift_right_with(&mut self, count: usize, bit: bool)
pub fn shift_right_with(&mut self, count: usize, bit: bool)
Shifts out lower bits, shifting zeros or ones into the upper bits.
§Examples
let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]);
x1.shift_right_with(17, false);
assert_eq!(x1.len(), 20);
assert_eq!(x1, Bits::slice(&[0x06, 0x00, 0x00], 20));
let mut x2 = Bits::from([0x0A, 0x0B, 0x0C]);
x2.shift_right_with(17, true);
assert_eq!(x2.len(), 20);
assert_eq!(x2, Bits::slice(&[0xFE, 0xFF, 0xFF], 20));Sourcepub fn shifted_right(&self, count: usize) -> Self
pub fn shifted_right(&self, count: usize) -> Self
Shifts out lower bits, shifting zeros into the upper bits.
§Examples
let x1 = Bits::from([0x0A, 0x0B, 0x0C]);
let s1 = x1.shifted_right(17);
assert_eq!(s1.len(), 20);
assert_eq!(s1, Bits::slice(&[0x06, 0x00, 0x00], 20));
let x2 = Bits::new([0x0A, 0x0B, 0x0C]);
let s2 = x2.shifted_right(4);
assert_eq!(s2.len(), 24);
assert_eq!(s2, Bits::new([0xB0, 0xC0, 0x00]));Sourcepub fn sticky_shift_left(&mut self, count: usize)
pub fn sticky_shift_left(&mut self, count: usize)
Shifts out upper bits, shifting in zeros or ones on the lower end based on the lowest bit.
§Examples
let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]);
x1.sticky_shift_left(17);
assert_eq!(x1, Bits::slice(&[0x00, 0x00, 0x04], 20));
assert_eq!(x1.len(), 20);
let mut x2 = Bits::from([0x09, 0x0B, 0x0C]);
x2.sticky_shift_left(17);
assert_eq!(x2, Bits::slice(&[0xFF, 0xFF, 0x03], 20));
assert_eq!(x2.len(), 20);Sourcepub fn sticky_shift_right(&mut self, count: usize)
pub fn sticky_shift_right(&mut self, count: usize)
Shifts out lower bits, shifting zeros or ones into the upper bits based on the highest bit.
§Examples
let mut x1 = Bits::slice(&[0x0A, 0x0B, 0x04], 20);
x1.sticky_shift_right(17);
assert_eq!(x1.len(), 20);
assert_eq!(x1, Bits::slice(&[0x02, 0x00, 0x00], 20));
let mut x2 = Bits::from([0x0A, 0x0B, 0x0C]);
x2.sticky_shift_right(17);
assert_eq!(x2.len(), 20);
assert_eq!(x2, Bits::slice(&[0xFE, 0xFF, 0xFF], 20));Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
The number of bytes.
§Examples
let x1 = Bits::empty();
assert_eq!(x1.size(), 0);
let x2 = Bits::from([0x0A, 0x0B, 0x0C]);
assert_eq!(x2.size(), 3);
let x3 = Bits::new([0x0A, 0x0B, 0x0C]);
assert_eq!(x3.size(), 3);
let x4 = Bits::slice(&[0x0A, 0x0B, 0x0C], 13);
assert_eq!(x4.size(), 2);Sourcepub fn split(&self, i: usize) -> (Bits, Bits)
pub fn split(&self, i: usize) -> (Bits, Bits)
Splits the bit string.
When the index is greater than or equal to the length
of the bit string, the second element of the returned
pair will be empty. When the index is 0, the first
element will be empty.
§Examples
let x1 = Bits::empty();
let (l1, r1) = x1.split(11);
assert_eq!(l1.len(), 0);
assert_eq!(r1.len(), 0);
let x2 = Bits::from([0x0A, 0x0B, 0x0C]);
let (l2, r2) = x2.split(20);
assert_eq!(l2, x2);
assert_eq!(r2.len(), 0);
let (l3, r3) = x2.split(0);
assert_eq!(l3.len(), 0);
assert_eq!(r3, x2);
let (l4, r4) = x2.split(11);
assert_eq!(l4, Bits::slice(&[0x0A, 0x03], 11));
assert_eq!(r4, Bits::slice(&[0x81, 0x01], 9));Sourcepub fn test(&self, i: usize) -> bool
pub fn test(&self, i: usize) -> bool
Whether a bit is one.
§Examples
let mut x = Bits::from([0x0F]);
assert!(x.test(3));
assert!(!x.test(4));
assert!(!x.test(8));Sourcepub fn toggle_bits(&mut self, start: usize, count: usize)
pub fn toggle_bits(&mut self, start: usize, count: usize)
Flips the state of a range of bits.
§Examples
let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]);
x1.toggle_bits(7, 10);
assert_eq!(x1, Bits::from([0x8A, 0xF4, 0x0D]));
let mut x2 = Bits::from([0x0A, 0x0B, 0x0C]);
x2.toggle_bits(8, 12);
assert_eq!(x2, Bits::slice(&[0x0A, 0xF4, 0x03], 20));let mut x = Bits::from([0x0A, 0x0B, 0x0C]);
x.toggle_bits(21, 14);let mut x = Bits::from([0x0A, 0x0B, 0x0C]);
x.toggle_bits(7, 14);Sourcepub fn toggle_byte(&mut self, i: usize)
pub fn toggle_byte(&mut self, i: usize)
Sourcepub fn toggle_bytes(&mut self, start: usize, count: usize)
pub fn toggle_bytes(&mut self, start: usize, count: usize)
Sourcepub fn trailing_ones(&self) -> usize
pub fn trailing_ones(&self) -> usize
The number of trailing ones.
§Examples
let x = Bits::from([0xFF, 0x0B, 0x0A]);
assert_eq!(x.trailing_ones(), 10);Sourcepub fn trailing_zeros(&self) -> usize
pub fn trailing_zeros(&self) -> usize
The number of trailing zeros.
§Examples
let x1 = Bits::from([0x00, 0x0A, 0x0B]);
assert_eq!(x1.trailing_zeros(), 9);
let x2 = Bits::zeros(20);
assert_eq!(x2.trailing_zeros(), 20);Sourcepub fn trim_end(&mut self, bit: bool)
pub fn trim_end(&mut self, bit: bool)
Trims leading bits.
§Examples
let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]);
x1.trim_end(false);
assert_eq!(x1.len(), 20);
assert_eq!(x1, Bits::from([0x0A, 0x0B, 0x0C]));
x1.trim_end(true);
assert_eq!(x1.len(), 18);
assert_eq!(x1, Bits::slice(&[0x0A, 0x0B, 0x00], 18));
let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]);
x2.trim_end(true);
assert_eq!(x2.len(), 24);
assert_eq!(x2, Bits::new([0x0A, 0x0B, 0x0C]));
x2.trim_end(false);
assert_eq!(x2.len(), 20);
assert_eq!(x2, Bits::from([0x0A, 0x0B, 0x0C]));
let mut x3 = Bits::slice(&[0x0A, 0x0B, 0x00], 18);
x3.trim_end(false);
assert_eq!(x3.len(), 12);
assert_eq!(x3, Bits::from([0x0A, 0x0B]));
let mut x4 = Bits::slice(&[0x0A, 0xFB, 0x03], 18);
x4.trim_end(true);
assert_eq!(x4.len(), 11);
assert_eq!(x4, Bits::slice(&[0x0A, 0x0B], 11));Sourcepub fn trim_start(&mut self, bit: bool)
pub fn trim_start(&mut self, bit: bool)
Trims trailing bits.
§Examples
let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]);
x1.trim_start(true);
assert_eq!(x1.len(), 20);
assert_eq!(x1, Bits::from([0x0A, 0x0B, 0x0C]));
x1.trim_start(false);
assert_eq!(x1.len(), 19);
assert_eq!(x1, Bits::from([0x85, 0x05, 0x06]));
let mut x2 = Bits::new([0x00, 0x0A, 0x0B]);
x2.trim_start(false);
assert_eq!(x2.len(), 15);
assert_eq!(x2, Bits::slice(&[0x85, 0x05], 15));
let mut x3 = Bits::new([0xFF, 0x0B, 0x0C]);
x3.trim_start(true);
assert_eq!(x3.len(), 14);
assert_eq!(x3, Bits::slice(&[0x02, 0x03], 14));Sourcepub fn xor(&self, rhs: &Bits) -> Bits
pub fn xor(&self, rhs: &Bits) -> Bits
Bitwise exclusive or of two bit strings.
§Examples
let x1 = Bits::from([0x20, 0x30, 0x40]);
let y1 = Bits::from([0xA0, 0xB0, 0xC0]);
assert_eq!(x1.xor(&y1), Bits::from([0x80, 0x80, 0x80]));
let x2 = Bits::from([0x20, 0x30, 0x40]);
let y2 = Bits::from([0x0A, 0xB0, 0xC0]);
assert_eq!(x2.len(), 23);
assert_eq!(y2.len(), 24);
let z = x2.xor(&y2);
assert_eq!(z.len(), 24);Sourcepub fn xor_mut(&mut self, rhs: &Bits)
pub fn xor_mut(&mut self, rhs: &Bits)
Bitwise exclusive or of two bit strings.
§Examples
let mut x1 = Bits::from([0x20, 0x30, 0x40]);
let y1 = Bits::from([0xA0, 0xB0, 0xC0]);
x1.xor_mut(&y1);
assert_eq!(x1, Bits::from([0x80, 0x80, 0x80]));
let mut x2 = Bits::from([0x20, 0x30, 0x40]);
let y2 = Bits::from([0x0A, 0xB0, 0xC0]);
assert_eq!(x2.len(), 23);
assert_eq!(y2.len(), 24);
x2.xor_mut(&y2);
assert_eq!(x2.len(), 24);Trait Implementations§
Source§impl BitAnd<&Bits> for &Bits
impl BitAnd<&Bits> for &Bits
Source§fn bitand(self, rhs: &Bits) -> Self::Output
fn bitand(self, rhs: &Bits) -> Self::Output
Bitwise and of two bit strings.
§Examples
let x1 = Bits::from([0x20, 0x30, 0x40]);
let y1 = Bits::from([0xA0, 0xB0, 0xC0]);
assert_eq!(x1 & &y1, Bits::new([0x20, 0x30, 0x40]));
let x2 = Bits::from([0x20, 0x30, 0x40]);
let y2 = Bits::from([0x0A, 0xB0, 0xC0]);
assert_eq!(x2.len(), 23);
assert_eq!(y2.len(), 24);
let z = x2 & &y2;
assert_eq!(z.len(), 24);Source§impl BitAnd<&Bits> for Bits
impl BitAnd<&Bits> for Bits
Source§fn bitand(self, rhs: &Bits) -> Self::Output
fn bitand(self, rhs: &Bits) -> Self::Output
Bitwise and of two bit strings.
§Examples
let x1 = Bits::from([0x20, 0x30, 0x40]);
let y1 = Bits::from([0xA0, 0xB0, 0xC0]);
assert_eq!(x1 & &y1, Bits::new([0x20, 0x30, 0x40]));
let x2 = Bits::from([0x20, 0x30, 0x40]);
let y2 = Bits::from([0x0A, 0xB0, 0xC0]);
assert_eq!(x2.len(), 23);
assert_eq!(y2.len(), 24);
let z = x2 & &y2;
assert_eq!(z.len(), 24);Source§impl<'a> BitAnd<Bits> for &'a Bits
impl<'a> BitAnd<Bits> for &'a Bits
Source§fn bitand(self, rhs: Bits) -> Self::Output
fn bitand(self, rhs: Bits) -> Self::Output
Bitwise and of two bit strings.
§Examples
let x1 = Bits::from([0x20, 0x30, 0x40]);
let y1 = Bits::from([0xA0, 0xB0, 0xC0]);
assert_eq!(&x1 & y1, Bits::new([0x20, 0x30, 0x40]));
let x2 = Bits::from([0x20, 0x30, 0x40]);
let y2 = Bits::from([0x0A, 0xB0, 0xC0]);
assert_eq!(x2.len(), 23);
assert_eq!(y2.len(), 24);
let z = &x2 & y2;
assert_eq!(z.len(), 24);Source§impl BitAnd for Bits
impl BitAnd for Bits
Source§fn bitand(self, rhs: Self) -> Self::Output
fn bitand(self, rhs: Self) -> Self::Output
Bitwise and of two bit strings.
§Examples
let x1 = Bits::from([0x20, 0x30, 0x40]);
let y1 = Bits::from([0xA0, 0xB0, 0xC0]);
assert_eq!(x1 & y1, Bits::new([0x20, 0x30, 0x40]));
let x2 = Bits::from([0x20, 0x30, 0x40]);
let y2 = Bits::from([0x0A, 0xB0, 0xC0]);
assert_eq!(x2.len(), 23);
assert_eq!(y2.len(), 24);
let z = x2 & y2;
assert_eq!(z.len(), 24);Source§impl BitAndAssign<&Bits> for Bits
impl BitAndAssign<&Bits> for Bits
Source§fn bitand_assign(&mut self, rhs: &Bits)
fn bitand_assign(&mut self, rhs: &Bits)
Bitwise and of two bit strings.
§Examples
let mut x1 = Bits::from([0x20, 0x30, 0x40]);
let y1 = Bits::from([0xA0, 0xB0, 0xC0]);
x1 &= &y1;
assert_eq!(x1, Bits::new([0x20, 0x30, 0x40]));
let mut x2 = Bits::from([0x20, 0x30, 0x40]);
let y2 = Bits::from([0x0A, 0xB0, 0xC0]);
assert_eq!(x2.len(), 23);
assert_eq!(y2.len(), 24);
x2 &= &y2;
assert_eq!(x2.len(), 24);Source§impl BitAndAssign for Bits
impl BitAndAssign for Bits
Source§fn bitand_assign(&mut self, rhs: Self)
fn bitand_assign(&mut self, rhs: Self)
Bitwise and of two bit strings.
§Examples
let mut x1 = Bits::from([0x20, 0x30, 0x40]);
let y1 = Bits::from([0xA0, 0xB0, 0xC0]);
x1 &= y1;
assert_eq!(x1, Bits::new([0x20, 0x30, 0x40]));
let mut x2 = Bits::from([0x20, 0x30, 0x40]);
let y2 = Bits::from([0x0A, 0xB0, 0xC0]);
assert_eq!(x2.len(), 23);
assert_eq!(y2.len(), 24);
x2 &= y2;
assert_eq!(x2.len(), 24);Source§impl BitOr<&Bits> for &Bits
impl BitOr<&Bits> for &Bits
Source§fn bitor(self, rhs: &Bits) -> Self::Output
fn bitor(self, rhs: &Bits) -> Self::Output
Bitwise or of two bit strings.
§Examples
let x1 = Bits::from([0x20, 0x30, 0x40]);
let y1 = Bits::from([0xA0, 0xB0, 0xC0]);
assert_eq!(x1 | &y1, Bits::from([0xA0, 0xB0, 0xC0]));
let x2 = Bits::from([0x20, 0x30, 0x40]);
let y2 = Bits::from([0x0A, 0xB0, 0xC0]);
assert_eq!(x2.len(), 23);
assert_eq!(y2.len(), 24);
let z = x2 | &y2;
assert_eq!(z.len(), 24);Source§impl BitOr<&Bits> for Bits
impl BitOr<&Bits> for Bits
Source§fn bitor(self, rhs: &Bits) -> Self::Output
fn bitor(self, rhs: &Bits) -> Self::Output
Bitwise or of two bit strings.
§Examples
let x1 = Bits::from([0x20, 0x30, 0x40]);
let y1 = Bits::from([0xA0, 0xB0, 0xC0]);
assert_eq!(x1 | &y1, Bits::from([0xA0, 0xB0, 0xC0]));
let x2 = Bits::from([0x20, 0x30, 0x40]);
let y2 = Bits::from([0x0A, 0xB0, 0xC0]);
assert_eq!(x2.len(), 23);
assert_eq!(y2.len(), 24);
let z = x2 | &y2;
assert_eq!(z.len(), 24);Source§impl<'a> BitOr<Bits> for &'a Bits
impl<'a> BitOr<Bits> for &'a Bits
Source§fn bitor(self, rhs: Bits) -> Self::Output
fn bitor(self, rhs: Bits) -> Self::Output
Bitwise or of two bit strings.
§Examples
let x1 = Bits::from([0x20, 0x30, 0x40]);
let y1 = Bits::from([0xA0, 0xB0, 0xC0]);
assert_eq!(&x1 | y1, Bits::from([0xA0, 0xB0, 0xC0]));
let x2 = Bits::from([0x20, 0x30, 0x40]);
let y2 = Bits::from([0x0A, 0xB0, 0xC0]);
assert_eq!(x2.len(), 23);
assert_eq!(y2.len(), 24);
let z = &x2 | y2;
assert_eq!(z.len(), 24);Source§impl BitOr for Bits
impl BitOr for Bits
Source§fn bitor(self, rhs: Self) -> Self::Output
fn bitor(self, rhs: Self) -> Self::Output
Bitwise or of two bit strings.
§Examples
let x1 = Bits::from([0x20, 0x30, 0x40]);
let y1 = Bits::from([0xA0, 0xB0, 0xC0]);
assert_eq!(x1 | y1, Bits::from([0xA0, 0xB0, 0xC0]));
let x2 = Bits::from([0x20, 0x30, 0x40]);
let y2 = Bits::from([0x0A, 0xB0, 0xC0]);
assert_eq!(x2.len(), 23);
assert_eq!(y2.len(), 24);
let z = x2 | y2;
assert_eq!(z.len(), 24);Source§impl BitOrAssign<&Bits> for Bits
impl BitOrAssign<&Bits> for Bits
Source§fn bitor_assign(&mut self, rhs: &Bits)
fn bitor_assign(&mut self, rhs: &Bits)
Bitwise or of two bit strings.
§Examples
let mut x1 = Bits::from([0x20, 0x30, 0x40]);
let y1 = Bits::from([0xA0, 0xB0, 0xC0]);
x1 |= &y1;
assert_eq!(x1, Bits::from([0xA0, 0xB0, 0xC0]));
let mut x2 = Bits::from([0x20, 0x30, 0x40]);
let y2 = Bits::from([0x0A, 0xB0, 0xC0]);
assert_eq!(x2.len(), 23);
assert_eq!(y2.len(), 24);
x2 |= &y2;
assert_eq!(x2.len(), 24);Source§impl BitOrAssign for Bits
impl BitOrAssign for Bits
Source§fn bitor_assign(&mut self, rhs: Self)
fn bitor_assign(&mut self, rhs: Self)
Bitwise or of two bit strings.
§Examples
let mut x1 = Bits::from([0x20, 0x30, 0x40]);
let y1 = Bits::from([0xA0, 0xB0, 0xC0]);
x1 |= y1;
assert_eq!(x1, Bits::from([0xA0, 0xB0, 0xC0]));
let mut x2 = Bits::from([0x20, 0x30, 0x40]);
let y2 = Bits::from([0x0A, 0xB0, 0xC0]);
assert_eq!(x2.len(), 23);
assert_eq!(y2.len(), 24);
x2 |= y2;
assert_eq!(x2.len(), 24);Source§impl BitXor<&Bits> for &Bits
impl BitXor<&Bits> for &Bits
Source§fn bitxor(self, rhs: &Bits) -> Self::Output
fn bitxor(self, rhs: &Bits) -> Self::Output
Bitwise exclusive or of two bit strings.
§Examples
let x1 = Bits::from([0x20, 0x30, 0x40]);
let y1 = Bits::from([0xA0, 0xB0, 0xC0]);
assert_eq!(x1 ^ &y1, Bits::from([0x80, 0x80, 0x80]));
let x2 = Bits::from([0x20, 0x30, 0x40]);
let y2 = Bits::from([0x0A, 0xB0, 0xC0]);
assert_eq!(x2.len(), 23);
assert_eq!(y2.len(), 24);
let z = x2 ^ &y2;
assert_eq!(z.len(), 24);Source§impl BitXor<&Bits> for Bits
impl BitXor<&Bits> for Bits
Source§fn bitxor(self, rhs: &Bits) -> Self::Output
fn bitxor(self, rhs: &Bits) -> Self::Output
Bitwise exclusive or of two bit strings.
§Examples
let x1 = Bits::from([0x20, 0x30, 0x40]);
let y1 = Bits::from([0xA0, 0xB0, 0xC0]);
assert_eq!(x1 ^ &y1, Bits::from([0x80, 0x80, 0x80]));
let x2 = Bits::from([0x20, 0x30, 0x40]);
let y2 = Bits::from([0x0A, 0xB0, 0xC0]);
assert_eq!(x2.len(), 23);
assert_eq!(y2.len(), 24);
let z = x2 ^ &y2;
assert_eq!(z.len(), 24);Source§impl<'a> BitXor<Bits> for &'a Bits
impl<'a> BitXor<Bits> for &'a Bits
Source§fn bitxor(self, rhs: Bits) -> Self::Output
fn bitxor(self, rhs: Bits) -> Self::Output
Bitwise exclusive or of two bit strings.
§Examples
let x1 = Bits::from([0x20, 0x30, 0x40]);
let y1 = Bits::from([0xA0, 0xB0, 0xC0]);
assert_eq!(&x1 ^ y1, Bits::from([0x80, 0x80, 0x80]));
let x2 = Bits::from([0x20, 0x30, 0x40]);
let y2 = Bits::from([0x0A, 0xB0, 0xC0]);
assert_eq!(x2.len(), 23);
assert_eq!(y2.len(), 24);
let z = &x2 ^ y2;
assert_eq!(z.len(), 24);Source§impl BitXor for Bits
impl BitXor for Bits
Source§fn bitxor(self, rhs: Self) -> Self::Output
fn bitxor(self, rhs: Self) -> Self::Output
Bitwise exclusive or of two bit strings.
§Examples
let x1 = Bits::from([0x20, 0x30, 0x40]);
let y1 = Bits::from([0xA0, 0xB0, 0xC0]);
assert_eq!(x1 ^ y1, Bits::from([0x80, 0x80, 0x80]));
let x2 = Bits::from([0x20, 0x30, 0x40]);
let y2 = Bits::from([0x0A, 0xB0, 0xC0]);
assert_eq!(x2.len(), 23);
assert_eq!(y2.len(), 24);
let z = x2 ^ y2;
assert_eq!(z.len(), 24);Source§impl BitXorAssign<&Bits> for Bits
impl BitXorAssign<&Bits> for Bits
Source§fn bitxor_assign(&mut self, rhs: &Bits)
fn bitxor_assign(&mut self, rhs: &Bits)
Bitwise exclusive or of two bit strings.
§Examples
let mut x1 = Bits::from([0x20, 0x30, 0x40]);
let y1 = Bits::from([0xA0, 0xB0, 0xC0]);
x1 ^= &y1;
assert_eq!(x1, Bits::from([0x80, 0x80, 0x80]));
let mut x2 = Bits::from([0x20, 0x30, 0x40]);
let y2 = Bits::from([0x0A, 0xB0, 0xC0]);
assert_eq!(x2.len(), 23);
assert_eq!(y2.len(), 24);
x2 ^= &y2;
assert_eq!(x2.len(), 24);Source§impl BitXorAssign for Bits
impl BitXorAssign for Bits
Source§fn bitxor_assign(&mut self, rhs: Self)
fn bitxor_assign(&mut self, rhs: Self)
Bitwise exclusive or of two bit strings.
§Examples
let mut x1 = Bits::from([0x20, 0x30, 0x40]);
let y1 = Bits::from([0xA0, 0xB0, 0xC0]);
x1 ^= y1;
assert_eq!(x1, Bits::from([0x80, 0x80, 0x80]));
let mut x2 = Bits::from([0x20, 0x30, 0x40]);
let y2 = Bits::from([0x0A, 0xB0, 0xC0]);
assert_eq!(x2.len(), 23);
assert_eq!(y2.len(), 24);
x2 ^= y2;
assert_eq!(x2.len(), 24);Source§impl Div<usize> for &Bits
impl Div<usize> for &Bits
Source§fn div(self, index: usize) -> Self::Output
fn div(self, index: usize) -> Self::Output
Splits the bit string.
When the index is greater than or equal to the length
of the bit string, the second element of the returned
pair will be empty. When the index is 0, the first
element will be empty.
§Examples
let x1 = Bits::empty();
let (l1, r1) = &x1 / 11;
assert_eq!(l1.len(), 0);
assert_eq!(r1.len(), 0);
let x2 = Bits::from([0x0A, 0x0B, 0x0C]);
let (l2, r2) = &x2 / 20;
assert_eq!(l2, x2);
assert_eq!(r2.len(), 0);
let (l3, r3) = &x2 / 0;
assert_eq!(l3.len(), 0);
assert_eq!(r3, x2);
let (l4, r4) = &x2 / 11;
assert_eq!(l4, Bits::slice(&[0x0A, 0x03], 11));
assert_eq!(r4, Bits::slice(&[0x81, 0x01], 9));Source§impl Div<usize> for Bits
impl Div<usize> for Bits
Source§fn div(self, index: usize) -> Self::Output
fn div(self, index: usize) -> Self::Output
Splits the bit string.
When the index is greater than or equal to the length
of the bit string, the second element of the returned
pair will be empty. When the index is 0, the first
element will be empty.
§Examples
let x1 = Bits::empty();
let (l1, r1) = x1 / 11;
assert_eq!(l1.len(), 0);
assert_eq!(r1.len(), 0);
let x2 = Bits::from([0x0A, 0x0B, 0x0C]);
let (l2, r2) = x2 / 11;
assert_eq!(l2, Bits::slice(&[0x0A, 0x03], 11));
assert_eq!(r2, Bits::slice(&[0x81, 0x01], 9));Source§impl From<&[bool]> for Bits
impl From<&[bool]> for Bits
Source§fn from(bits: &[bool]) -> Self
fn from(bits: &[bool]) -> Self
Creates a bit string from booleans.
§Examples
let bits1 = vec![true, false, true, true, false, true, true];
let x1 = Bits::from(bits1.as_slice());
assert_eq!(x1.len(), 7);
assert_eq!(x1, Bits::from([0x6D]));
let bits2 = vec![true, false, true, false, false, false, false, false, false];
let x2 = Bits::from(bits2.as_slice());
assert_eq!(x2.len(), 9);
assert_eq!(x2, Bits::slice(&[0x05, 0x00], 9));Source§impl<const N: usize> From<[bool; N]> for Bits
impl<const N: usize> From<[bool; N]> for Bits
Source§fn from(bits: [bool; N]) -> Self
fn from(bits: [bool; N]) -> Self
Creates a bit string from booleans.
§Examples
let x1 = Bits::from([true, false, true, true, false, true, true]);
assert_eq!(x1.len(), 7);
assert_eq!(x1, Bits::from([0x6D]));
let x2 = Bits::from([true, false, true, false, false, false, false, false, false]);
assert_eq!(x2.len(), 9);
assert_eq!(x2, Bits::slice(&[0x05, 0x00], 9));Source§impl From<Bits> for Vec<bool>
impl From<Bits> for Vec<bool>
Source§fn from(bits: Bits) -> Self
fn from(bits: Bits) -> Self
Converts a bit string to a vector of booleans
§Example
let bits = Bits::new([0x10, 0x32]);
let bools = Vec::<bool>::from(bits);
assert_eq!(bools.len(), 16);
assert_eq!(
bools,
vec![
false, false, false, false, true, false, false, false,
false, true, false, false, true, true, false, false
]
);Source§impl From<Bits> for Vec<f32>
impl From<Bits> for Vec<f32>
Source§fn from(bits: Bits) -> Self
fn from(bits: Bits) -> Self
Converts a bit string to a vector of 32 bit floats
§Example
let bits = Bits::new([0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE]);
let floats = Vec::<f32>::from(bits);
assert_eq!(floats.len(), 2);
assert_eq!(floats, vec![f32::from_bits(0x76543210), f32::from_bits(0xFEDCBA98)]);
let bits = Bits::new([0x10, 0x32, 0x54]);
let floats = Vec::<f32>::from(bits);
assert_eq!(floats.len(), 1);
assert_eq!(floats, vec![f32::from_bits(0x00543210)]);Source§impl From<Bits> for Vec<f64>
impl From<Bits> for Vec<f64>
Source§fn from(bits: Bits) -> Self
fn from(bits: Bits) -> Self
Converts a bit string to a vector of 64 bit floats
§Example
let bits = Bits::new([0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE]);
let floats = Vec::<f64>::from(bits);
assert_eq!(floats.len(), 1);
assert_eq!(floats, vec![f64::from_bits(0xFEDCBA9876543210)]);
let bits = Bits::new([0x10, 0x32, 0x54]);
let floats = Vec::<f64>::from(bits);
assert_eq!(floats.len(), 1);
assert_eq!(floats, vec![f64::from_bits(0x0000000000543210)]);Source§impl From<Bits> for Vec<i16>
impl From<Bits> for Vec<i16>
Source§fn from(bits: Bits) -> Self
fn from(bits: Bits) -> Self
Converts a bit string to a vector of 16 bit signed integers.
If the length of the the bit string is not divisible by 16, it is sign-extended to the next length that is, then it is converted.
§Example
let bits = Bits::new([0x10, 0xFE]);
let ishorts = Vec::<i16>::from(bits);
assert_eq!(ishorts.len(), 1);
assert_eq!(ishorts, vec![0xFE10u16 as i16]);
let bits = Bits::from([0x10, 0x5E]);
let ishorts = Vec::<i16>::from(bits);
assert_eq!(ishorts.len(), 1);
assert_eq!(ishorts, vec![0xDE10u16 as i16]);Source§impl From<Bits> for Vec<i32>
impl From<Bits> for Vec<i32>
Source§fn from(bits: Bits) -> Self
fn from(bits: Bits) -> Self
Converts a bit string to a vector of 32 bit signed integers.
If the length of the the bit string is not divisible by 32, it is sign-extended to the next length that is, then it is converted.
§Example
let bits = Bits::new([0x10, 0x32, 0x54, 0x76]);
let ints = Vec::<i32>::from(bits);
assert_eq!(ints.len(), 1);
assert_eq!(ints, vec![0x76543210]);
let bits = Bits::from([0x10, 0x32, 0x54, 0x76]);
let ints = Vec::<i32>::from(bits);
assert_eq!(ints.len(), 1);
assert_eq!(ints, vec![0xF6543210u32 as i32]);Source§impl From<Bits> for Vec<i64>
impl From<Bits> for Vec<i64>
Source§fn from(bits: Bits) -> Self
fn from(bits: Bits) -> Self
Converts a bit string to a vector of 64 bit signed integers.
If the length of the the bit string is not divisible by 64, it is sign-extended to the next length that is, then it is converted.
§Example
let bits = Bits::new([0x10, 0x32, 0x54, 0x76]);
let longs = Vec::<i64>::from(bits);
assert_eq!(longs.len(), 1);
assert_eq!(longs, vec![0x0000000076543210]);
let bits = Bits::from([0x10, 0x32, 0x54, 0x76]);
let longs = Vec::<i64>::from(bits);
assert_eq!(longs.len(), 1);
assert_eq!(longs, vec![0xFFFFFFFFF6543210u64 as i64]);Source§impl From<Bits> for Vec<i8>
impl From<Bits> for Vec<i8>
Source§fn from(bits: Bits) -> Self
fn from(bits: Bits) -> Self
Converts a bit string to a vector of 8 bit signed integers.
If the length of the the bit string is not divisible by 8, it is sign-extended to the next length that is, then it is converted.
§Example
let bits = Bits::new([0x10, 0xFE]);
let ibytes = Vec::<i8>::from(bits);
assert_eq!(ibytes.len(), 2);
assert_eq!(ibytes, vec![0x10, 0xFEu8 as i8]);
let bits = Bits::from([0x10, 0x5E]);
let ibytes = Vec::<i8>::from(bits);
assert_eq!(ibytes.len(), 2);
assert_eq!(ibytes, vec![0x10, 0xDEu8 as i8]);Source§impl From<Bits> for Vec<isize>
impl From<Bits> for Vec<isize>
Source§fn from(bits: Bits) -> Self
fn from(bits: Bits) -> Self
Converts a bit string to a vector of signed integers.
If the length of the the bit string is not divisible by the length of isize, it is
sign-extended to the next length that is, then it is converted.
§Example
let bits = Bits::new([0x10, 0x32, 0x54, 0x76]);
let signed_ints = Vec::<isize>::from(bits);
assert_eq!(signed_ints.len(), 1);
assert_eq!(signed_ints, vec![0x0000000076543210]);
let bits = Bits::from([0x10, 0x32, 0x54, 0x76]);
let signed_ints = Vec::<isize>::from(bits);
assert_eq!(signed_ints.len(), 1);
assert_eq!(signed_ints, vec![0xFFFFFFFFF6543210u64 as isize]);Source§impl From<Bits> for Vec<u16>
impl From<Bits> for Vec<u16>
Source§fn from(bits: Bits) -> Self
fn from(bits: Bits) -> Self
Converts a bit string to a vector of 16 bit unsigned integers.
§Example
let bits = Bits::new([0x10, 0xFE]);
let shorts = Vec::<u16>::from(bits);
assert_eq!(shorts.len(), 1);
assert_eq!(shorts, vec![0xFE10]);
let bits = Bits::from([0x10, 0x5E]);
let shorts = Vec::<u16>::from(bits);
assert_eq!(shorts.len(), 1);
assert_eq!(shorts, vec![0x5E10])Source§impl From<Bits> for Vec<u32>
impl From<Bits> for Vec<u32>
Source§fn from(bits: Bits) -> Self
fn from(bits: Bits) -> Self
Converts a bit string to a vector of 32 bit unsigned integers.
§Example
let bits = Bits::new([0x10, 0x32, 0x54, 0x76]);
let uints = Vec::<u32>::from(bits);
assert_eq!(uints.len(), 1);
assert_eq!(uints, vec![0x76543210]);
let bits = Bits::from([0x10, 0x32, 0x54, 0x76]);
let uints = Vec::<u32>::from(bits);
assert_eq!(uints.len(), 1);
assert_eq!(uints, vec![0x76543210]);Source§impl From<Bits> for Vec<u64>
impl From<Bits> for Vec<u64>
Source§fn from(bits: Bits) -> Self
fn from(bits: Bits) -> Self
Converts a bit string to a vector of 64 bit unsigned integers.
§Example
let bits = Bits::new([0x10, 0x32, 0x54, 0x76]);
let ulongs = Vec::<u64>::from(bits);
assert_eq!(ulongs.len(), 1);
assert_eq!(ulongs, vec![0x0000000076543210]);
let bits = Bits::from([0x10, 0x32, 0x54, 0x76]);
let ulongs = Vec::<u64>::from(bits);
assert_eq!(ulongs.len(), 1);
assert_eq!(ulongs, vec![0x0000000076543210]);Source§impl From<Bits> for Vec<u8>
impl From<Bits> for Vec<u8>
Source§fn from(bits: Bits) -> Self
fn from(bits: Bits) -> Self
Converts a bit string to a vector of 8 bit unsigned integers.
§Example
let bits = Bits::new([0x10, 0xFE]);
let bytes = Vec::<u8>::from(bits);
assert_eq!(bytes.len(), 2);
assert_eq!(bytes, vec![0x10, 0xFE]);
let bits = Bits::from([0x10, 0x5E]);
let bytes = Vec::<u8>::from(bits);
assert_eq!(bytes.len(), 2);
assert_eq!(bytes, vec![0x10, 0x5E]);Source§impl From<Bits> for Vec<usize>
impl From<Bits> for Vec<usize>
Source§fn from(bits: Bits) -> Self
fn from(bits: Bits) -> Self
Converts a bit string to a vector of unsigned integers.
§Example
let bits = Bits::new([0x10, 0x32, 0x54, 0x76]);
let unsigned_ints = Vec::<u64>::from(bits);
assert_eq!(unsigned_ints.len(), 1);
assert_eq!(unsigned_ints, vec![0x0000000076543210]);
let bits = Bits::from([0x10, 0x32, 0x54, 0x76]);
let unsigned_ints = Vec::<u64>::from(bits);
assert_eq!(unsigned_ints.len(), 1);
assert_eq!(unsigned_ints, vec![0x0000000076543210]);Source§impl From<Bits> for f32
impl From<Bits> for f32
Source§fn from(bits: Bits) -> Self
fn from(bits: Bits) -> Self
Converts up to the first 32 bits of a bit string to a 32 bit float
§Example
let bits = Bits::new([0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE]);
let float = f32::from(bits);
assert_eq!(float, f32::from_bits(0x76543210));
let bits = Bits::new([0x10, 0x32, 0x54]);
let float = f32::from(bits);
assert_eq!(float, f32::from_bits(0x00543210));Source§impl From<Bits> for f64
impl From<Bits> for f64
Source§fn from(bits: Bits) -> Self
fn from(bits: Bits) -> Self
Converts up to the first 64 bits of a bit string to a 64 bit float
§Example
let bits = Bits::new([0x10, 0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE]);
let float = f64::from(bits);
assert_eq!(float, f64::from_bits(0xFEDCBA9876543210));
let bits = Bits::new([0x10, 0x32, 0x54]);
let float = f64::from(bits);
assert_eq!(float, f64::from_bits(0x0000000000543210));Source§impl From<Bits> for i16
impl From<Bits> for i16
Source§fn from(bits: Bits) -> Self
fn from(bits: Bits) -> Self
Converts up the first 16 bits of a bit string to a 16 bit signed integer.
If the length of the the bit string is less than 16, the result is sign-extended to 16 bits.
§Example
let bits = Bits::new([0x10, 0xFE]);
let ishort = i16::from(bits);
assert_eq!(ishort, 0xFE10u16 as i16);
let bits = Bits::from([0x10, 0x5E]);
let ishort = i16::from(bits);
assert_eq!(ishort, 0xDE10u16 as i16);Source§impl From<Bits> for i32
impl From<Bits> for i32
Source§fn from(bits: Bits) -> Self
fn from(bits: Bits) -> Self
Converts up to the first 32 bits of a bit string to a 32 bit signed integer.
If the length of the the bit string is less than 32, the result is sign-extended to 32 bits.
§Example
let bits = Bits::new([0x10, 0x32, 0x54, 0x76]);
let int = i32::from(bits);
assert_eq!(int, 0x76543210);
let bits = Bits::from([0x10, 0x32, 0x54, 0x76]);
let int = i32::from(bits);
assert_eq!(int, 0xF6543210u32 as i32);Source§impl From<Bits> for i64
impl From<Bits> for i64
Source§fn from(bits: Bits) -> Self
fn from(bits: Bits) -> Self
Converts up to the first 64 bits of a bit string to a 64 bit signed integer.
If the length of the the bit string is less than 64, the result is sign-extended to 64 bits.
§Example
let bits = Bits::new([0x10, 0x32, 0x54, 0x76]);
let long = i64::from(bits);
assert_eq!(long, 0x0000000076543210);
let bits = Bits::from([0x10, 0x32, 0x54, 0x76]);
let long = i64::from(bits);
assert_eq!(long, 0xFFFFFFFFF6543210u64 as i64);Source§impl From<Bits> for i8
impl From<Bits> for i8
Source§fn from(bits: Bits) -> Self
fn from(bits: Bits) -> Self
Converts up to the first 8 bits of a bit string to 8 bit signed integer.
If the length of the the bit string is less than 8, the result is sign-extended to 8 bits.
§Example
let bits = Bits::new([0xFE]);
let ibyte = i8::from(bits);
assert_eq!(ibyte, 0xFEu8 as i8);
let bits = Bits::from([0x5E]);
let ibyte = i8::from(bits);
assert_eq!(ibyte, 0xDEu8 as i8);Source§impl From<Bits> for isize
impl From<Bits> for isize
Source§fn from(bits: Bits) -> Self
fn from(bits: Bits) -> Self
Converts up to the first 64 bits of a bit string to a 64 bit signed integer.
If the length of the the bit string is less than the length of isize, the result is
sign-extended to 64 bits.
§Example
let bits = Bits::new([0x10, 0x32, 0x54, 0x76]);
let signed = isize::from(bits);
assert_eq!(signed, 0x0000000076543210);
let bits = Bits::from([0x10, 0x32, 0x54, 0x76]);
let signed = isize::from(bits);
assert_eq!(signed, 0xFFFFFFFFF6543210u64 as isize);Source§impl From<Bits> for u16
impl From<Bits> for u16
Source§fn from(bits: Bits) -> Self
fn from(bits: Bits) -> Self
Converts up the first 16 bits of a bit string to a 16 bit unsigned integer.
§Example
let bits = Bits::new([0x10, 0xFE]);
let ushort = u16::from(bits);
assert_eq!(ushort, 0xFE10);
let bits = Bits::from([0x10, 0x5E]);
let ushort = u16::from(bits);
assert_eq!(ushort, 0x5E10);Source§impl From<Bits> for u32
impl From<Bits> for u32
Source§fn from(bits: Bits) -> Self
fn from(bits: Bits) -> Self
Converts up to the first 32 bits of a bit string to a 32 bit unsigned integer.
§Example
let bits = Bits::new([0x10, 0x32, 0x54, 0x76]);
let uint = u32::from(bits);
assert_eq!(uint, 0x76543210);
let bits = Bits::from([0x10, 0x32, 0x54, 0x76]);
let uint = u32::from(bits);
assert_eq!(uint, 0x76543210);Source§impl From<Bits> for u64
impl From<Bits> for u64
Source§fn from(bits: Bits) -> Self
fn from(bits: Bits) -> Self
Converts up to the first 64 bits of a bit string to a 64 bit unsigned integer.
§Example
let bits = Bits::new([0x10, 0x32, 0x54, 0x76]);
let ulong = u64::from(bits);
assert_eq!(ulong, 0x0000000076543210);
let bits = Bits::from([0x10, 0x32, 0x54, 0x76]);
let ulong = u64::from(bits);
assert_eq!(ulong, 0x0000000076543210);Source§impl From<Bits> for usize
impl From<Bits> for usize
Source§fn from(bits: Bits) -> Self
fn from(bits: Bits) -> Self
Converts up to the first 64 bits of a bit string to an unsigned integer.
§Example
let bits = Bits::new([0x10, 0x32, 0x54, 0x76]);
let unsigned = usize::from(bits);
assert_eq!(unsigned, 0x0000000076543210);
let bits = Bits::from([0x10, 0x32, 0x54, 0x76]);
let unsigned = usize::from(bits);
assert_eq!(unsigned, 0x0000000076543210);Source§impl From<Vec<bool>> for Bits
impl From<Vec<bool>> for Bits
Source§fn from(bits: Vec<bool>) -> Self
fn from(bits: Vec<bool>) -> Self
Creates a bit string from booleans.
§Examples
let bits1 = vec![true, false, true, true, false, true, true];
let x1 = Bits::from(bits1);
assert_eq!(x1.len(), 7);
assert_eq!(x1, Bits::from([0x6D]));
let bits2 = vec![true, false, true, false, false, false, false, false, false];
let x2 = Bits::from(bits2);
assert_eq!(x2.len(), 9);
assert_eq!(x2, Bits::slice(&[0x05, 0x00], 9));Source§impl FromIterator<bool> for Bits
impl FromIterator<bool> for Bits
Source§impl FromIterator<u8> for Bits
impl FromIterator<u8> for Bits
Source§impl IntoIterator for Bits
impl IntoIterator for Bits
Source§impl Ord for Bits
impl Ord for Bits
Source§fn cmp(&self, other: &Self) -> Ordering
fn cmp(&self, other: &Self) -> Ordering
Compares a bit string with another
§Examples
let x = Bits::from([0x0A, 0x0B, 0x0C]);
let y = Bits::from([0x09, 0x0B, 0x0C]);
let z = Bits::from([0x0A, 0x09, 0x0D]);
assert_eq!(x.cmp(&y), Ordering::Greater);
assert_eq!(x.cmp(&z), Ordering::Less);1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for Bits
impl PartialEq for Bits
Source§fn eq(&self, other: &Self) -> bool
fn eq(&self, other: &Self) -> bool
Whether two bit strings are equal.
§Examples
let x = Bits::from([0x20, 0x30, 0x40]);
assert!(Bits::eq(&x, &x));
let y = Bits::from([0xA0, 0xB0, 0xC0]);
assert!(Bits::ne(&x, &y));
let z1 = Bits::from([0x20, 0x30, 0x40, 0x00]);
assert!(Bits::eq(&x, &z1));
let z2 = Bits::new([0x20, 0x30, 0x40, 0x00]);
assert!(Bits::ne(&x, &z2));Source§impl PartialOrd for Bits
impl PartialOrd for Bits
Source§fn partial_cmp(&self, other: &Self) -> Option<Ordering>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
Compares a bit string with another
§Examples
let x = Bits::from([0x0A, 0x0B, 0x0C]);
let y = Bits::from([0x09, 0x0B, 0x0C]);
let z = Bits::from([0x0A, 0x09, 0x0D]);
assert_eq!(x.partial_cmp(&y), Some(Ordering::Greater));
assert_eq!(x.partial_cmp(&z), Some(Ordering::Less));Source§impl Shl<&usize> for &Bits
impl Shl<&usize> for &Bits
Source§fn shl(self, count: &usize) -> Self::Output
fn shl(self, count: &usize) -> Self::Output
Shifts out upper bits, shifting in zeros on the lower end.
§Examples
let x1 = Bits::from([0x0A, 0x0B, 0x0C]);
let s1 = &x1 << &17;
assert_eq!(s1, Bits::slice(&[0x00, 0x00, 0x04], 20));
assert_eq!(s1.len(), x1.len());
let x2 = Bits::new([0x0A, 0x0B, 0x0C]);
let s2 = &x2 << &4;
assert_eq!(s2, Bits::new([0xA0, 0xB0, 0xC0]));
assert_eq!(s2.len(), x2.len());Source§impl Shl<&usize> for Bits
impl Shl<&usize> for Bits
Source§fn shl(self, count: &usize) -> Self::Output
fn shl(self, count: &usize) -> Self::Output
Shifts out upper bits, shifting in zeros on the lower end.
§Examples
let x1 = Bits::from([0x0A, 0x0B, 0x0C]);
let s1 = x1 << &17;
assert_eq!(s1, Bits::slice(&[0x00, 0x00, 0x04], 20));
assert_eq!(s1.len(), 20);
let x2 = Bits::new([0x0A, 0x0B, 0x0C]);
let s2 = x2 << &4;
assert_eq!(s2, Bits::new([0xA0, 0xB0, 0xC0]));
assert_eq!(s2.len(), 24);Source§impl Shl<usize> for &Bits
impl Shl<usize> for &Bits
Source§fn shl(self, count: usize) -> Self::Output
fn shl(self, count: usize) -> Self::Output
Shifts out upper bits, shifting in zeros on the lower end.
§Examples
let x1 = Bits::from([0x0A, 0x0B, 0x0C]);
let s1 = &x1 << 17;
assert_eq!(s1, Bits::slice(&[0x00, 0x00, 0x04], 20));
assert_eq!(s1.len(), x1.len());
let x2 = Bits::new([0x0A, 0x0B, 0x0C]);
let s2 = &x2 << 4;
assert_eq!(s2, Bits::new([0xA0, 0xB0, 0xC0]));
assert_eq!(s2.len(), x2.len());Source§impl Shl<usize> for Bits
impl Shl<usize> for Bits
Source§fn shl(self, count: usize) -> Self::Output
fn shl(self, count: usize) -> Self::Output
Shifts out upper bits, shifting in zeros on the lower end.
§Examples
let x1 = Bits::from([0x0A, 0x0B, 0x0C]);
let s1 = x1 << 17;
assert_eq!(s1, Bits::slice(&[0x00, 0x00, 0x04], 20));
assert_eq!(s1.len(), 20);
let x2 = Bits::new([0x0A, 0x0B, 0x0C]);
let s2 = x2 << 4;
assert_eq!(s2, Bits::new([0xA0, 0xB0, 0xC0]));
assert_eq!(s2.len(), 24);Source§impl ShlAssign<&usize> for Bits
impl ShlAssign<&usize> for Bits
Source§fn shl_assign(&mut self, count: &usize)
fn shl_assign(&mut self, count: &usize)
Shifts out upper bits, shifting in zeros on the lower end.
§Examples
let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]);
x1 <<= &17;
assert_eq!(x1, Bits::slice(&[0x00, 0x00, 0x04], 20));
assert_eq!(x1.len(), 20);
let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]);
x2 <<= &4;
assert_eq!(x2, Bits::new([0xA0, 0xB0, 0xC0]));
assert_eq!(x2.len(), 24);Source§impl ShlAssign<usize> for Bits
impl ShlAssign<usize> for Bits
Source§fn shl_assign(&mut self, count: usize)
fn shl_assign(&mut self, count: usize)
Shifts out upper bits, shifting in zeros on the lower end.
§Examples
let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]);
x1 <<= 17;
assert_eq!(x1, Bits::slice(&[0x00, 0x00, 0x04], 20));
assert_eq!(x1.len(), 20);
let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]);
x2 <<= 4;
assert_eq!(x2, Bits::new([0xA0, 0xB0, 0xC0]));
assert_eq!(x2.len(), 24);Source§impl Shr<&usize> for &Bits
impl Shr<&usize> for &Bits
Source§fn shr(self, count: &usize) -> Self::Output
fn shr(self, count: &usize) -> Self::Output
Shifts out lower bits, shifting zeros into the upper bits.
§Examples
let x1 = Bits::from([0x0A, 0x0B, 0x0C]);
let s1 = &x1 >> &17;
assert_eq!(s1.len(), x1.len());
assert_eq!(s1, Bits::slice(&[0x06, 0x00, 0x00], 20));
let x2 = Bits::new([0x0A, 0x0B, 0x0C]);
let s2 = &x2 >> &4;
assert_eq!(s2.len(), 24);
assert_eq!(s2, Bits::new([0xB0, 0xC0, 0x00]));Source§impl Shr<&usize> for Bits
impl Shr<&usize> for Bits
Source§fn shr(self, count: &usize) -> Self::Output
fn shr(self, count: &usize) -> Self::Output
Shifts out lower bits, shifting zeros into the upper bits.
§Examples
let x1 = Bits::from([0x0A, 0x0B, 0x0C]);
let s1 = x1 >> &17;
assert_eq!(s1.len(), 20);
assert_eq!(s1, Bits::slice(&[0x06, 0x00, 0x00], 20));
let x2 = Bits::new([0x0A, 0x0B, 0x0C]);
let s2 = x2 >> &4;
assert_eq!(s2.len(), 24);
assert_eq!(s2, Bits::new([0xB0, 0xC0, 0x00]));Source§impl Shr<usize> for &Bits
impl Shr<usize> for &Bits
Source§fn shr(self, count: usize) -> Self::Output
fn shr(self, count: usize) -> Self::Output
Shifts out lower bits, shifting zeros into the upper bits.
§Examples
let x1 = Bits::from([0x0A, 0x0B, 0x0C]);
let s1 = &x1 >> 17;
assert_eq!(s1.len(), x1.len());
assert_eq!(s1, Bits::slice(&[0x06, 0x00, 0x00], 20));
let x2 = Bits::new([0x0A, 0x0B, 0x0C]);
let s2 = &x2 >> 4;
assert_eq!(s2.len(), x2.len());
assert_eq!(s2, Bits::new([0xB0, 0xC0, 0x00]));Source§impl Shr<usize> for Bits
impl Shr<usize> for Bits
Source§fn shr(self, count: usize) -> Self::Output
fn shr(self, count: usize) -> Self::Output
Shifts out lower bits, shifting zeros into the upper bits.
§Examples
let x1 = Bits::from([0x0A, 0x0B, 0x0C]);
let s1 = x1 >> 17;
assert_eq!(s1.len(), 20);
assert_eq!(s1, Bits::slice(&[0x06, 0x00, 0x00], 20));
let x2 = Bits::new([0x0A, 0x0B, 0x0C]);
let s2 = x2 >> 4;
assert_eq!(s2.len(), 24);
assert_eq!(s2, Bits::new([0xB0, 0xC0, 0x00]));Source§impl ShrAssign<&usize> for Bits
impl ShrAssign<&usize> for Bits
Source§fn shr_assign(&mut self, count: &usize)
fn shr_assign(&mut self, count: &usize)
Shifts out lower bits, shifting zeros into the upper bits.
§Examples
let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]);
x1 >>= &17;
assert_eq!(x1.len(), 20);
assert_eq!(x1, Bits::slice(&[0x06, 0x00, 0x00], 20));
let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]);
x2 >>= &4;
assert_eq!(x2.len(), 24);
assert_eq!(x2, Bits::new([0xB0, 0xC0, 0x00]));Source§impl ShrAssign<usize> for Bits
impl ShrAssign<usize> for Bits
Source§fn shr_assign(&mut self, count: usize)
fn shr_assign(&mut self, count: usize)
Shifts out lower bits, shifting zeros into the upper bits.
§Examples
let mut x1 = Bits::from([0x0A, 0x0B, 0x0C]);
x1 >>= 17;
assert_eq!(x1, Bits::slice(&[0x06, 0x00, 0x00], 20));
assert_eq!(x1.len(), 20);
let mut x2 = Bits::new([0x0A, 0x0B, 0x0C]);
x2 >>= 4;
assert_eq!(x2, Bits::new([0xB0, 0xC0, 0x00]));
assert_eq!(x2.len(), 24);