3 releases

0.0.3 Sep 2, 2024
0.0.2 Sep 2, 2024
0.0.1 Sep 2, 2024

#1101 in Data structures

Download history 352/week @ 2024-08-30 67/week @ 2024-09-06 35/week @ 2024-09-13 72/week @ 2024-09-20 42/week @ 2024-09-27 33/week @ 2024-10-04 47/week @ 2024-10-11 70/week @ 2024-10-18 13/week @ 2024-10-25 25/week @ 2024-11-01

157 downloads per month
Used in 11 crates (2 directly)

MIT license

13KB
159 lines

BitArray - A Simple Bit Array Implementation in Rust

bit-array-rs is a Rust library that provides a simple and efficient implementation of a bit array. A bit array (or bit vector) is a data structure that compactly stores bits (0s and 1s).

Installation

Add the following to your Cargo.toml:

[dependencies]
bit-array-rs = "0.0.3"

Usage

Creating a BitArray

use bit_array_rs::BitArray;

fn main() {
    let mut bit_array = BitArray::new(16);
    bit_array.set(3);
    bit_array.set(7);
    println!("{:?}", bit_array);  // Outputs: "00001000 10000000"
    println!("{}", bit_array);    // Outputs: "0000100010000000"
}

Setting and Unsetting Bits

use bit_array_rs::BitArray;

fn main() {
    let mut bit_array = BitArray::new(8);

    bit_array.set(2);
    bit_array.unset(2);

    assert_eq!(bit_array[2], false);
    bit_array.set_bit(2, true);
    assert_eq!(bit_array[2], true);
}

Finding the First Set/Unset Bit

use bit_array_rs::BitArray;

fn main() {
    let mut bit_array = BitArray::new(16);

    bit_array.set(3);
    bit_array.set(5);

    assert_eq!(bit_array.first_unset_bit(), Some(0));
    assert_eq!(bit_array.first_set_bit(), Some(3));
}

Debug and Display

use bit_array_rs::BitArray;

fn main() {
    let mut bit_array = BitArray::new(16);
    bit_array.set(3);
    bit_array.set(7);
    bit_array.set(9);
    bit_array.set(15);

    println!("{:?}", bit_array);  // Outputs: "00010001 01000001"
    println!("{}", bit_array);    // Outputs: "0001000101000001"
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

No runtime deps