Bittersweet is a library for bit manipulation.
Bit-wise operations are often used in low-level programming and it sometimes contains magical bugs. I want to manipulate bits with more
- type-safe
- well-tested
- well-documented
- fast and efficient
You can install this library via cargo.
$ cargo add bittersweetOr you can add this to your Cargo.toml manually.
For no_std environments, disable the default std feature:
[dependencies]
bittersweet = { version = "0.3", default-features = false }The crate enables std by default. With default-features = false, the core
bitline traits and integer implementations remain available without linking the
standard library. Re-enable std when you need APIs gated behind that feature.
bittersweet supports Rust 1.56 and newer. This is the first stable release
with Rust 2021 edition support, which matches the crate's edition setting.
use bittersweet::bitline::{Bitline, Bitline8};
fn main() {
let t = 0b00111000_u8 as Bitline8;
if t.includes(0b00110000_u8) {
println!("Yes!");
}
}Following Bitline traits are implemented for u8, u16, u32, u64, u128.
So these operations keep zero-cost abstraction.
Bitline8... 8 bits (u8)Bitline16... 16 bits (u16)Bitline32... 32 bits (u32)Bitline64... 64 bits (u64)Bitline128... 128 bits (u128)
See docs.rs
See CHANGELOG.md for release notes.
is_not_empty and is_not_full were deprecated in 0.2.1. They remain
available throughout the 0.3.x line so downstream users can migrate to
!is_empty() and !is_full() without an immediate break. Both aliases are
scheduled for removal in 0.4.0.
rank(index, bit), rank_range(begin, end, bit), try_rank(index, bit), and
try_rank_range(begin, end, bit) are deprecated in 0.3.1. They remain
available throughout the 0.3.x line so downstream users can migrate to the
explicit _0 / _1 variants without an immediate break. These bool-dispatch
aliases are scheduled for removal in 0.4.0.
Bitline position APIs use MSB-first indexing. Index 0 is the most significant
bit, shown at the left edge of a binary literal or bit_repr(). The last valid
index is the least significant bit.
Range arguments use the same ordering and follow Rust's half-open convention:
[begin, end). For example, 0b10000000_u8.access(0) is true, and
Bitline8::by_range(2, 5) returns 0b00111000_u8.
as_emptyas_fullmask_01mask_10by_rangebytes_lengthlengthis_emptyis_not_empty(deprecated since 0.2.1 — use!is_empty()instead; retained through 0.3.x and scheduled for removal in 0.4.0)is_fullis_not_full(deprecated since 0.2.1 — use!is_full()instead; retained through 0.3.x and scheduled for removal in 0.4.0)first_indexlast_indexradiusaroundwith_aroundfirst_bitlast_bitfirst_bitslast_bitsfilled_first_bit_to_last_bitnum_bitsbit_reprrangeincludesoverlapsremoveleft_rotateright_rotatebin_to_gray_codegray_code_to_binbit_reversal_permutation_to_binbin_to_bit_reversal_permutationtwo_bits_gray_code_rotationaccessrank_0rank_1rank(deprecated since 0.3.1 — userank_0/rank_1instead; retained through 0.3.x and scheduled for removal in 0.4.0)rank_range_0rank_range_1rank_range(deprecated since 0.3.1 — userank_range_0/rank_range_1instead; retained through 0.3.x and scheduled for removal in 0.4.0)try_accesstry_rank_0try_rank_1try_rank(deprecated since 0.3.1 — usetry_rank_0/try_rank_1instead; retained through 0.3.x and scheduled for removal in 0.4.0)try_rank_range_0try_rank_range_1try_rank_range(deprecated since 0.3.1 — usetry_rank_range_0/try_rank_range_1instead; retained through 0.3.x and scheduled for removal in 0.4.0)select_0select_1select
This repository uses lefthook to run the same checks as CI locally, so problems surface before they reach CI.
# Install the Git hooks (once; requires lefthook on your PATH)
lefthook installOnce installed, the hooks run automatically:
- pre-commit:
cargo fmt --all -- --checkandcargo clippy -- -D warnings - pre-push: the above plus
cargo test
CI still runs the full suite (see .github/workflows/); the hooks only bring that
feedback earlier on your machine.
BSD-3-Clause