A C++ Bit Vector Arithmetic Library

FULL CODE LINK

bsim is a header only library that provides arbitrary length bit vectors.

There are many bit vector libraries designed for representing sets, for example std::bitset. Unlike these libraries bsim was designed with hardware simulation in mind. bsim supports bit operations such as "xor", "and", and "or" as well as unsigned and twos complement arithmetic and comparisons.

Bit Vector Classes

bsim supports both static and dynamic bit vectors. Static bit vectors, like those provided in std::bitset, are a class template with a template argument for the length of the bit vector. They are useful for applications where the length of a bit vector is known at compile time.

Dynamic bit vectors support the same operations, but they are used when the length of the bit vector is not known at compile time.

Supported Operations

  1. Bitwise logical operators: and, or, nand, nor, xor, not
  2. Bitwise comparisons: equal and not equal
  3. Reduction operations: "andr", "orr", "xorr"
  4. Shifts: left shifts (shl), arithmetic right shifts (ashr), and logical right shifts (lshr).
  5. Unsigned integer arithmetic: add, subtract, multiply, and divide
  6. Unsigned integer comparisons: greater, less, greater than or equal, less than or equal
  7. Signed integer arithmetic: add, subtract, and multiply
  8. Signed integer comparisons: greater, less, greater than or equal, less than or equal

I am currently working on adding support for signed division as well as signed and unsigned remainder operations.

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

If you want to use static bit vectors then copy the file src/bit_vector.h into your project.

If you want to use dynamic bit vectors then use src/dynamic_bit_vector.h instead.

Leave a Reply

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