Bits

Struct Bits 

Source
pub struct Bits { /* private fields */ }

Implementations§

Source§

impl Bits

Source

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

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

pub fn empty() -> Self

Creates an empty bit string.

§Examples
let mut x = Bits::empty();

assert_eq!(x.len(), 0);
Source

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

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

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

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

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

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

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

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

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

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

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

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

pub fn byte(&self, i: usize) -> u8

State of a byte.

§Examples
let mut x = Bits::from([0x0A, 0x0B]);

assert_eq!(x.byte(0), 0x0A);
assert_eq!(x.byte(1), 0x0B);
let mut x = Bits::from([0x0A, 0x0B]);

assert_eq!(x.byte(2), 0x0C);
Source

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

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

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

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

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

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

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

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

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

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

pub fn i(&self, i: usize) -> u8

State of a bit.

§Examples
let mut x = Bits::from([0x0F]);

assert_eq!(x.i(3), 1u8);
let mut x = Bits::from([0x0F]);

assert_eq!(x.i(4), 0);
Source

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

pub fn reset_byte(&mut self, i: usize)

Sets the state of a byte to zero.

§Examples
let mut x = Bits::from([0x0A, 0x0B, 0x0C]);

x.reset_byte(1);

assert_eq!(x, Bits::from([0x0A, 0x00, 0x0C]));

x.reset_byte(2);

assert_eq!(x, Bits::slice(&[0x0A, 0x00, 0x00], 20));
let mut x = Bits::from([0x0A, 0x0B, 0x0C]);

x.reset_byte(3);
Source

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

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

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

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

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

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

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

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

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

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

Sets the state of a byte to all ones.

§Examples
let mut x = Bits::from([0x0A, 0x0B, 0x0C]);

x.set_byte(1);

assert_eq!(x, Bits::from([0x0A, 0xFF, 0x0C]));

x.set_byte(2);

assert_eq!(x, Bits::slice(&[0x0A, 0xFF, 0x0F], 20));
let mut x = Bits::from([0x0A, 0x0B, 0x0C]);

x.toggle_byte(3);
Source

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

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

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

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

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

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

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

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

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

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

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

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

pub fn toggle(&mut self, i: usize)

Flips the state of a bit.

§Examples
let mut x = Bits::from([0x0A, 0x0B, 0x0C]);

assert_eq!(x.i(0), 0u8);
assert_eq!(x.i(1), 1u8);

x.toggle(0);
x.toggle(1);

assert_eq!(x.i(0), 1u8);
assert_eq!(x.i(1), 0u8);
let mut x = Bits::from([0x0A, 0x0B, 0x0C]);

x.toggle(21);
Source

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

pub fn toggle_byte(&mut self, i: usize)

Flips the state of a byte.

§Examples
let mut x = Bits::from([0x0A, 0x0B, 0x0C]);

x.toggle_byte(1);

assert_eq!(x, Bits::from([0x0A, 0xF4, 0x0C]));

x.toggle_byte(2);

assert_eq!(x, Bits::slice(&[0x0A, 0xF4, 0x03], 20));
let mut x = Bits::from([0x0A, 0x0B, 0x0C]);

x.toggle_byte(3);
Source

pub fn toggle_bytes(&mut self, start: usize, count: usize)

Flips the state of a range of bytes.

§Examples
let mut x = Bits::from([0x0A, 0x0B, 0x0C]);

x.toggle_bytes(1, 2);

assert_eq!(x, Bits::slice(&[0x0A, 0xF4, 0x03], 20));
let mut x = Bits::from([0x0A, 0x0B, 0x0C]);

x.toggle_bytes(3, 2);
let mut x = Bits::from([0x0A, 0x0B, 0x0C]);

x.toggle_bytes(0, 4);
Source

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

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

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

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

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

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 Binary for Bits

Source§

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

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

impl BitAnd<&Bits> for &Bits

Source§

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§

type Output = Bits

The resulting type after applying the & operator.
Source§

impl BitAnd<&Bits> for Bits

Source§

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§

type Output = Bits

The resulting type after applying the & operator.
Source§

impl<'a> BitAnd<Bits> for &'a Bits

Source§

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§

type Output = Bits

The resulting type after applying the & operator.
Source§

impl BitAnd for Bits

Source§

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§

type Output = Bits

The resulting type after applying the & operator.
Source§

impl BitAndAssign<&Bits> for Bits

Source§

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

Source§

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

Source§

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§

type Output = Bits

The resulting type after applying the | operator.
Source§

impl BitOr<&Bits> for Bits

Source§

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§

type Output = Bits

The resulting type after applying the | operator.
Source§

impl<'a> BitOr<Bits> for &'a Bits

Source§

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§

type Output = Bits

The resulting type after applying the | operator.
Source§

impl BitOr for Bits

Source§

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§

type Output = Bits

The resulting type after applying the | operator.
Source§

impl BitOrAssign<&Bits> for Bits

Source§

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

Source§

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

Source§

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§

type Output = Bits

The resulting type after applying the ^ operator.
Source§

impl BitXor<&Bits> for Bits

Source§

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§

type Output = Bits

The resulting type after applying the ^ operator.
Source§

impl<'a> BitXor<Bits> for &'a Bits

