# Crate bitlab [−] [src]

# 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.6.0

# Usage

- In your Cargo.toml file, add
`bitlab = "0.6"`

under`[dependencies]`

- 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_u8(1, 2, b).unwrap(), 0b0110_0000);

## Example 5:

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 following in the command line

```
cargo run --release --example gif
```

# MIT Licence

Copyright <2017, Kağan Kayal>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

## Traits

ExtractBitsFromIntegralTypes |
Defines a number of functions, which extract a range of bits from primitive numeric types (u8, u16, u32 and u64, i8, i16, i32 and i64) and return the result as one of the following types (u8, u16, u32 and u64, i8, i16, i32 and i64) E.g. the a.get_u8(5,3) function extracts the bits 5,6 and 7 of the variable a and returns the result as a u8 variable |

ExtractBitsFromVecU8 |
Defines a number of functions, which extract a range of bits from a Vec |

InsertBitsIntoIntegralTypes |
Defines a number of functions, which insert any integer value into any integer type (u8, u16, u32 and u64, i8, i16, i32 and i64) and return the result as the same type. |

SignedInfo |
A trait to find out if a varibale type is signed or unsigned for integer types. |

SingleBits |
Defines a set of functions to get, set and clear single bits |

TypeInfo |
A trait to get the data type as a string for a integer and floating point types. |

## Functions

n_required_bits_for_a_signed_int |
How many bits does it take to write a signed integer? |

n_required_bits_for_an_unsigned_int |
How many bits does it take to write an unsigned integer? |