A Vec of Bits
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Reynard User 7a8b98a664
Some checks failed
Rust / build (push) Has been cancelled
Rust / Miri (push) Has been cancelled
Rust / fmt (push) Has been cancelled
Rust / Rust 1.82.0 (push) Has been cancelled
Rust / Rust 1.83.0 (push) Has been cancelled
Rust / clippy (push) Has been cancelled
Rust / docs (push) Has been cancelled
chore: sync dependencies (monorepo)
2026-04-07 18:06:22 +02:00
.github/workflows Bump MSRV to 1.82 2026-02-28 16:49:38 +01:00
.vscode Fix DeRon 2026-02-28 18:40:37 +01:00
benches Add benchmark for to_bytes 2025-12-15 00:24:37 +03:30
src Release version 0.9.0 2026-03-10 12:23:30 +01:00
.cargo-rdme.toml Update README.md and add rdme settings 2023-02-13 12:11:10 +01:00
.gitignore Revert changes (nits); disable crusader 2019-05-16 19:11:12 +02:00
Cargo.toml chore: sync dependencies (monorepo) 2026-04-07 18:06:22 +02:00
LICENSE-APACHE initial port of bitvec 2015-06-02 18:08:26 -07:00
LICENSE-MIT Update README.md and add rdme settings 2023-02-13 12:11:10 +01:00
README.md Release version 0.9.1 2026-03-12 12:51:08 +01:00
RELEASES.md Release version 0.9.1 2026-03-12 12:51:08 +01:00

bit-vec

A compact vector of bits.

crates.io Documentation Rust CI MSRV

Dependency Status Download Status

Usage

Add this to your Cargo.toml:

[dependencies]
bit-vec = "0.9"

If you want serde support, include the feature like this:

[dependencies]
bit-vec = { version = "0.9", features = ["serde"] }

If you want to use bit-vec in a program that has #![no_std], just drop default features:

[dependencies]
bit-vec = { version = "0.9", default-features = false }

If you want to use serde with the alloc crate instead of std, just use the serde_no_std feature:

[dependencies]
bit-vec = { version = "0.9", default-features = false, features = ["serde", "serde_no_std"] }

If you want borsh-rs support, include it like this:

[dependencies]
bit-vec = { version = "0.9", features = ["borsh"] }

Other available serialization libraries can be enabled with the miniserde and nanoserde features.

Description

Dynamic collections implemented with compact bit vectors.

Examples

This is a simple example of the Sieve of Eratosthenes which calculates prime numbers up to a given limit.

use bit_vec::BitVec;

let max_prime = 10000;

// Store the primes as a BitVec
let primes = {
    // Assume all numbers are prime to begin, and then we
    // cross off non-primes progressively
    let mut bv = BitVec::from_elem(max_prime, true);

    // Neither 0 nor 1 are prime
    bv.set(0, false);
    bv.set(1, false);

    for i in 2.. 1 + (max_prime as f64).sqrt() as usize {
        // if i is a prime
        if bv[i] {
            // Mark all multiples of i as non-prime (any multiples below i * i
            // will have been marked as non-prime previously)
            for j in i.. {
                if i * j >= max_prime {
                    break;
                }
                bv.set(i * j, false)
            }
        }
    }
    bv
};

// Simple primality tests below our max bound
let print_primes = 20;
print!("The primes below {} are: ", print_primes);
for x in 0..print_primes {
    if primes.get(x).unwrap_or(false) {
        print!("{} ", x);
    }
}
println!();

let num_primes = primes.iter().filter(|x| *x).count();
println!("There are {} primes below {}", num_primes, max_prime);
assert_eq!(num_primes, 1_229);

License

Dual-licensed for compatibility with the Rust project.

Licensed under the Apache License Version 2.0: http://www.apache.org/licenses/LICENSE-2.0, or the MIT license: http://opensource.org/licenses/MIT, at your option.