tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
bitset< Bits > Struct Template Reference

The class template bitset represents a fixed-size sequence of Bits bits. Bitsets can be manipulated by standard logic operators. More...

#include <etl/bitset.hpp>

Public Types

using reference = basic_bitset<Bits, etl::size_t>::reference
 

Public Member Functions

constexpr bitset () noexcept=default
 Constructs a bitset with all bits set to zero.
 
template<typename CharT, typename Traits>
constexpr bitset (basic_string_view< CharT, Traits > const &str, typename basic_string_view< CharT, Traits >::size_type pos=0, typename basic_string_view< CharT, Traits >::size_type n=basic_string_view< CharT, Traits >::npos, CharT zero=CharT('0'), CharT one=CharT('1'))
 Constructs a bitset using the characters in the etl::basic_string_view str.
 
template<typename CharT>
constexpr bitset (CharT const *str, typename basic_string_view< CharT >::size_type n=basic_string_view< CharT >::npos, CharT zero=CharT('0'), CharT one=CharT('1'))
 Constructs a bitset using the characters in the char const* str.
 
constexpr bitset (unsigned long long val) noexcept
 Constructs a bitset, initializing the first (rightmost, least significant) M bit positions to the corresponding bit values of val, where M is the smaller of the number of bits in an unsigned long long and the number of bits Bits in the bitset being constructed. If M is less than Bits (the bitset is longer than 64 bits, for typical implementations of unsigned long long), the remaining bit positions are initialized to zeroes.
 
constexpr auto all () const noexcept -> bool
 Checks if all bits are set to true.
 
constexpr auto any () const noexcept -> bool
 Checks if any bits are set to true.
 
constexpr auto count () const noexcept -> size_t
 Returns the number of bits that are set to true.
 
constexpr auto flip () noexcept -> bitset &
 Flips all bits (like operator~, but in-place).
 
constexpr auto flip (size_t pos) noexcept -> bitset &
 Flips the bit at the position pos.
 
constexpr auto none () const noexcept -> bool
 Checks if none bits are set to true.
 
constexpr auto operator&= (bitset const &other) noexcept -> bitset &
 Sets the bits to the result of binary AND on corresponding pairs of bits of *this and other.
 
constexpr auto operator== (bitset const &rhs) const noexcept -> bool
 Returns true if all of the bits in *this and rhs are equal.
 
constexpr auto operator[] (size_t const pos) -> reference
 Returns a reference like proxy to the bit at the position pos. Perfoms no bounds checking.
 
constexpr auto operator[] (size_t const pos) const -> bool
 Returns the value of the bit at the position pos. Perfoms no bounds checking.
 
constexpr auto operator^= (bitset const &other) noexcept -> bitset &
 Sets the bits to the result of binary XOR on corresponding pairs of bits of *this and other.
 
constexpr auto operator|= (bitset const &other) noexcept -> bitset &
 Sets the bits to the result of binary OR on corresponding pairs of bits of *this and other.
 
constexpr auto operator~ () const noexcept -> bitset
 Returns a temporary copy of *this with all bits flipped (binary NOT).
 
constexpr auto reset () noexcept -> bitset &
 Sets all bits to false.
 
constexpr auto reset (size_t pos) noexcept -> bitset &
 Sets the bit at position pos to false.
 
constexpr auto set () noexcept -> bitset &
 Sets all bits to true.
 
constexpr auto set (etl::size_t pos, bool value=true) -> bitset &
 Sets the bit at the given position to the given value.
 
constexpr auto size () const noexcept -> size_t
 Returns the number of bits that the bitset holds.
 
constexpr auto test (size_t const pos) const -> bool
 Returns the value of the bit at the position pos. Perfoms no bounds checking.
 
template<size_t Capacity, typename CharT = char, typename Traits = char_traits<CharT>>
requires (Capacity >= Bits)
constexpr auto to_string (CharT zero=CharT('0'), CharT one=CharT('1')) const -> basic_inplace_string< CharT, Capacity, Traits >
 Converts the contents of the bitset to a string. Uses zero to represent bits with value of false and one to represent bits with value of true. The resulting string contains Bits characters with the first character corresponds to the last (Bits-1th) bit and the last character corresponding to the first bit.
 
constexpr auto to_ullong () const noexcept -> unsigned long long requires(etl::numeric_limits< unsigned long long >::digits >=Bits)
 Converts the contents of the bitset to an unsigned long long integer. The first bit corresponds to the least significant digit of the number and the last bit corresponds to the most significant digit.
 
