A C++ Bit Vector Arithmetic Library

FULL CODE LINK

bsim is a single header library that provides arbitrary length bit vectors. The library also includes a bit vector wrapper for unsigned integers.

There are many bit vector libraries (like std::bitset) that provide bit vectors to represent sets. Unlike these libraries bsim was designed with hardware simulation in mind. This library supports logical operations as well as signed and unsigned arithmetic.

Supported Operations

  1. Bitwise logical operators: and, or, nand, nor, xor, not
  2. Bitwise comparisons: equal and not equal
  3. Unsigned integer arithmetic: add, subtract, and multiply
  4. Unsigned integer comparisons: greater, less, greater than or equal, less than or equal
  5. Signed integer arithmetic: add, subtract, and multiply

I am currently working on adding support for signed and unsigned division and left and right shifts.

Native Instruction Use for Small Bit Vectors

bsim supports operating on arbitrary length bit vectors, but for operations on bit vectors with less than 65 bits the library uses conditional compilation to native instructions rather than arbitrary length algorithms.

Use of native operations comes at a slight storage cost. For example a 33 bit vector must be stored as 64 bits in order to use 64 bit operations without changing bits in memory beyond the object.

Installation

Just copy the file src/bit_vector.h in to your project.

Leave a Reply

Your email address will not be published. Required fields are marked *