#bits #macros #bitmask #proc-macro #bit-manipulation

macro bit_seq

A procedural macro for creating bit sequences

5 releases

0.2.1 Jun 12, 2023
0.2.0 Jun 5, 2023
0.1.2 Jun 3, 2023
0.1.1 Jun 3, 2023
0.1.0 Jun 3, 2023

#729 in Encoding

32 downloads per month

MIT license

189 lines


GitHub Workflow Status Crates.io docs.rs

bit_seq is a convenient Rust crate that provides a procedural macro for creating bit sequences. This crate simplifies the generation of bit sequences, increasing readability and reducing the potential for errors. Bit sequences can be specified directly, via hex values, or through identifiers or integers with a specific length. It is particularly useful in systems programming and lower-level hardware or protocol interfacing where bit manipulation is common.


  • Generate bit sequences using simple syntax
  • Specify bit sequences directly or via hex values
  • Use identifiers or integers to define bit sequence with a specific length
  • Interpolate outer variables in length expressions
  • Compiles to common bit manipulation operations, so using this crate does not add runtime overhead


First, add the following in you project cmd-line:

cargo add bit_seq

Then import the crate in your Rust file:

use bit_seq::bseq;

Here are some examples of how to use the bseq macro:

// Direct raw bit sequence
let t = bseq!(0110 01 0 1);
assert_eq!(t, 0b0110_01_0_1);

// Using hex values
let t = bseq!(01 0x1f);
assert_eq!(t, 0b01_0001_1111);

// Using value length expression
let t = bseq!(3:1 0 0xf:2);
assert_eq!(t, 0b1_0_11);

// Using variable length expression
let var = 0xf;
let t = bseq!(10 var:2);
assert_eq!(t, 0b10_11);

// Using mixed variable types
let var_64: u64 = 0xf;
let var_16: u16 = 0xf;
let t = bseq_8!(var_16:4 var_64:4);
assert_eq!(t, 0xff);

// Using unary operators 
assert_eq!(bseq!(!0:6), 0b111111);


You can view the full API documentation here.


Contributions to bit_seq are welcome! Please submit a pull request or create an issue on the GitHub page.


This project is licensed under the MIT license.


~19K SLoC