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
Experimental
Documentation
This crate is published at crates.io. The detailed documentation is available at docs.rs/bitlab
Version
0.8.0
Usage
- In your Cargo.toml file, add
bitlab = "0.8"
under[dependencies]
- In your source file, add
extern crate bitlab
anduse bitlab::*;
Example 1:
Start at bit offset 1, extract 3 bits and interpret the result as u8
use *;
let a: i8 = -33; // = 0b1101_1111;
let b = a.get_u8.unwrap; // 1 --> 101 <-- 1111
// = 5
assert_eq!;
Example 2:
use *;
let a: u8 = 0b0000_0101;
// Get the most significant bit. It has the bit offset 0
assert_eq!;
// Set the most significant bit. Expect 0b1000_0101
assert_eq!;
// Clear the most significant bit. Expect 0b0000_0101
assert_eq!;
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 *;
let v: = vec!; // = "Hallo"
let bar = v.get_u16; // relevant bytes = 0x616C = 0b0110_000 --> 1_01 <-- 10_1100
// = 5
assert_eq!;
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 *;
let a : u8 = 0;
let b : u8 = 3;
assert_eq!;
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 *;
let a : u8 = 3; // = 0b0000_0011
let mut v: = vec!;
// relevant bytes = 0x6C_6C = 0b0110_110 --> 0_0 <-- 110_1100
let bar = v.set;
assert_eq!;
assert_eq!;
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