#no-std #bit #bit-manipulation #value #pos #8 #true

no-std bit

A library which provides helpers to manipulate bits and bit ranges

2 releases

Uses old Rust 2015

0.1.1 Apr 12, 2017
0.1.0 Apr 11, 2017

#1277 in Rust patterns

Download history 861/week @ 2023-10-20 501/week @ 2023-10-27 779/week @ 2023-11-03 639/week @ 2023-11-10 618/week @ 2023-11-17 979/week @ 2023-11-24 796/week @ 2023-12-01 799/week @ 2023-12-08 750/week @ 2023-12-15 1036/week @ 2023-12-22 756/week @ 2023-12-29 436/week @ 2024-01-05 723/week @ 2024-01-12 562/week @ 2024-01-19 846/week @ 2024-01-26 395/week @ 2024-02-02

2,586 downloads per month
Used in 10 crates (9 directly)

MIT/Apache

7KB
104 lines

Bit

crates.io version badge

bit is a library which provides some useful helpers for dealing with bits and bit ranges. For now it's just a rewrite of rust-bit-field crate, but more features are planned. Some of them could be:

  • Support for arrays and slices.
  • bitflags-like functionality.

Usage

Add to your Cargo.toml:

[dependencies]
bit = "0.1"

And add to your code:

extern crate bit;
use bit::BitIndex;

Example

extern crate bit;
use bit::BitIndex;

fn main() {
    let mut value = 0b11010110u8;

    // 8
    println!("{}", u8::bit_length());

    // true
    println!("{}", value.bit(1));

    // 0b10
    println!("{:#b}", value.bit_range(0..2));

    value
        .set_bit(3, true)
        .set_bit(2, false)
        .set_bit_range(5..8, 0b001);

    // 0b111010
    println!("{:#b}", value);
}

lib.rs:

This crate adds simple bit-manipulation helpers in a convenient trait. Also, it implements them for the unsigned primitive types.

No runtime deps