| Crates.io | bitit |
| lib.rs | bitit |
| version | 0.1.2 |
| created_at | 2025-08-15 03:48:20.641419+00 |
| updated_at | 2025-09-30 16:50:44.140341+00 |
| description | Bitwise iteration over integers. |
| homepage | |
| repository | https://github.com/jsode64/bitit |
| max_upload_size | |
| id | 1796166 |
| size | 20,829 |
A library for bitwise iteration over Rust integers.
Provides simple and efficient iterators for binary integer types.
use bitit::BitIter;
let x = 0b10101100u8;
// Iterating each bit:
let mut bits = x.bits();
assert_eq!(bits.next(), Some(false));
assert_eq!(bits.next(), Some(false));
assert_eq!(bits.next(), Some(true));
// Or just the ones:
let mut ones = x.ones();
assert_eq!(ones.next(), Some(0b00000100u8));
assert_eq!(ones.next(), Some(0b00001000u8));
assert_eq!(ones.next_back(), Some(0b10000000u8));
// Or the zeros, even with indices:
let mut zero_indices = x.zero_indices();
assert_eq!(zero_indices.next(), Some(0));
assert_eq!(zero_indices.next(), Some(1));
assert_eq!(zero_indices.next_back(), Some(6));
// Works with floats too like this:
let f = -5.0f64;
let mut ieee_bits = f.to_bits().bits_rev();
// Now we can see its bits, such as the sign bit:
if ieee_bits.next() == Some(true) {
println!("{f} is negative.");
} else {
println!("{f} is positive.");
}
cargo run -q
-5 is negative.
Bitit provides the following iterators with the BitIter trait:
BitIter::onesBitIter::zerosBitIter::one_indicesBitIter::zero_indicesBitIter::bitsBitIter::bits_revBitIter::bits which has BitIter::bits_rev) with Iterator::revThe BitIter trait is implemented for all signed and unsigned primitive integers i8, u8, i16, etc. as well as usize and isize.