bitit 0.1.2

Bitwise iteration over integers.
Documentation
  • Coverage
  • 100%
    14 out of 14 items documented13 out of 13 items with examples
  • Size
  • Source code size: 20.33 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 8.21 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 14s Average build duration of successful builds.
  • all releases: 14s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • jsode64/bitit
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • jsode64

Bitit

A library for bitwise iteration over Rust integers.

Provides simple and efficient iterators for binary integer types.

Examples

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.

Features

Bitit provides the following iterators with the BitIter trait:

  • Iterating ones from least to most significant with BitIter::ones
  • Iterating zeros from least to most significant with BitIter::zeros
  • Iterating the bitwise indices of all ones with BitIter::one_indices
  • Iterating the bitwise indices of all zeros with BitIter::zero_indices
  • Iterating all bits as booleans from least to most significant with BitIter::bits
  • Iterating all bits as booleans from most to least significant with BitIter::bits_rev
  • The ability to reverse all iterators (except BitIter::bits which has BitIter::bits_rev) with Iterator::rev

The BitIter trait is implemented for all signed and unsigned primitive integers i8, u8, i16, etc. as well as usize and isize.