constexpr auto to_ulong () const noexcept -> unsigned long requires(etl::numeric_limits< unsigned long >::digits >=Bits)
 Converts the contents of the bitset to an unsigned long integer. The first bit corresponds to the least significant digit of the number and the last bit corresponds to the most significant digit.
 

Friends

constexpr auto operator& (bitset const &lhs, bitset const &rhs) noexcept -> bitset
 Performs binary AND between two bitsets, lhs and rhs.
 
constexpr auto operator^ (bitset const &lhs, bitset const &rhs) noexcept -> bitset
 Performs binary XOR between two bitsets, lhs and rhs.
 
constexpr auto operator| (bitset const &lhs, bitset const &rhs) noexcept -> bitset
 Performs binary OR between two bitsets, lhs and rhs.
 

Detailed Description

template<etl::size_t Bits>
struct etl::bitset< Bits >

The class template bitset represents a fixed-size sequence of Bits bits. Bitsets can be manipulated by standard logic operators.

Examples
bitset.cpp.

Member Typedef Documentation

◆ reference

template<etl::size_t Bits>
using reference = basic_bitset<Bits, etl::size_t>::reference
Examples
bitset.cpp.

Constructor & Destructor Documentation

◆ bitset() [1/4]

template<etl::size_t Bits>
bitset ( )
constexprdefaultnoexcept

Constructs a bitset with all bits set to zero.

◆ bitset() [2/4]

template<etl::size_t Bits>
bitset ( unsigned long long val)
inlineconstexprnoexcept

Constructs a bitset, initializing the first (rightmost, least significant) M bit positions to the corresponding bit values of val, where M is the smaller of the number of bits in an unsigned long long and the number of bits Bits in the bitset being constructed. If M is less than Bits (the bitset is longer than 64 bits, for typical implementations of unsigned long long), the remaining bit positions are initialized to zeroes.

◆ bitset() [3/4]

template<etl::size_t Bits>
template<typename CharT, typename Traits>
bitset ( basic_string_view< CharT, Traits > const & str,
typename basic_string_view< CharT, Traits >::size_type pos = 0,
typename basic_string_view< CharT, Traits >::size_type n = basic_string_view<CharT, Traits>::npos,
CharT zero = CharT('0'),
CharT one = CharT('1') )
inlineexplicitconstexpr

Constructs a bitset using the characters in the etl::basic_string_view str.

An optional starting position pos and length n can be provided, as well as characters denoting alternate values for set (one) and unset (zero) bits. Traits::eq() is used to compare the character values. The effective length of the initializing string is min(n, str.size() - pos).

Parameters
strstring used to initialize the bitset
posa starting offset into str
nnumber of characters to use from str
zeroalternate character for set bits in str
onealternate character for unset bits in str

◆ bitset() [4/4]

template<etl::size_t Bits>
template<typename CharT>
bitset ( CharT const * str,
typename basic_string_view< CharT >::size_type n = basic_string_view<CharT>::npos,
CharT zero = CharT('0'),
CharT one = CharT('1') )
inlineexplicitconstexpr

Constructs a bitset using the characters in the char const* str.

Parameters
strstring used to initialize the bitset
nnumber of characters to use from str
zeroalternate character for set bits in str
onealternate character for unset bits in str

Member Function Documentation

◆ all()

template<etl::size_t Bits>
auto all ( ) const -> bool
inlinenodiscardconstexprnoexcept

Checks if all bits are set to true.

◆ any()

template<etl::size_t Bits>
auto any ( ) const -> bool
inlinenodiscardconstexprnoexcept

Checks if any bits are set to true.

◆ count()

template<etl::size_t Bits>
auto count ( ) const -> size_t
inlinenodiscardconstexprnoexcept

Returns the number of bits that are set to true.

◆ flip() [1/2]

template<etl::size_t Bits>
auto flip ( ) -> bitset&
inlineconstexprnoexcept

Flips all bits (like operator~, but in-place).

◆ flip() [2/2]

template<etl::size_t Bits>
auto flip ( size_t pos) -> bitset&
inlineconstexprnoexcept

Flips the bit at the position pos.

Parameters
posIndex of the bit to be reset.
Returns
*this

◆ none()

template<etl::size_t Bits>
auto none ( ) const -> bool
inlinenodiscardconstexprnoexcept

Checks if none bits are set to true.

◆ operator&=()

template<etl::size_t Bits>
auto operator&= ( bitset< Bits > const & other) -> bitset&
inlineconstexprnoexcept

Sets the bits to the result of binary AND on corresponding pairs of bits of *this and other.

◆ operator==()

template<etl::size_t Bits>
auto operator== ( bitset< Bits > const & rhs) const -> bool
inlinenodiscardconstexprnoexcept

