bitset


A specialized class for manipulating bit sequences.

Library

Standards<ToolKit>

Declaration


#include <bitset>

template< size_t N >
class bitset
  <
  typename Operation::first_argument_type,
  typename Operation::result_type
  >
  

Interface

Constructor
bitset()
Constructs a bitset with all bits set to zero.
Constructor
bitset( unsigned long value )
Constructs a bitset with the first sizeof( value ) bits set to value . Any remaining bits are set to zero.
Constructor
explicit bitset( const string& str , size_t pos , size_t n )
Constructs a bitset with the first n bits set to the string starting at str[ pos ] . If any of the characters in str[ pos...pos+n] are not 0 or 1 , then an invalid_argument exception is thrown. If the starting string offset pos exceeds the length of str , then an out_of_range exception is raised.

Throws: invalid_argument , out_of_range

=
bitset< N >& operator=( const bitset< N >& bits )
Assigns bits to bitset.
==
bool operator==( const bitset< N >& bits ) const
Returns true if bitset is equal to bits .
!=
bool operator!=( const bitset< N >& bits ) const
Returns true if bitset does not equal bits .
&=
bitset< N >& operator&=( const bitset<N>& bits )
Logically ANDs the bitset with bits and returns a reference to the bitset.
|=
bitset< N >& operator|=( const bitset<N>& bits )
Logically ORs the bitset with bits and returns a reference to the bitset.
^=
bitset< N >& operator^=( const bitset<N>& bits )
Logically XORs the bitset with bits and returns a reference to the bitset.
<<=
bitset< N >& operator<<=( size_t pos )
Logically shifts the bitset's value to the left pos bits, and sets the right-most bits to 0 . Returns a reference to the bitset.
>>=
bitset< N >& operator>>=( size_t pos )
Logically shifts the bitset's value to the right pos bits, and sets the left-most bits to 0 . Returns a reference to the bitset.
<<
bitset< N > operator<<( size_t pos ) const
Returns a bitset with a value equivalent to bitset logically shifted left pos bits.
>>
bitset< N > operator>>( size_t pos ) const
Returns a bitset with a value equivalent to bitset logically shifted right pos bits.
~
bitset< N > operator~() const
Returns the inverse of bitset.
any
bool any() const
Returns true if any of the bitset's bits are set.
count
size_t count() const
Returns the number of bits that are set.
flip
bitset< N >& flip()
Inverts all of the bitset's bits, and returns a reference to the bitset.
flip
bitset< N >& flip( size_t pos )
Inverts the bit at position pos , and returns a reference to the bitset. If pos is not a valid bit, an out_of_range exception is raised.

Throws: out_of_range

none
bool none() const
Returns true if none of bitset's bits are set.
reset
bitset< N >& reset()
Sets all of bitset's bits to 0 and returns a reference to the bitset.
reset
bitset< N >& reset( size_t pos )
Sets the bit at position pos to 0 and returns a reference to the bitset. If pos is not a valid bit, an out_of_range exception is raised.

Throws: out_of_range

set
bitset< N >& set()
Sets all the bitset's bits to 1 , and returns a reference to the bitset.
set
bitset< N >& set( size_t pos , bool value )
Sets the bit at position pos to value (default true ) and returns a reference to the bitset. If pos is not a valid bit, an out_of_range exception is raised.

Throws: out_of_range

size
size_t size() const
Returns the number of bits the bitset represents.
test
bool test( size_t pos ) const
Returns true if the bit at position pos is set. If pos is not a valid bit, an out_of_range exception is raised.

Throws: out_of_range

to_string
string to_string() const
Returns a string representing the bitset value, with each character representing one of the bits. A character contains the value 1 if its corresponding bit is set, or 0 otherwise.
to_ulong
unsigned long to_ulong() const
Returns an unsigned long representing the bitset value. If the bitset's value cannot be adequately represented, an overflow_error exception is raised.

Throws: overflow_error

Non-Member Functions

&
template<size_t N > bitset< N > operator&( const bitset< N >& x , const bitset< N >& y )
Returns a bitset containing the result of ( x & y ).
|
template< size_t N > bitset< N > operator|( const bitset< N >& x , const bitset< N >& y )
Returns a bitset containing the result of ( x | y ).
^
template< size_t N > bitset< N > operator^( const bitset< N >& x , const bitset< N >& y )
Returns a bitset containing the result of ( x ^ y ).

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.