A no_std Rust crate providing a compact owned bit string type with construction, editing, matching, and bitwise operations.
The core type is BitString. Bits are packed into Box<[u64]> with unused high bits in the last word always zero (masked after every mutation).
- Construction:
new,zeros,repeat,from_bool_iter,from_words,try_from(&str) - Bitwise ops:
and,or,xor,not,shl,shr(each with_assignand_intovariants) - Bit counting:
count_ones,count_zeros - Editing:
push,pop,insert,remove,set,extend,truncate,slice,split_off,replace_interval,retain,push_bit_string,insert_bit_string - Matching:
starts_with,ends_with,contains,find,rfind,strip_prefix,strip_suffix - Access:
get,len,is_empty,as_words,get_chunk,to_string,iter
Bitwise operations and construction routines dispatch to SIMD backends automatically:
| Backend | Target | Width |
|---|---|---|
| AVX2 | x86 / x86_64 | 256-bit (4×u64) |
| SSSE3 | x86 / x86_64 | 128-bit (2×u64) |
| NEON | aarch64 | 128-bit (2×u64) |
| Scalar | all targets | fallback |
Enable target-cpu=native via .cargo/config.toml to test your local CPU's best backend.
use bit_string::BitString;
let a = BitString::try_from("1010").unwrap();
let b = BitString::try_from("1100").unwrap();
assert_eq!(a.and(&b).unwrap().to_string(), "1000");
assert_eq!(a.or(&b).unwrap().to_string(), "1110");
assert_eq!((!a).to_string(), "0101");
assert_eq!(a.count_ones(), 2);Continuous benchmarking results are published at:
https://jcfangc.github.io/bit-string/compare-plotly/index.html
This crate is still early. APIs may change before the first stable release.
Licensed under either of:
- MIT license
- Apache License, Version 2.0
at your option.