A library for working with bitsets.
The name bittle
comes from bit
and little
. Small bitsets!
This crate defines the [Bits] and [OwnedBits] traits which allows for
generically interacting with and manipulating bit sets over types such as
u128
, [u32; 4]
, or even slices like &[u8]
.
To to these implementations it is possible to use bit-oriented APIs on regular types, such as with arrays and vectors:
```rust use bittle::Bits;
let array: [u32; 4] = bittle::set![4, 63, 71]; assert!(array.iterones().eq([4, 63, 71])); assert!(array.bittest(63));
let mut vector: Vec
vector.bitset(20); asserteq!(vector, [0, 1, 18, 3]); ```
Add bittle
as a dependency in your Cargo.toml
:
toml
[dependencies]
bittle = "0.3.5"
Due to how broadly these traits are implemented, we also try and avoid using names which are commonly used in other APIs, instead opting for terminology such as:
is_empty
becomes is_zeros
.test
becomes bit_test
.set
becomes bit_set
.clear
becomes bits_clear
.```rust use std::mem;
use bittle::Bits;
let mut a = 0u64;
assert!(a.is_zeros());
assert!(!a.bittest(31)); a.bitset(31); assert!(a.bittest(31)); a.bitclear(31); assert!(!a.bit_test(31)); ```
Some other interesting operations, such as [Bits::join_ones] are available, allowing bitsets to act like masks over other iterators:
```rust use bittle::Bits;
let elements = vec![10, 48, 101]; let mut m = 0u128;
m.bitset(0); assert!(m.joinones(&elements).eq([&10])); m.bitset(2); assert!(m.joinones(&elements).eq([&10, &101])); ```