bitsel is a header-only C++17 library provides a class bits
that represents a variable-size sequence of bits.
Inspired from std::bitset
and chisel3-like syntax of data representation and operations.
- C++17
- Header-only
- Dependency-free
- MIT License
#include <iostream>
#include "bitsel.hpp"
using namespace bitsel;
using namespace bitsel::literals;
int main()
{
// Create a 16-bit variable a which contains all '1's
bits a = bits::ones(16);
// Create a 32-bit variable b which contains 0xDEADBEEF
bits b = "0xDEADBEEF"_u(32_w);
// Create a 20-bit variable c which contains 0b101010
bits c{20, 0b101010};
// Create a 24-bit variable d which contains 0x101010
bits d = fill(3, 0x10_u());
// Create a variable e which is the concatenation of a, b, c and d
bits e = {a, b, c, d};
// Set 3th bit of e to true
e.set(3, true);
// Set 28th bit of e to 17th bit of e
e.set(28, e[17]);
// Create a variable f which is the slice from 40th bit to 8th bit of e
bits f = e(39, 8);
// Create a variable g which is the slice from 7th bit to 0th bit of e
bits g = e(7, 0);
// Set e to f xor g, the length of e is now max(39 - 8 + 1, 7 - 0 + 1) =
// 32-bit
e = f ^ g;
// Print h in binary, octal and hex representations
std::cout << "Binary: " << e.to_string(num_base::bin) << "\n"
<< "Octal: " << e.to_string(num_base::oct) << "\n"
<< "Hex: " << e.to_string(num_base::hex) << "\n";
}
You can refer example for more examples.
- Copy the file bitsel.hpp into your project.
- Done!