Bits.hpp

A generic bit-handler to replace vector<bool>, etc +additional bitwise logic features.

The Bits template allows the user to recreate the functionality of std::vector<bool>, array<bool>, std::bitset, and other such bit-handling classes.

This class stores an arbitrary number of bits in a set of “fields” (typically 32 bits or 64 bits per field, depending on which should be faster.) Individual bits can be extracted, -or- bitwise logic (including more complex bit magic) can be used on the groups of bits.

The template parameters are: DATA_T : How is memory managed? ZERO_LEFT : Should the index of zero be the left-most bit? (right-most if false)

Specializations are: BitVector : A replacement for std::vector<bool> (index 0 is on left) BitValue : Like BitVector, but index 0 is on the right StaticBitVector : Like Bitvector, but max size and fixed memory. StaticBitValue : Like BitValue, but max size and fixed memory. BitArray : A replacement for std::array<bool> (index 0 is on left) BitSet : A replacement for std::bitset (index 0 is on right)

In the case of replacements, the aim was for identical functionality, but many additional features, especially associated with bitwise logic operations.

Todo:

Most of the operators don’t check to make sure that both Bit groups are the same size. We should create versions (Intersection() and Union()?) that adjust sizes if needed.

Do small BitVector optimization. Currently we have number of bits (8 bytes) and a pointer to the memory for the bitset (another 8 bytes), but we could use those 16 bytes as 1 byte of size info followed by 15 bytes of bitset (120 bits!)

For large BitVectors we can use a factory to preserve/adjust bit info. That should be just as efficient than a reserve, but without the need to store extra in-class info.

Think about how iterators should work for Bit collections. It should probably go bit-by-bit, but there are very few circumstances where that would be useful. Going through the positions of all ones would be more useful, but perhaps less intuitive.

Note

Status: RELEASE

Note

Compile with -O3 and -msse4.2 for fast bit counting.

Typedefs

using BitVector = Bits<Bits_WatermarkData, true>
using BitValue = Bits<Bits_DynamicData, false>
template<size_t NUM_BITS>
using BitArray = Bits<Bits_FixedData<NUM_BITS>, true>
template<size_t NUM_BITS>
using BitSet = Bits<Bits_FixedData<NUM_BITS>, false>
template<size_t MAX_BITS>
using StaticBitVector = Bits<Bits_StaticData<MAX_BITS>, true>
template<size_t MAX_BITS>
using StaticBitValue = Bits<Bits_StaticData<MAX_BITS>, false>
template<typename DATA_T, bool ZERO_LEFT>
class Bits
#include <Bits.hpp>

A flexible base template to handle BitVector, BitArray, BitSet, & other combinations.

Template Parameters:
  • DATA_T – How is this Bits object allowed to change size?

  • ZERO_LEFT – Should the index of zero be the left-most bit? (right-most if false)

Public Functions

inline Bits(bool init_val = 0)

Default constructor; will build the default number of bits (often 0, but not always)

Parameters:

init_val – Initial value of all default bits.

Bits(size_t in_num_bits, bool init_val = false)

Build a new Bits with specified bit count and initialization (default 0)

Build a new Bits object with specified bit count and initialization (default 0)

template<typename T, typename std::enable_if<std::is_arithmetic<T>::value, int>::type = 0>
inline Bits(T in_num_bits, bool init_val = false)

Anything not otherwise defined for first argument, convert to size_t.

Bits(const Bits &in) = default

Copy constructor of existing bits object.

template<typename DATA2_T, bool ZERO_LEFT2>
Bits(const Bits<DATA2_T, ZERO_LEFT2> &in)

Constructor for other type of existing bits object.

Constructor from other type of Bits field.

Bits(this_t &&in) = default

Move constructor of existing bit field.

template<size_t NUM_BITS>
explicit Bits(const std::bitset<NUM_BITS> &bitset)

Constructor to generate a Bits from a std::bitset.

Bits(const std::string &bitstring)

Constructor to generate a Bits from a string of ‘0’s and ‘1’s.

inline Bits(const char *bitstring)

Constructor to generate a Bits from a literal string of ‘0’s and ‘1’s.

Bits(Random &random)

Constructor to generate a random set of bits in the default size.

Parameters:

random – Random number generator to use.

Bits(Random &random, const double p1)

Constructor to generate random Bits with provided prob of 1’s, default size.

Parameters:
  • random – Random number generator to use.

  • p1 – Probability of a bit being a one.

Bits(Random &random, const size_t target_ones)

Constructor to generate random Bits with specified # of ones, default size.

Constructor to generate random Bits with specified number of ones.

Parameters:
  • random – Random number generator to use.

  • target_ones – Number of ones to include in the Bits.

inline Bits(Random &random, const int target_ones)

Constructor to generate random Bits with specified # of ones, default size.

Parameters:
  • random – Random number generator to use.

  • target_ones – Number of ones to include in the Bits.

Bits(size_t in_num_bits, Random &random)

Constructor to generate a specified number of random Bits (with equal prob of 0 or 1).

Constructor to generate a random Bits (with equal prob of 0 or 1).

Bits(size_t in_num_bits, Random &random, const double p1)

Constructor to generate a random Bits with provided prob of 1’s.

Bits(size_t in_num_bits, Random &random, const size_t target_ones)

Constructor to generate a random Bits with provided number of 1’s.

inline Bits(size_t in_num_bits, Random &random, const int target_ones)

Constructor to generate a random Bits with provided number of 1’s.

template<typename T>
Bits(const std::initializer_list<T> l)

Initializer list constructor.

template<typename DATA2_T, bool ZERO_LEFT2>
Bits(const Bits<DATA2_T, ZERO_LEFT2> &in, size_t new_size)

Copy, but with a resize.

~Bits() = default

Destructor.

Bits &operator=(const Bits<DATA_T, ZERO_LEFT> &in) &

Copy assignment operator.

template<typename DATA2_T, bool ZERO_LEFT2>
Bits &operator=(const Bits<DATA2_T, ZERO_LEFT2> &in) &

Assignment operator for other Bits object.

Bits &operator=(Bits &&in) &

Move operator.

template<size_t NUM_BITS>
Bits &operator=(const std::bitset<NUM_BITS> &bitset) &

Assignment operator from a std::bitset.

Bits &operator=(const std::string &bitstring) &

Assignment operator from a string of ‘0’s and ‘1’s.

inline Bits &operator=(const char *bitstring) &

Assignment operator from a literal string of ‘0’s and ‘1’s.

template<typename DATA2_T, bool ZERO_LEFT2>
Bits &Import(const Bits<DATA2_T, ZERO_LEFT2> &from_bits, const size_t from_start_pos = 0, size_t max_copy_bits = MAX_SIZE_T)

Assignment from another Bits object without changing size.

template<typename OUT_T = Bits<Bits_DynamicData, ZERO_LEFT>>
OUT_T Export(size_t out_size, size_t start_bit = 0) const

Convert to a Bits of a different size.

Convert to a Bitset of a different size.

template<size_t NUM_BITS>
inline Bits<Bits_FixedData<NUM_BITS>, true> ExportArray(size_t start_bit = 0) const

Convert to a BitArray of a different size.

template<typename DATA2_T, bool ZERO_LEFT2>
Bits &Append(const Bits<DATA2_T, ZERO_LEFT2> &in_bits)

concatenate another Bits object on to the end of this one.

inline bool OK() const
inline constexpr auto GetSize() const

How many bits do we currently have?

inline constexpr auto GetNumBytes() const

How many bytes are in this Bits? (includes empty field space)

inline constexpr double GetNumStates() const

How many distinct values could be held in this Bits?

constexpr bool Get(size_t index) const

Retrieve the bit value from the specified index.

inline constexpr bool Has(size_t index) const

A safe version of Get() for indexing out of range. Useful for representing collections.

Bits &Set(size_t index, bool value = true)

Update the bit value at the specified index.

Bits &SetAll()

Set all bits to 1.

inline Bits &SetRange(size_t start, size_t stop, bool value = true)

Set a range of bits to value (default one): [start, stop)

Bits &Clear()

Set all bits to 0.