Source§

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§

type Output = Bits

The resulting type after applying the ^ operator.
Source§

impl BitXor for Bits

Source§

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§

type Output = Bits

The resulting type after applying the ^ operator.
Source§

impl BitXorAssign<&Bits> for Bits

Source§

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

Source§

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 Clone for Bits

Source§

fn clone(&self) -> Self

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 Bits

Source§

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

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

impl Display for Bits

Source§

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

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

impl Div<usize> for &Bits

Source§

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§

type Output = (Bits, Bits)

The resulting type after applying the / operator.
Source§

impl Div<usize> for Bits

Source§

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§

type Output = (Bits, Bits)

The resulting type after applying the / operator.
Source§

impl Drop for Bits

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl From<&[bool]> for Bits

Source§

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 From<&[u8]> for Bits

Source§

fn from(data: &[u8]) -> Self

Creates a bit string from a sequence of bytes, ignoring leading zeros.

§Examples
let mut x = Bits::from([0x0A, 0x0B, 0x0C, 0x00].as_slice());

assert_eq!(x.len(), 20);
Source§

impl<const N: usize> From<[bool; N]> for Bits

Source§

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<const N: usize> From<[u8; N]> for Bits

Source§

fn from(bytes: [u8; N]) -> Self

Creates a bit string from a sequence of bytes, ignoring leading zeros.

§Examples
let mut x = Bits::from([0x0A, 0x0B, 0x0C, 0x00]);

assert_eq!(x.len(), 20);
Source§

impl From<Bits> for Vec<bool>

Source§

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>

Source§

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>

Source§

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>

Source§

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>

Source§

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>

Source§

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>

Source§

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>

Source§

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>

Source§

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>

Source§

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>

Source§

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>

Source§

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>

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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 u8

Source§

fn from(bits: Bits) -> Self

Converts up to the first 8 bits of a bit string to a 8 bit unsigned integer.

§Example
let bits = Bits::new([0xFE]);
let byte = u8::from(bits);

assert_eq!(byte, 0xFE);

let bits = Bits::from([0x5E]);
let byte = u8::from(bits);

assert_eq!(byte, 0x5E);
Source§

impl From<Bits> for usize

Source§

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

Source§

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 From<Vec<u8>> for Bits

Source§

fn from(bytes: Vec<u8>) -> Self

Creates a bit string from a sequence of bytes, ignoring leading zeros.

§Examples
let mut x = Bits::from(vec![0x0A, 0x0B, 0x0C, 0x00]);

assert_eq!(x.len(), 20);
Source§

impl FromIterator<bool> for Bits

Source§

fn from_iter<I: IntoIterator<Item = bool>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl FromIterator<u8> for Bits

Source§

fn from_iter<I: IntoIterator<Item = u8>>(iter: I) -> Self

Creates a bit string from a sequence of bytes, ignoring leading zeros.

§Examples
let mut x = Bits::from_iter([0x0A, 0x0B, 0x0C, 0x00]);

assert_eq!(x.len(), 20);
Source§

impl IntoIterator for Bits

Source§

type Item = u8

The type of the elements being iterated over.
Source§

type IntoIter = IntoBits

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl Not for &Bits

Source§

fn not(self) -> Self::Output

Flips every bit.

§Examples
let x = Bits::from([0x0A, 0x0B, 0x0C]);

assert_eq!(!&x, Bits::slice(&[0xF5, 0xF4, 0x03], 20));
Source§

type Output = Bits

The resulting type after applying the ! operator.
Source§

impl Not for Bits

Source§

fn not(self) -> Self::Output

Flips every bit.

§Examples
let x = Bits::from([0x0A, 0x0B, 0x0C]);

assert_eq!(!x, Bits::slice(&[0xF5, 0xF4, 0x03], 20));
Source§

type Output = Bits

The resulting type after applying the ! operator.
Source§

impl Ord for Bits

Source§

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) -> Self
where Self: Sized,

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

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

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

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

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

impl PartialEq for Bits

Source§

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

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

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

impl PartialOrd for Bits

Source§

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

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

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

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

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

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

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

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

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

impl Shl<&usize> for &Bits

Source§

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§

type Output = Bits

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

impl Shl<&usize> for Bits

Source§

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§

type Output = Bits

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

impl Shl<usize> for &Bits

Source§

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§

type Output = Bits

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

impl Shl<usize> for Bits

Source§

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§

type Output = Bits

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

impl ShlAssign<&usize> for Bits

Source§

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

Source§

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

Source§

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§

type Output = Bits

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

impl Shr<&usize> for Bits

Source§

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§

type Output = Bits

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

impl Shr<usize> for &Bits

Source§

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§

type Output = Bits

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

impl Shr<usize> for Bits

Source§

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§

type Output = Bits

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

impl ShrAssign<&usize> for Bits

Source§

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

Source§

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

impl Eq for Bits

Source§

impl Send for Bits

Source§

impl Sync for Bits

Auto Trait Implementations§

§

impl Freeze for Bits

§

impl RefUnwindSafe for Bits

§

impl Unpin for Bits

§

impl UnwindSafe for Bits

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

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

Source§

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

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

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

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

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

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

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

Source§

fn to_string(&self) -> String

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

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.