Crate nbits

Source
Expand description

Bit operations on [u8]

§Usage

[u16] chunks to mnemonic indices by 11 bits.
[u8] chunks to base64 indices by 6 bits.

§Examples

use nbits::XBits;
use nbits::FromBits;

assert_eq!(vec![0b1111_1111, 0b1100_0000].bits().all_one(), false);
assert_eq!(vec![0b1111_1111, 0b1100_0000].bits().trailing_zeros(), 6);

assert_eq!(
    [0b1111_0000_u8].bits().iter().collect::<Vec<bool>>(),
    vec![true, true, true, true, false, false, false, false]
);

assert_eq!(
    vec![0b1111_1111, 0b1111_1111].bits().chunks(6).collect::<Vec<u8>>(),
    vec![0b11_1111, 0b11_1111, 0b11_1100]
);

assert_eq!(
    vec![0b1111_1111; 3].bits().chunks(11).collect::<Vec<u16>>(),
    vec![0b111_1111_1111, 0b111_1111_1111, 0b110_0000_0000]
);

assert_eq!(
   vec![0b1111_1111, 0b1100_0000].bits_mut().reverse().to_ref().as_bytes(),
   vec![0b0000_0011, 0b1111_1111]
);

assert_eq!(
    Vec::from_bits([true, true, true, true, false, false, false, false].iter().copied()),
    [0b1111_0000]
);

assert_eq!(
    Vec::from_bits_chunk([0b11_1111_u8, 0b11_1111, 0b11_1111].into_iter(), 6),
    vec![0b1111_1111, 0b1111_1111, 0b1100_0000]
);

assert_eq!(
    Vec::from_bits_chunk([0b1111_u16, 0b1111, 0b1111].into_iter(), 6),
    vec![0b001111_00, 0b1111_0011, 0b1100_0000]
);

Re-exports§

pub use core::FromBits;

Modules§

core

Structs§

BitsMut
A mutable reference to a byte array that allows for bit-level operations.
BitsRef
A reference to a byte array that allows for bit-level operations.
NBits
NBits is a wrapper around an array of bytes that provides arithmetic and bitwise operations on the bits represented by the bytes. The operations are performed in big-endian order.

Traits§

XBits
XBits trait provides a way to work with bit-level operations on byte arrays. It allows you to get a reference to the bits in a byte array and perform operations such as checking if all bits are one or zero, and iterating over the bits.