Nebula
Loading...
Searching...
No Matches
Util::BitField< NUMBITS > Class Template Reference

#include <bitfield.h>

Detailed Description

template<unsigned int NUMBITS>
class Util::BitField< NUMBITS >

Implements large bit field.

The bit field occupies as little memory as possible, based on how many bits are required per the template parameter.

Will occupy 8, 16, 32 or 64 bits, or a multiple of 64 bits for larger fields.

Classes

struct  BitType
 
struct  BitType< 16 >
 
struct  BitType< 32 >
 
struct  BitType< 64 >
 

Public Member Functions

 BitField ()
 constructor
 
constexpr BitField (std::initializer_list< unsigned int > list)
 constructs a bitfield based on multiple values
 
 BitField (const BitField< NUMBITS > &rhs)=default
 copy constructor
 
constexpr bool operator== (const BitField< NUMBITS > &rhs) const
 equality operator
 
constexpr bool operator!= (const BitField< NUMBITS > &rhs) const
 inequality operator
 
constexpr bool IsSet (const uint64_t bitIndex) const
 Check if single bit is set.
 
template<uint64_t bitIndex>
constexpr bool IsSet () const
 Check if single bit is set.
 
void Clear ()
 clear content
 
bool IsNull () const
 return true if all bits are 0
 
constexpr void SetBit (const uint64_t bitIndex)
 set a bit by index
 
constexpr void SetBitIf (const uint64_t bitIndex, uint64_t cond)
 set a bit by index. Multiplies the bit with cond before OR-ing which means it won't set the bit if mul is 0, which makes it branchless.
 
template<uint64_t bitIndex>
constexpr void SetBit ()
 set a bit by index
 
void ClearBit (const uint64_t bitIndex)
 clear a bit by index
 
bool SectionIsNull (uint64_t section) const
 Check if a section of the bitfield is set.
 

Static Public Member Functions

static constexpr BitField< NUMBITS > Or (const BitField< NUMBITS > &b0, const BitField< NUMBITS > &b1)
 set bitfield to OR combination
 
static constexpr BitField< NUMBITS > And (const BitField< NUMBITS > &b0, const BitField< NUMBITS > &b1)
 set bitfield to AND combination
 

Private Types

using TYPE = typename BitType<BASE>::T
 

Private Attributes

TYPE bits [size]
 

Static Private Attributes

static constexpr uint64_t BASE = NUMBITS > 32 ? 64 : NUMBITS > 16 ? 32 : NUMBITS > 8 ? 16 : 8
 
static const int size = ((NUMBITS + BASE - 1) / BASE)
 

Member Typedef Documentation

◆ TYPE

template<unsigned int NUMBITS>
using Util::BitField< NUMBITS >::TYPE = typename BitType<BASE>::T
private

Constructor & Destructor Documentation

◆ BitField() [1/3]

template<unsigned int NUMBITS>
Util::BitField< NUMBITS >::BitField ( )

constructor

◆ BitField() [2/3]

template<unsigned int NUMBITS>
Util::BitField< NUMBITS >::BitField ( std::initializer_list< unsigned int > list)
constexpr

constructs a bitfield based on multiple values

◆ BitField() [3/3]

template<unsigned int NUMBITS>
Util::BitField< NUMBITS >::BitField ( const BitField< NUMBITS > & rhs)
default

copy constructor

Member Function Documentation

◆ And()

template<unsigned int NUMBITS>
BitField< NUMBITS > Util::BitField< NUMBITS >::And ( const BitField< NUMBITS > & b0,
const BitField< NUMBITS > & b1 )
staticconstexpr

set bitfield to AND combination

◆ Clear()

template<unsigned int NUMBITS>
void Util::BitField< NUMBITS >::Clear ( )

clear content

◆ ClearBit()

template<unsigned int NUMBITS>
void Util::BitField< NUMBITS >::ClearBit ( const uint64_t bitIndex)

clear a bit by index

◆ IsNull()

template<unsigned int NUMBITS>
bool Util::BitField< NUMBITS >::IsNull ( ) const

return true if all bits are 0

◆ IsSet() [1/2]

template<unsigned int NUMBITS>
template<uint64_t bitIndex>
bool Util::BitField< NUMBITS >::IsSet ( ) const
constexpr

Check if single bit is set.

◆ IsSet() [2/2]

template<unsigned int NUMBITS>
bool Util::BitField< NUMBITS >::IsSet ( const uint64_t bitIndex) const
constexpr

Check if single bit is set.

◆ operator!=()

template<unsigned int NUMBITS>
bool Util::BitField< NUMBITS >::operator!= ( const BitField< NUMBITS > & rhs) const
constexpr

inequality operator

◆ operator==()

template<unsigned int NUMBITS>
bool Util::BitField< NUMBITS >::operator== ( const BitField< NUMBITS > & rhs) const
constexpr

equality operator

◆ Or()

template<unsigned int NUMBITS>
BitField< NUMBITS > Util::BitField< NUMBITS >::Or ( const BitField< NUMBITS > & b0,
const BitField< NUMBITS > & b1 )
staticconstexpr

set bitfield to OR combination

◆ SectionIsNull()

template<unsigned int NUMBITS>
bool Util::BitField< NUMBITS >::SectionIsNull ( uint64_t section) const

Check if a section of the bitfield is set.

The section size depends on the size of the bit field. If the bitfield is 8, 16 or 32 bits large, the section size is the same size as the bitfield, thus there's only one section. If the bitfield is 64 bits or larger, the section size is 64 bits per section.

◆ SetBit() [1/2]

template<unsigned int NUMBITS>
template<uint64_t i>
void Util::BitField< NUMBITS >::SetBit ( )
constexpr

set a bit by index

◆ SetBit() [2/2]

template<unsigned int NUMBITS>
void Util::BitField< NUMBITS >::SetBit ( const uint64_t bitIndex)
constexpr

set a bit by index

◆ SetBitIf()

template<unsigned int NUMBITS>
void Util::BitField< NUMBITS >::SetBitIf ( const uint64_t bitIndex,
uint64_t cond )
constexpr

set a bit by index. Multiplies the bit with cond before OR-ing which means it won't set the bit if mul is 0, which makes it branchless.

Member Data Documentation

◆ BASE

template<unsigned int NUMBITS>
uint64_t Util::BitField< NUMBITS >::BASE = NUMBITS > 32 ? 64 : NUMBITS > 16 ? 32 : NUMBITS > 8 ? 16 : 8
staticconstexprprivate

◆ bits

template<unsigned int NUMBITS>
TYPE Util::BitField< NUMBITS >::bits[size]
private

◆ size

template<unsigned int NUMBITS>
const int Util::BitField< NUMBITS >::size = ((NUMBITS + BASE - 1) / BASE)
staticprivate

The documentation for this class was generated from the following file: