A C++ template library for embedded applications
MIT licensed
Designed and
maintained by
John Wellbelove
bitset
This is the new default bitset implementation, from 20.33.0 onwards.
For the older version, see etl::bitset legacy.

A fixed capacity bitset.
Has a number of extensions over std::bitset. Can be considered similar to an array of bool.
STL equivalent: std::bitset

Internally defined buffers

etl::bitset<size_t N>
etl::bitset<size_t N, typename TElement>

Externally defined buffers

20.34.0
etl::bitset_ext<size_t N>
etl::bitset_ext<size_t N, typename TElement>

The template parameters will determine whether the bitset uses a common implementation or one of four fixed sized
implementations.

Note: etl::ibitset is no longer a reference type for any size bitset.
____________________________________________________________________________________________________
etl::bitset<size_t N> and etl::bitset_ext<size_t N> on their own will use unsigned char as the underlying
element type and the operations will be implemented by the generic protected member functions in etl::ibitset.
____________________________________________________________________________________________________
etl::bitset<size_t N, typename TElement>
etl::bitset_ext<size_t N, typename TElement>
Specifying a type for TElement will override the default element type and define it as the unsigned type of TElement.
As above, the operations will be implemented by the generic protected member functions in etl::ibitset.

Specialisations for maximum efficiency


There are specialisations for when the required number of bits matches the number of bits in the element type.
These specialisations are considerably faster and more efficient.

etl::bitset<8,  int8_t>
etl::bitset<16, int16_t>
etl::bitset<32, int32_t>
etl::bitset<8,  int64_t>

any(), none() and all() are overloaded in these specialisations to allow a mask to be specified, so that a bitset that
requires less bits than the element type may still use the most efficient implementation.

etl::bitset<8, int8_t> bst; // But only want to use 6 bits.
bool any = bst.any(0x3F);   // Check the lower 6 bits.
____________________________________________________________________________________________________
Types

span_type       A mutable span type
const_span_type A non-mutable span type
element_type    The type used as the internal storage for a bitset. By default, this is unsigned char.
buffer_type     The type used as the to define the external buffer. Defined in etl::bitset_ext only. 20.34.0
____________________________________________________________________________________________________
Constants

All npos values are equivalent.

etl::bitset_constants::npos

etl::bitset<>::npos

template <size_t Size, typename TElement>
etl::bitset<Size, TElement>::npos

template <typename TElement>
etl::bitset_impl<TElement>::npos
____________________________________________________________________________________________________

Number_Of_Elements
Bits_Per_Element
Allocated_Bits
All_Set_Element
All_Clear_Element
____________________________________________________________________________________________________
Constructor

The initial state of the bitset is all clear (false).

etl::bitset();
etl::bitset(unsigned long value);
etl::bitset(const char* str);
etl::bitset(const wchar_t* str);
etl::bitset(const char16_t* str);
etl::bitset(const char32_t* str);
The bitset is either default constructed, initialised with a numeric value, or a text string of zeros and ones.

etl::bitset_ext(element_type* pbuffer);
etl::bitset_ext(unsigned long value, element_type* pbuffer);
etl::bitset_ext(const char* str, element_type* pbuffer);
etl::bitset_ext(const wchar_t* str, element_type* pbuffer);
etl::bitset_ext(const char16_t* str, element_type* pbuffer);
etl::bitset_ext(const char32_t* str, element_type* pbuffer);
The bitset is either default constructed, initialised with a numeric value, or a text string of zeros and ones.
A pointer to the external must b supplied that is large enough to hold the bitset.
The buffer may be defined as follows:-
using Bitset = etl::bitset_ext<32>;
Bitset::element_type buffer[Bitset::Number_Of_Elements];
20.34.0

etl::bitset_ext(buffer_type& buffer);
etl::bitset_ext(unsigned long value, buffer_type& buffer);
etl::bitset_ext(const char* str, buffer_type& buffer);
etl::bitset_ext(const wchar_t* str, buffer_type& buffer);
etl::bitset_ext(const char16_t* str, buffer_type& buffer);
etl::bitset_ext(const char32_t* str, buffer_type& buffer);
The bitset is either default constructed, initialised with a numeric value, or a text string of zeros and ones.
A n external must b supplied that is large enough to hold the bitset.
The buffer may be defined as follows:-
using Bitset = etl::bitset_ext<32>;
Bitset::buffer_type buffer;
20.34.0
____________________________________________________________________________________________________
Modifiers