inline Bits &Clear(size_t index)

Set specific bit to 0.

inline Bits &Clear(const size_t start, const size_t stop)

Set bits to 0 in the range [start, stop)

inline bool operator[](size_t index) const

Const index operator &#8212; return the bit at the specified position.

inline BitProxy<Bits> operator[](size_t index)

Index operator; return proxy to bit at specified position usable as an lvalue.

inline Bits &Toggle()

Change every bit in the sequence.

Bits &Toggle(size_t index)

Change a specified bit to the opposite value.

inline Bits &Toggle(size_t start, size_t stop)

Flips all the bits in a range [start, end)

bool Any() const

Return true if ANY bits are set to 1, otherwise return false.

inline bool None() const

Return true if NO bits are set to 1, otherwise return false.

inline bool All() const

Return true if ALL bits are set to 1, otherwise return false.

inline Bits &Resize(size_t new_bits)

Resize this Bits object to have the specified number of bits (if allowed)

Bits &Randomize(Random &random)

Set all bits randomly, with a 50% probability of being a 0 or 1.

template<Random::Prob P>
Bits &RandomizeP(Random &random, const size_t start_pos = 0, size_t stop_pos = MAX_SIZE_T)

Set all bits randomly, with probability specified at compile time.

Bits &Randomize(Random &random, const double p, const size_t start_pos = 0, size_t stop_pos = MAX_SIZE_T)

Set all bits randomly, with a given probability of being a one.

Set all bits randomly, with a given probability of being on.

Bits &ChooseRandom(Random &random, const size_t target_ones, const size_t start_pos = 0, size_t stop_pos = MAX_SIZE_T)

Set all bits randomly, with a given number of ones.

Set all bits randomly, with a given number of them being on.

Bits &FlipRandom(Random &random, const double p, const size_t start_pos = 0, size_t stop_pos = MAX_SIZE_T)

Flip random bits with a given probability.

Bits &SetRandom(Random &random, const double p, const size_t start_pos = 0, size_t stop_pos = MAX_SIZE_T)

Set random bits with a given probability (does not check if already set.)

Bits &ClearRandom(Random &random, const double p, const size_t start_pos = 0, size_t stop_pos = MAX_SIZE_T)

Unset random bits with a given probability (does not check if already zero.)

Bits &FlipRandomCount(Random &random, const size_t target_bits)

Flip a specified number of random bits.

Bits &SetRandomCount(Random &random, const size_t target_bits)

Set a specified number of random bits (does not check if already set.)

Bits &ClearRandomCount(Random &random, const size_t target_bits)

Unset a specified number of random bits (does not check if already zero.)

template<typename DATA2_T, bool ZL2>
bool operator==(const Bits<DATA2_T, ZL2> &in) const

Compare two bits objects, even with different template arguments.

Test if two bit vectors are identical.

template<typename DATA2_T, bool ZL2>
inline bool operator!=(const Bits<DATA2_T, ZL2> &in) const
template<typename DATA2_T, bool ZL2>
bool operator<(const Bits<DATA2_T, ZL2> &in) const

Compare the would-be numerical values of two bit vectors.

template<typename DATA2_T, bool ZL2>
inline bool operator>(const Bits<DATA2_T, ZL2> &in) const
template<typename DATA2_T, bool ZL2>
inline bool operator<=(const Bits<DATA2_T, ZL2> &in) const
template<typename DATA2_T, bool ZL2>
inline bool operator>=(const Bits<DATA2_T, ZL2> &in) const
template<typename T>
operator vector<T>()

Automatically convert Bits to other vector types.

Automatically convert Bits object to other vector types.

inline explicit operator bool() const

Casting a bit array to bool identifies if ANY bits are set to 1.

uint8_t GetByte(size_t index) const

Retrieve the byte at the specified byte index.

inline auto GetBytes() const

et a read-only view into the internal array used by Bits.

Returns:

Read-only span of Bits’s bytes.

inline std::span<field_t> FieldSpan()

Return a span with all fields in order.

inline std::span<const field_t> FieldSpan() const

Return a const span with all fields in order.

