Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
lampda::common::BitSet< N > Class Template Reference

A compact, zero-allocation bitset for compile-time sized bit arrays. More...

#include <bitset.h>

Classes

struct  reference
 Non-const operator[]: returns a reference-like proxy that allows set/reset. More...
 

Public Types

using storage_t = uint32_t
 

Public Member Functions

constexpr BitSet ()
 Default constructor: all bits set to zero.
 
constexpr BitSet (storage_t value)
 Construct with a single storage value repeated for all words.
 
void set () noexcept
 Set all bits to 1.
 
void reset () noexcept
 Set all bits to 0.
 
void set (uint16_t pos) noexcept
 Set the bit at position pos to 1.
 
void set (uint16_t pos, bool value) noexcept
 Set the bit at position pos to the given value.
 
void reset (uint16_t pos) noexcept
 Clear the bit at position pos.
 
void flip (uint16_t pos) noexcept
 Flip (invert) the bit at position pos.
 
constexpr bool test (uint16_t pos) const noexcept
 Get the value of the bit at position pos.
 
constexpr bool operator[] (uint16_t pos) const noexcept
 Alias for test(pos)
 
reference operator[] (uint16_t pos) noexcept
 Non-const operator[] returning a proxy for bit manipulation.
 
constexpr uint16_t count () const noexcept
 Count the number of bits set to 1.
 
constexpr bool any () const noexcept
 Return true if any bit is set.
 
constexpr bool all () const noexcept
 Return true if all bits are set.
 
constexpr bool none () const noexcept
 Return true if no bits are set.
 
void flip () noexcept
 Flip all bits.
 
constexpr BitSet< N > operator& (const BitSet< N > &other) const noexcept
 Compute the intersection of this bitset with another.
 
constexpr BitSet< N > operator| (const BitSet< N > &other) const noexcept
 Compute the union of this bitset with another.
 
constexpr BitSet< N > operator- (const BitSet< N > &other) const noexcept
 Compute the difference of this bitset with another.
 
BitSet< N > operator~ () const noexcept
 Compute the bitwise NOT of this bitset.
 
BitSet< N > & operator&= (const BitSet< N > &other) noexcept
 In-place bitwise AND.
 
BitSet< N > & operator|= (const BitSet< N > &other) noexcept
 In-place bitwise OR.
 
BitSet< N > & operator^= (const BitSet< N > &other) noexcept
 In-place bitwise XOR.
 
constexpr bool operator== (const BitSet< N > &other) const noexcept
 Check equality with another bitset.
 
constexpr bool operator!= (const BitSet< N > &other) const noexcept
 Check inequality with another bitset.
 

Static Public Member Functions

static constexpr uint16_t size () noexcept
 Return the number of bits this bitset can hold.
 
static constexpr uint16_t size_words () noexcept
 Return the number of storage words used.
 

Static Public Attributes

static constexpr uint16_t storageSize = (N + sizeof(storage_t) * 8 - 1) / (sizeof(storage_t) * 8)
 The underlying storage type, sized to hold at least N bits.
 
static constexpr uint16_t bitsPerStorage = sizeof(storage_t) * 8
 

Detailed Description

template<uint16_t N>
class lampda::common::BitSet< N >

A compact, zero-allocation bitset for compile-time sized bit arrays.

This is a lightweight alternative to std::bitset, designed for embedded environments where std::bitset may not be available or desirable.

Template Parameters
NThe number of bits in the bitset

Constructor & Destructor Documentation

◆ BitSet()

template<uint16_t N>
constexpr lampda::common::BitSet< N >::BitSet ( storage_t  value)
inlineexplicitconstexpr

Construct with a single storage value repeated for all words.

Parameters
valueThe value to initialize all storage words with

Member Function Documentation

◆ count()

template<uint16_t N>
constexpr uint16_t lampda::common::BitSet< N >::count ( ) const
inlineconstexprnoexcept

Count the number of bits set to 1.

Returns
The population count

◆ flip()

template<uint16_t N>
void lampda::common::BitSet< N >::flip ( uint16_t  pos)
inlinenoexcept

Flip (invert) the bit at position pos.

Parameters
posBit position (0 to N-1)

◆ operator&()

template<uint16_t N>
constexpr BitSet< N > lampda::common::BitSet< N >::operator& ( const BitSet< N > &  other) const
inlineconstexprnoexcept

Compute the intersection of this bitset with another.

Parameters
otherThe other bitset (must be the same size)
Returns
A new bitset with bits set where both operands have them set

◆ operator-()

template<uint16_t N>
constexpr BitSet< N > lampda::common::BitSet< N >::operator- ( const BitSet< N > &  other) const
inlineconstexprnoexcept

Compute the difference of this bitset with another.

Parameters
otherThe other bitset (must be the same size)
Returns
A new bitset with bits set where this has them but other does not

◆ operator|()

template<uint16_t N>
constexpr BitSet< N > lampda::common::BitSet< N >::operator| ( const BitSet< N > &  other) const
inlineconstexprnoexcept

Compute the union of this bitset with another.

Parameters
otherThe other bitset (must be the same size)
Returns
A new bitset with bits set where either operand has them set

◆ operator~()

template<uint16_t N>
BitSet< N > lampda::common::BitSet< N >::operator~ ( ) const
inlinenoexcept

Compute the bitwise NOT of this bitset.

Returns
A new bitset with all bits flipped

◆ reset()

template<uint16_t N>
void lampda::common::BitSet< N >::reset ( uint16_t  pos)
inlinenoexcept

Clear the bit at position pos.

Parameters
posBit position (0 to N-1)

◆ set() [1/2]

template<uint16_t N>
void lampda::common::BitSet< N >::set ( uint16_t  pos)
inlinenoexcept

Set the bit at position pos to 1.

Parameters
posBit position (0 to N-1)

◆ set() [2/2]

template<uint16_t N>
void lampda::common::BitSet< N >::set ( uint16_t  pos,
bool  value 
)
inlinenoexcept

Set the bit at position pos to the given value.

Parameters
posBit position (0 to N-1)
valueTrue to set the bit, false to clear it

◆ test()

template<uint16_t N>
constexpr bool lampda::common::BitSet< N >::test ( uint16_t  pos) const
inlineconstexprnoexcept

Get the value of the bit at position pos.

Parameters
posBit position (0 to N-1)
Returns
True if the bit is set, false otherwise

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