etl::bitset& set();
Set all bits.
____________________________________________________________________________________________________
etl::bitset& set(element_type value);
20.34.0
Set the bits to value.
Valid when the bitset width matches the element type width.
____________________________________________________________________________________________________
etl::bitset& set(const char* str);
etl::bitset& set(const wchar_t* str);
etl::bitset& set(const char16_t* str);
etl::bitset& set(const char32_t* str);
Set with a text string of  '0' and '1' characters.
____________________________________________________________________________________________________
etl::bitset& set(size_t position, bool value = true);
Set a position to a one or zero, default one.
____________________________________________________________________________________________________
etl::bitset& reset();
Reset all bits.
____________________________________________________________________________________________________
etl::bitset& reset(size_t position);
Set a position to a zero.
____________________________________________________________________________________________________
etl::bitset& from_string(const char*);
etl::bitset& from_string(const wchar_t*);
etl::bitset& from_string(const char16_t*);
etl::bitset& from_string(const char32_t*);
Alias of set.
The bitset is built from a string of  '0' and '1' characters.
____________________________________________________________________________________________________
Access

template <typename T>
T value() const
Returns the value corresponding to the bitset.
T specifies the integral type to convert to.

unsigned long to_ulong() const
unsigned long long to_ullong() const
Functions for compatibility with the STL.
Calls value<unsigned long>() or value<unsigned long long>().

If the type is too small to contain the bitset size, a compile time error will result.
____________________________________________________________________________________________________
template <typename TString>
TString to_string(typename TString::value_type zero = typename TString::value_type('0'),
                  typename TString::value_type one  = typename TString::value_type('1')) const
Returns the value as a string of 0 and 1 characters.
If the string type is not large enough to contain the digits then an etl::bitset_string_too_small is emitted.
____________________________________________________________________________________________________
span_type span()
const_span_type span() const
Returns an etl::span of the underlying binary data.
The span is ordered LSB to MSB.
____________________________________________________________________________________________________
Bit access

bool operator[](size_t position) const
Returns the boolean state of the indexed bit.
position is not checked for validity.
____________________________________________________________________________________________________
size_t count() const
Returns the number of set bits.
____________________________________________________________________________________________________
size_t size() const
Returns the number of bits supported by this bitset.
____________________________________________________________________________________________________
bool test(size_t position) const
Returns the boolean state of the indexed bit.
position is not checked for validity.
____________________________________________________________________________________________________
bool any() const
Returns true if any of the bits are set, otherwise false.

bool any(element_type mask) const
Only enabled for bitsets that fit within one element.
Returns true if any of the bits are set, after the mask has been applied, otherwise false.
Valid when the bitset width matches the element type width.
20.34.0
____________________________________________________________________________________________________
bool none() const
Returns true if none of the bits are set, otherwise false.

bool none(element_type mask) const
Only enabled for bitsets that fit within one element.
Returns true if none of the bits are set, after the mask has been applied, otherwise false.
Valid when the bitset width matches the element type width.
20.34.0
____________________________________________________________________________________________________
bool all() const
Returns true if al of the bits are set, otherwise false.

bool all(element_type mask) const
Only enabled for bitsets that fit within one element.
Returns true if al of the bits are set, after the mask has been applied, otherwise false.
Valid when the bitset width matches the element type width.
20.34.0
____________________________________________________________________________________________________
size_t find_first(bool state) const

Returns the position of the first bit in the specified state. If not found then returns bitset<>::npos.
____________________________________________________________________________________________________
size_t find_next(bool state, size_t position) const
Returns the position of the next bit in the specified state, starting from position. If not found then returns
bitset<>::npos.
position is not checked for validity.
____________________________________________________________________________________________________
Bit operations

bitset<size_t Size>& operator &= (const bitset<size_t Size>& rhs);
bitset<size_t Size>& operator |= (const bitset<size_t Size>& rhs);
bitset<size_t Size>& operator ^= (const bitset<size_t Size>& rhs);
bitset<size_t Size>& operator <<= (size_t shift);
bitset<size_t Size>& operator >>= (size_t shift);
bool operator == (const bitset<size_t Size>& rhs);
bool operator != (const bitset<size_t Size>& rhs);
____________________________________________________________________________________________________
Non-member functions

void swap(etl::bitset<Size>& lhs, etl::bitset<Size>& rhs)
Swaps the contents of the two bitsets.
The bitsets must be the same size.
bitset.h