inline size_t NumFields() const
inline auto FieldPtr()

Return a pointer to the set of fields.

inline auto FieldPtr() const

Return a const pointer to the set of fields.

inline Ptr<const unsigned char> RawBytes() const

Get a read-only pointer to the internal array used by Bits. (note that bits are NOT in order at the byte level!)

Returns:

Read-only pointer to Bits’ bytes.

void SetByte(size_t index, uint8_t value)

Update the byte at the specified byte index.

double GetValue() const

Get overall base-2 value of this Bits, returning as a double.

Get the overall value of this BitSet, using a uint encoding, but including all bits and returning the value as a double.

template<typename T>
T GetValueAtIndex(const size_t index) const

Get specified type at a given index (in steps of that type size)

inline uint8_t GetUInt8(size_t index) const

Retrieve the 8-bit uint from the specified uint index.

inline uint16_t GetUInt16(size_t index) const

Retrieve the 16-bit uint from the specified uint index.

inline uint32_t GetUInt32(size_t index) const

Retrieve the 32-bit uint from the specified uint index.

inline uint64_t GetUInt64(size_t index) const

Retrieve the 64-bit uint from the specified uint index.

inline uint32_t GetUInt(size_t index) const

By default, retrieve the 32-bit uint from the specified uint index.

template<typename T>
Bits &SetValueAtIndex(const size_t index, T value)

Set specified type at a given index (in steps of that type size)

inline void SetUInt8(const size_t index, uint8_t value)

Update the 8-bit uint at the specified uint index.

inline void SetUInt16(const size_t index, uint16_t value)

Update the 16-bit uint at the specified uint index.

inline void SetUInt32(const size_t index, uint32_t value)

Update the 32-bit uint at the specified uint index.

inline void SetUInt64(const size_t index, uint64_t value)

Update the 64-bit uint at the specified uint index.

inline void SetUInt(const size_t index, uint32_t value)

By default, update the 32-bit uint at the specified uint index.

template<typename T>
T GetValueAtBit(const size_t index) const

@briefGet specified type starting at a given BIT position.

Get the specified type starting from a given BIT position.

inline uint8_t GetUInt8AtBit(size_t index) const

Retrieve the 8-bit uint from the specified uint index.

inline uint16_t GetUInt16AtBit(size_t index) const

Retrieve the 16-bit uint from the specified uint index.

inline uint32_t GetUInt32AtBit(size_t index) const

Retrieve the 32-bit uint from the specified uint index.

inline uint64_t GetUInt64AtBit(size_t index) const

Retrieve the 64-bit uint from the specified uint index.

inline uint32_t GetUIntAtBit(size_t index) const

By default, retrieve the 32-bit uint from the specified uint index.

template<typename T>
Bits &SetValueAtBit(const size_t index, T value)
inline void SetUInt8AtBit(const size_t index, uint8_t value)

Update the 8-bit uint at the specified uint index.

inline void SetUInt16AtBit(const size_t index, uint16_t value)

Update the 16-bit uint at the specified uint index.

inline void SetUInt32AtBit(const size_t index, uint32_t value)

Update the 32-bit uint at the specified uint index.

inline void SetUInt64AtBit(const size_t index, uint64_t value)

Update the 64-bit uint at the specified uint index.

inline void SetUIntAtBit(const size_t index, uint32_t value)

By default, update the 32-bit uint at the specified uint index.

std::size_t Hash(size_t start_field = 0) const

A simple hash function for bit vectors.

constexpr size_t CountOnes() const

Count the number of ones in Bits.

constexpr size_t CountOnes(size_t start, size_t end) const

Count the number of ones in a range within Bits. [start, end)

Count the number of ones in a specified range of Bits.

constexpr size_t CountOnes_Sparse() const

Faster counting of ones for very sparse bit vectors.

inline constexpr size_t CountZeros() const

Count the number of zeros in Bits.

bool PopBack()

Pop the last bit in the vector.

Pop the last bit in the vector.

Returns:

value of the popped bit.

Returns:

value of the popped bit.

void PushBack(const bool bit = true, const size_t num = 1)

Push given bit(s) onto the back of a vector.

Push given bit(s) onto the back of a vector.

Parameters:
  • bit – value of bit to be pushed.

  • num – number of bits to be pushed.

  • bit – value of bit to be pushed.

  • num – number of bits to be pushed.

void PushFront(const bool bit = true, const size_t num = 1)

Push given bit(s) onto the front of a vector.

Push given bit(s) onto the front of a vector.

Parameters:
  • bit – value of bit to be pushed.

  • num – number of bits to be pushed.

  • bit – value of bit to be pushed.

  • num – number of bits to be pushed.

void Insert(const size_t index, const bool val = true, const size_t num = 1)

Insert bit(s) into any index of vector using bit magic. Blog post on implementation reasoning: https://devolab.org/?p=2249.

Insert bit(s) into any index of vector using bit magic. Blog post on implementation reasoning: https://devolab.org/?p=2249

Parameters:
  • index – location to insert bit(s).

  • val – value of bit(s) to insert.

  • num – number of bits to insert, default 1.

  • index – location to insert bit(s).

  • val – value of bit(s) to insert (default true)

  • num – number of bits to insert, default 1.

void Delete(const size_t index, const size_t num = 1)

Delete bits from any index in a vector.

Delete bits from any index in a vector.

Parameters:
  • index – location to delete bit(s).

  • num – number of bits to delete, default 1.

  • index – location to delete bit(s).

  • num – number of bits to delete, default 1.

int FindOne() const

Return the position of the first one; return -1 if no ones in vector.

int FindZero() const

Return the position of the first zero; return -1 if no zeroes in vector.

Return the position of the first zero; return -1 if no zeros in vector.

inline int FindBit() const

Deprecated: Return the position of the first one; return -1 if no ones in vector.

int FindOne(const size_t start_pos) const

Return the position of the first one after start_pos (or -1 if none) You can loop through all 1-bit positions of Bits object “bits” with:

for (int pos = bits.FindOne(); pos >= 0; pos = bits.FindOne(pos+1)) { … }

Return the position of the first one after start_pos; return -1 if no ones in vector. You can loop through all 1-bit positions in “bits” with:

for (int pos = bits.FindOne(); pos >= 0; pos = bits.FindOne(pos+1)) { … }

int FindZero(const size_t start_pos) const

Return the position of the first zero after start_pos (or -1 if none) You can loop through all 0-bit positions of Bits object “bits” with:

for (int pos = bits.FindZero(); pos >= 0; pos = bits.FindZero(pos+1)) { … }

Return the position of the first zero after start_pos; return -1 if no zeroes in vector. You can loop through all 0-bit positions in “bits” with:

for (int pos = bits.FindZero(); pos >= 0; pos = bits.FindZero(pos+1)) { … }

inline int FindOne(int start_pos) const

Special version of FindOne takes int; common way to call.

inline int FindZero(int start_pos) const

Special version of FindZero takes int; common way to call.

int FindBit(const size_t start_pos) const

Deprecated version of FindOne().

int FindMaxOne() const

Find the most-significant set-bit.

int PopOne()

Return the position of the first one and change it to a zero. Return -1 if none.

Return the position of the first one and change it to a zero. Return -1 if no ones.

inline int PopBit()

Deprecated version of PopOne().

vector<size_t> GetOnes() const

Return vector of positions of all ones.

Return positions of all ones.

template<typename T>
vector<T> &GetOnes(vector<T> &out_vals) const

Collect positions of ones in the provided vector (allows id type choice)

Return positions of all ones using a specified type.

size_t LongestSegmentOnes() const

Find the length of the longest continuous series of ones.

vector<Range<size_t>> GetRanges() const

Find ids of all groups of ones.

Returns:

A vector of ranges that identify all ids of ones.

bool HasOverlap(const Bits &in) const

Return true if any ones are in common with another Bits.

Return true if any ones are in common with another Bits object.

inline char GetAsChar(size_t id) const

Convert a specified bit to a character.

std::string ToString() const

Convert this Bits to a vector string [index 0 based on ZERO_LEFT].

Convert this Bits object to a vector string [0 index on left].

