#bit #extract #binary

bitlab

Extracting a range of bits from a binary data source

22 releases

✓ Uses Rust 2018 edition

0.8.2 Jan 21, 2019
0.8.1 Mar 15, 2018
0.8.0 Feb 16, 2018
0.4.5 Dec 16, 2017
0.4.1 Nov 28, 2017

#82 in Data structures

Download history 392/week @ 2019-07-17 48/week @ 2019-07-24 7/week @ 2019-07-31 91/week @ 2019-08-07 34/week @ 2019-08-14 404/week @ 2019-08-21 248/week @ 2019-08-28 255/week @ 2019-09-04 94/week @ 2019-09-11 650/week @ 2019-09-18 267/week @ 2019-09-25 105/week @ 2019-10-02 69/week @ 2019-10-09 68/week @ 2019-10-16 45/week @ 2019-10-23

220 downloads per month

MIT license

140KB
2K SLoC

Travis Build Status Build status Latest Version

Objective:

To extract a range of bits from a binary data source or to insert a range of bits into a binary data structure

Status

passively-maintained

Documentation

This crate is published at crates.io. The detailed documentation is available at docs.rs/bitlab

Version

0.8.2

Usage

  1. In your Cargo.toml file, add bitlab = "0.8" under [dependencies]
  2. In your source file, add extern crate bitlab and use bitlab::*;

Example 1:

Start at bit offset 1, extract 3 bits and interpret the result as u8

use bitlab::*;
let a: i8 = -33; // = 0b1101_1111;
let b = a.get_u8(1, 3).unwrap();  // 1 --> 101 <-- 1111
//                                         = 5
assert_eq!(b, 5);

Example 2:

use bitlab::*;
let a: u8 = 0b0000_0101;

// Get the most significant bit. It has the bit offset 0
assert_eq!(a.get_bit(0).unwrap(), false);

// Set the most significant bit. Expect 0b1000_0101
assert_eq!(a.set_bit(0).unwrap(), 133);

// Clear the most significant bit. Expect 0b0000_0101
assert_eq!(a.clear_bit(0).unwrap(), 5);

Example 3:

The data source is a vector of u8 types. We want to go to byte offset 1, bit offset 7 and starting from there extract 3 bits as an u16

use bitlab::*;
let v: Vec<u8> = vec!{ 0x48, 0x61, 0x6C, 0x6C, 0x6F }; // = "Hallo"
let bar = v.get_u16(1, 7, 3); // relevant bytes = 0x616C = 0b0110_000  --> 1_01 <-- 10_1100
//                                                                         = 5
assert_eq!(bar.unwrap(), 5);

Example 4:

Insert a 2 bit unsigned integer value (b = 3) into a variable starting at the bit offset 1, where the offset = zero is the most significant bit.

use bitlab::*;
let a : u8 = 0;
let b : u8 = 3;
assert_eq!(a.set(1, 2, b).unwrap(), 0b0110_0000);

Example 5:

Insert the value 3 (only 2 bits = 0b11) from a u8 into a vector at byte offset = 1 and bit offset = 15

use bitlab::*;
let a : u8 = 3; // = 0b0000_0011
let mut v: Vec<u8> = vec!{ 0x48, 0x61, 0x6C, 0x6C, 0x6F };
// relevant bytes = 0x6C_6C = 0b0110_110 --> 0_0 <-- 110_1100
let bar = v.set(1, 15, 2, a);
assert_eq!(v[2], 0b0110_1101);
assert_eq!(v[3], 0b1110_1100);

Example 6:

There is a very simple application in the examples directory, which extracts the color resolution from a real gif file. To run it enter the folloeing in the command line

cargo run --release --example gif

Dependencies

~210KB