bitset_utils.hpp

A set of simple functions to manipulate bitsets.

Note

Status: BETA

Typedefs

using bits_field_t = size_t

Use size_t as the default bits field type.

Functions

static constexpr size_t NumBitFields(size_t num_bits) noexcept

Convert a bit count to the number of fields needed to store them.

static std::string BitFieldToString(bits_field_t field)

Convert a single bit field to a string.

Parameters:

field – A single bit field to convert to a string.

static std::string BitFieldsToString(Ptr<bits_field_t> bits, size_t count)

Convert a series of bit fields to a string.

Parameters:

field – A single bit field to convert to a string.

template<int NUM_BITS>
constexpr uint32_t UIntMaskFirst()

Create a series of a specified number of ones (at compile time) in a uint.

template<>
constexpr uint32_t UIntMaskFirst<0>()

Create an empty bit mask (all zeros)

template<typename T>
inline constexpr size_t count_bits(T val)

Count the number of bits in an unsigned integer.

template<typename T>
inline constexpr size_t find_bit(const T val)

Return the position of the first one bit.

template<typename T>
inline constexpr size_t find_last_bit(T val)

Return the position of the first one bit.

template<typename T>
inline size_t pop_bit(T &val)

Return the position of the first one bit AND REMOVE IT.

template<typename TYPE = size_t>
static constexpr TYPE MaskLow(std::size_t num_bits)

Quick bit-mask generator for low bits.

template<typename TYPE = size_t>
static constexpr TYPE MaskHigh(std::size_t num_bits)

Quick bit-mask generator for high bits.

template<typename TYPE = size_t>
static constexpr TYPE MaskUsed(TYPE val)
template<typename T>
constexpr T ReverseBits(T in)
template<typename T>
constexpr T RotateBitsLeft(T in, size_t rotate_size = 1)
template<typename T>
constexpr T RotateBitsLeft(T in, size_t rotate_size, size_t bit_count)
template<typename T>
constexpr T RotateBitsRight(T in, size_t rotate_size = 1)
template<typename T>
constexpr T RotateBitsRight(T in, size_t rotate_size, size_t bit_count)
static size_t CountBits(const std::string &bitstring)

Count the number of bits (‘0’ or ‘1’) found in a string.

Variables

static constexpr size_t NUM_FIELD_BITS = sizeof(bits_field_t) * 8

Track the number of bits in a single bit field.

constexpr size_t ByteCount[256] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8}

How many bits are set to one in each possible byte?