std::string ToArrayString() const

Convert this Bits to an array-based string [index 0 on left].

Convert this Bits object to a vector string [0 index on left].

std::string ToBinaryString() const

Convert this Bits to a numerical string [index 0 on right].

Convert this Bits object to a numerical string [0 index on right].

std::string ToIDString(const std::string &spacer = " ") const

Convert this Bits to a series of IDs.

Convert this Bits object to a series of IDs.

std::string ToRangeString(const std::string &spacer = ",", const std::string &ranger = "-") const

Convert this Bits to a series of IDs with ranges condensed.

Convert this Bits object to a series of IDs with ranges condensed.

inline void Print(std::ostream &out = std::cout) const

Regular print function (from least significant bit to most)

inline void PrintBinary(std::ostream &out = std::cout) const

Numerical print function (from most significant bit to least)

inline void PrintArray(std::ostream &out = std::cout) const

Print from smallest bit position to largest.

void PrintFields(std::ostream &out = std::cout, const std::string &spacer = " ") const

Print a space between each field (or other provided spacer)

void PrintDebug(std::ostream &out = std::cout, const std::string &label = "") const

Print out details about the internals of Bits.

Print a space between each field (or other provided spacer)

void PrintOneIDs(std::ostream &out = std::cout, const std::string &spacer = " ") const

Print the positions of all one bits, spaces are the default separator.

void PrintAsRange(std::ostream &out = std::cout, const std::string &spacer = ",", const std::string &ranger = "-") const

Print the ones in a range format. E.g., 2-5,7,10-15.

Bits &NOT_SELF()

Perform a Boolean NOT with this Bits, store result here, and return this object.

Perform a Boolean NOT with this Bits object, store result here, and return this object.

Bits &AND_SELF(const Bits &bits2)

Perform a Boolean AND with this Bits, store result here, and return this object.

Perform a Boolean AND with this Bits object, store result here, and return this object.

Bits &OR_SELF(const Bits &bits2)

Perform a Boolean OR with this Bits, store result here, and return this object.

Perform a Boolean OR with this Bits object, store result here, and return this object.

Bits &NAND_SELF(const Bits &bits2)

Perform a Boolean NAND with this Bits, store result here, and return this object.

Perform a Boolean NAND with this Bits object, store result here, and return this object.

Bits &NOR_SELF(const Bits &bits2)

Perform a Boolean NOR with this Bits, store result here, and return this object.

Perform a Boolean NOR with this Bits object, store result here, and return this object.

Bits &XOR_SELF(const Bits &bits2)

Perform a Boolean XOR with this Bits, store result here, and return this object.

Perform a Boolean XOR with this Bits object, store result here, and return this object.

Bits &EQU_SELF(const Bits &bits2)

Perform a Boolean EQU with this Bits, store result here, and return this object.

Perform a Boolean EQU with this Bits object, store result here, and return this object.

inline Bits NOT() const

Perform a Boolean NOT on this Bits and return the result.

inline Bits AND(const Bits &bits2) const

Perform a Boolean AND on this Bits and return the result.

inline Bits OR(const Bits &bits2) const

Perform a Boolean OR on this Bits and return the result.

inline Bits NAND(const Bits &bits2) const

Perform a Boolean NAND on this Bits and return the result.

inline Bits NOR(const Bits &bits2) const

Perform a Boolean NOR on this Bits and return the result.

inline Bits XOR(const Bits &bits2) const

Perform a Boolean XOR on this Bits and return the result.

inline Bits EQU(const Bits &bits2) const

Perform a Boolean EQU on this Bits and return the result.

Bits SHIFT(const int shift_size) const

Positive shifts left and negative right (0 does nothing); return result.

Positive shifts go left and negative go right (0 does nothing); return result.

Bits &SHIFT_SELF(const int shift_size)

Positive shifts left and negative right; store result here, and return *this.

Positive shifts go left and negative go right; store result here, and return this object.

Bits &REVERSE_SELF()

Reverse the order of bits in the bitset.

Bits REVERSE() const

Reverse order of bits in the bitset.

Bits ROTATE(const int rotate_size) const