Returns true if all of the bits in *this and rhs are equal.

◆ operator[]() [1/2]

template<etl::size_t Bits>
auto operator[] ( size_t const pos) -> reference
inlinenodiscardconstexpr

Returns a reference like proxy to the bit at the position pos. Perfoms no bounds checking.

Parameters
posIndex of the bit.

◆ operator[]() [2/2]

template<etl::size_t Bits>
auto operator[] ( size_t const pos) const -> bool
inlinenodiscardconstexpr

Returns the value of the bit at the position pos. Perfoms no bounds checking.

Parameters
posIndex of the bit.

◆ operator^=()

template<etl::size_t Bits>
auto operator^= ( bitset< Bits > const & other) -> bitset&
inlineconstexprnoexcept

Sets the bits to the result of binary XOR on corresponding pairs of bits of *this and other.

◆ operator|=()

template<etl::size_t Bits>
auto operator|= ( bitset< Bits > const & other) -> bitset&
inlineconstexprnoexcept

Sets the bits to the result of binary OR on corresponding pairs of bits of *this and other.

◆ operator~()

template<etl::size_t Bits>
auto operator~ ( ) const -> bitset
inlineconstexprnoexcept

Returns a temporary copy of *this with all bits flipped (binary NOT).

◆ reset() [1/2]

template<etl::size_t Bits>
auto reset ( ) -> bitset&
inlineconstexprnoexcept

Sets all bits to false.

◆ reset() [2/2]

template<etl::size_t Bits>
auto reset ( size_t pos) -> bitset&
inlineconstexprnoexcept

Sets the bit at position pos to false.

Parameters
posIndex of the bit to be reset.
Returns
*this

◆ set() [1/2]

template<etl::size_t Bits>
auto set ( ) -> bitset&
inlineconstexprnoexcept

Sets all bits to true.

◆ set() [2/2]

template<etl::size_t Bits>
auto set ( etl::size_t pos,
bool value = true ) -> bitset&
inlineconstexpr

Sets the bit at the given position to the given value.

Parameters
posIndex of the bit to be modified.
valueThe new value for the bit.
Returns
*this

◆ size()

template<etl::size_t Bits>
auto size ( ) const -> size_t
inlinenodiscardconstexprnoexcept

Returns the number of bits that the bitset holds.

◆ test()

template<etl::size_t Bits>
auto test ( size_t const pos) const -> bool
inlinenodiscardconstexpr

Returns the value of the bit at the position pos. Perfoms no bounds checking.

Parameters
posIndex of the bit.

◆ to_string()

template<etl::size_t Bits>
template<size_t Capacity, typename CharT = char, typename Traits = char_traits<CharT>>
requires (Capacity >= Bits)
auto to_string ( CharT zero = CharT('0'),
CharT one = CharT('1') ) const -> basic_inplace_string<CharT, Capacity, Traits>
inlinenodiscardconstexpr

Converts the contents of the bitset to a string. Uses zero to represent bits with value of false and one to represent bits with value of true. The resulting string contains Bits characters with the first character corresponds to the last (Bits-1th) bit and the last character corresponding to the first bit.

Todo
Currently truncates the low bits, if the string is large enough.

◆ to_ullong()

template<etl::size_t Bits>
auto to_ullong ( ) const -> unsigned long long requires(etl::numeric_limits<unsigned long long>::digits >= Bits)
inlinenodiscardconstexprnoexcept

Converts the contents of the bitset to an unsigned long long integer. The first bit corresponds to the least significant digit of the number and the last bit corresponds to the most significant digit.

◆ to_ulong()

template<etl::size_t Bits>
auto to_ulong ( ) const -> unsigned long requires(etl::numeric_limits<unsigned long>::digits >= Bits)
inlinenodiscardconstexprnoexcept

Converts the contents of the bitset to an unsigned long integer. The first bit corresponds to the least significant digit of the number and the last bit corresponds to the most significant digit.

Friends And Related Symbol Documentation

◆ operator&

template<etl::size_t Bits>
auto operator& ( bitset< Bits > const & lhs,
bitset< Bits > const & rhs ) -> bitset
friend

Performs binary AND between two bitsets, lhs and rhs.

◆ operator^

template<etl::size_t Bits>
auto operator^ ( bitset< Bits > const & lhs,
bitset< Bits > const & rhs ) -> bitset
friend

Performs binary XOR between two bitsets, lhs and rhs.

◆ operator|

template<etl::size_t Bits>
auto operator| ( bitset< Bits > const & lhs,
bitset< Bits > const & rhs ) -> bitset
friend

Performs binary OR between two bitsets, lhs and rhs.


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