Positive rotates right and negative goes left; return result.

Positive rotates go left and negative rotates go left (0 does nothing); return result.

Bits &ROTATE_SELF(const int rotate_size)

Positive rotates right and negative goes left; store here, and return *this.

Positive rotates go right and negative rotates go left (0 does nothing); store result here, and return this object.

Bits ADD(const Bits &set2) const

Sums two Bits objects (following uint rules); returns result.

Addition of two Bitsets. Wraps if it overflows. Returns result.

Bits &ADD_SELF(const Bits &set2)

Sums another Bits object onto this one (following uint rules); returns *this.

Addition of two Bitsets. Wraps if it overflows. Returns this object.

Bits SUB(const Bits &set2) const

Subtracts on Bits object from another (following uint rules); returns result.

Subtraction of two Bitsets. Wraps around if it underflows. Returns result.

Bits &SUB_SELF(const Bits &set2)

Subtracts another Bits object from this one (following uint rules); returns *this.

Subtraction of two Bitsets. Wraps if it underflows. Returns this object.

inline Bits operator~() const

Operator bitwise NOT…

inline Bits operator&(const Bits &ar2) const

Operator bitwise AND…

inline Bits operator|(const Bits &ar2) const

Operator bitwise OR…

inline Bits operator^(const Bits &ar2) const

Operator bitwise XOR…

inline Bits operator<<(const size_t shift_size) const

Operator shift left…

inline Bits operator>>(const size_t shift_size) const

Operator shift right…

inline Bits &operator&=(const Bits &ar2)

Compound operator bitwise AND…

inline Bits &operator|=(const Bits &ar2)

Compound operator bitwise OR…

inline Bits &operator^=(const Bits &ar2)

Compound operator bitwise XOR…

inline Bits &operator<<=(const size_t shift_size)

Compound operator for shift left…

inline Bits &operator>>=(const size_t shift_size)

Compound operator for shift right…

inline Bits operator+(const Bits &ar2) const

Operator plus…

inline Bits operator-(const Bits &ar2) const

Operator minus…

inline const Bits &operator+=(const Bits &ar2)

Compound operator plus…

inline const Bits &operator-=(const Bits &ar2)

Compound operator minus…

template<class Archive>
inline void serialize(Archive &ar)

Setup this bits object so that it can be stored in an archive and re-loaded.

inline constexpr size_t size() const
inline auto &at(size_t pos)
inline auto at(size_t pos) const
inline auto &front()
inline auto front() const
inline auto &back()
inline auto back() const
inline void resize(std::size_t new_size)
inline void push_back(const bool bit = true, const size_t num = 1)
inline void pop_back()
inline constexpr bool all() const
inline constexpr bool any() const
inline constexpr bool none() const
inline constexpr size_t count() const
inline Bits &flip()
inline Bits &flip(size_t pos)
inline Bits &flip(size_t start, size_t end)
inline void reset()
inline void reset(size_t id)
inline void set()
inline void set(size_t id)
inline bool test(size_t index) const
inline auto data()
inline auto data() const
template<typename DATA_T, bool ZERO_LEFT>
Bits<DATA_T, ZERO_LEFT> &operator=(const Bits<DATA_T, ZERO_LEFT> &in) &

Copy assignment operator.

template<typename DATA2_T, bool ZERO_LEFT2>
Bits<DATA_T, ZERO_LEFT> &operator=(const Bits<DATA2_T, ZERO_LEFT2> &in) &

Other Bits assignment operator.

template<typename DATA_T, bool ZERO_LEFT>
Bits<DATA_T, ZERO_LEFT> &operator=(Bits<DATA_T, ZERO_LEFT> &&in) &

Move operator.

template<size_t NUM_BITS>
Bits<DATA_T, ZERO_LEFT> &operator=(const std::bitset<NUM_BITS> &bitset) &

Assignment operator from a std::bitset.

template<typename DATA_T, bool ZERO_LEFT>
Bits<DATA_T, ZERO_LEFT> &operator=(const std::string &bitstring) &

Assignment operator from a string of ‘0’s and ‘1’s.

template<typename DATA2_T, bool ZERO_LEFT2>
Bits<DATA_T, ZERO_LEFT> &Import(const Bits<DATA2_T, ZERO_LEFT2> &from_bits, const size_t from_start_pos, size_t max_copy_bits)

Assign from a Bits object of a different size, while keeping current size. If there are too many bits being imported, extras are cut off. If there are fewer bits, the remainder are zero’d out (up to max_copy_bits)

template<typename DATA2_T, bool ZERO_LEFT2>
Bits<DATA_T, ZERO_LEFT> &Append(const Bits<DATA2_T, ZERO_LEFT2> &in_bits)

Concatenate another Bits object on to the end of this one.

template<Random::Prob P>
Bits<DATA_T, ZERO_LEFT> &RandomizeP(Random &random, const size_t start_pos, size_t stop_pos)

Set all bits randomly, with probability specified at compile time.

template<typename T>
Bits<DATA_T, ZERO_LEFT> &SetValueAtIndex(const size_t index, T in_value)

Set specified type at a given index (in steps of that type size)

template<typename T>
Bits<DATA_T, ZERO_LEFT> &SetValueAtBit(const size_t index, T value)

Set the specified type starting from a given BIT position.

Public Static Functions

static inline constexpr auto GetCTSize()

How many bits are locked in a compile time?

Private Types

using this_t = Bits<DATA_T, ZERO_LEFT>
using field_t = bits_field_t

Private Functions

inline constexpr field_t MaskField(size_t mask_size) const
inline constexpr field_t MaskField(size_t mask_size, size_t offset) const
void RawCopy(const Ptr<const field_t> from, size_t copy_fields = MAX_SIZE_T)
template<typename DATA2_T, bool ZERO_LEFT2>
inline void RawCopy(const Bits<DATA2_T, ZERO_LEFT2> &in_bits)
constexpr void RawMove(const size_t from_start, const size_t from_stop, const size_t to)
inline Ptr<unsigned char> BytePtr()
inline Ptr<const unsigned char> BytePtr() const
inline constexpr this_t &ClearExcessBits()
template<typename FUN_T>
Bits<DATA_T, ZERO_LEFT> &ApplyRange(const FUN_T &fun, size_t start, size_t stop)
constexpr void ShiftLeft(const size_t shift_size)
constexpr void ShiftRight(const size_t shift_size, bool raw = false)
constexpr void ROTL_SELF(const size_t shift_size_raw)

Helper: call ROTATE with negative number instead.

Helper: call ROTATE with negative number.

constexpr void ROTR_SELF(const size_t shift_size_raw)

Helper for calling ROTATE with positive number.

Private Members

DATA_T _data

Private Static Functions

static inline constexpr size_t FieldID(const size_t index)
static inline constexpr size_t FieldPos(const size_t index)
static inline constexpr size_t Byte2Field(const size_t index)
static inline constexpr size_t Byte2FieldPos(const size_t index)

Private Static Attributes

static constexpr size_t FIELD_BITS = NUM_FIELD_BITS
static constexpr size_t FIELD_LOG2 = static_cast<size_t>(Log2(FIELD_BITS))
static constexpr field_t FIELD_LOG2_MASK = MaskLow<field_t>(FIELD_LOG2)
static constexpr field_t FIELD_0 = (field_t)0

All bits in a field set to 0.

static constexpr field_t FIELD_1 = (field_t)1

Least significant bit set to 1.

static constexpr field_t FIELD_255 = (field_t)255

Least significant 8 bits set to 1.

static constexpr field_t FIELD_ALL = ~FIELD_0

All bits in a field set to 1.

Friends

inline friend std::ostream &operator<<(std::ostream &out, const Bits &bits)

Overload ostream operator to return Print.

namespace std

STL namespace.

template<typename DATA_T, bool ZERO_LEFT>
struct hash<Bits<DATA_T, ZERO_LEFT>>
#include <Bits.hpp>

Hash function to allow Bits objects to be used with maps and sets (must be in std).

Public Functions

inline std::size_t operator()(const Bits<DATA_T, ZERO_LEFT> &bits) const