Base class, general case. It is a class invariant that _Nw will be nonnegative.
See documentation for bitset.
0 is the least significant word.
Base class, specialization for a single word.
See documentation for bitset.
Base class, specialization for no storage (zero-length bitset).
See documentation for bitset.
The bitset class represents a fixed-size sequence of bits.
(Note that bitset does not meet the formal requirements of a container. Mainly, it lacks iterators.)
The template argument, Nb, may be any non-negative number, specifying the number of bits (e.g., "0", "12", "1024*1024").
In the general unoptimized case, storage is allocated in word-sized blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B words will be used for storage. B - NbB bits are unused. (They are the high-order bits in the highest word.) It is a class invariant that those unused bits are always zero.
If you think of bitset as a simple array of bits, be aware that your mental picture is reversed: a bitset behaves the same way as bits in integers do, with the bit at index 0 in the least significant / right-hand position, and the bit at index Nb-1 in the most significant / left-hand position. Thus, unlike other containers, a bitset's index counts from right to left, to put it very loosely.
This behavior is preserved when translating to and from strings. For example, the first line of the following program probably prints b('a') is 0001100001 on a modern ASCII system.
#include <bitset>
using namespace std;
int main()
{
long a = 'a';
bitset<10> b(a);
cout << "b('a') is " << b << endl;
ostringstream s;
s << b;
string str = s.str();
cout << "index 3 in the string is " << str[3] << " but\n"
<< "index 3 in the bitset is " << b[3] << endl;
}
Also see: http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt12ch33s02.html for a description of extensions.
Most of the actual code isn't contained in bitset<> itself, but in the base class _Base_bitset. The base class works with whole words, not with individual bits. This allows us to specialize _Base_bitset for the important special case where the bitset is only a single word.
Extra confusion can result due to the fact that the storage for _Base_bitset is a regular array, and is indexed as such. This is carefully encapsulated.
This encapsulates the concept of a single bit. An instance of this class is a proxy for an actual bit; this way the individual bit operations are done as faster word-size bitwise instructions.
Most users will never need to use this class directly; conversions to and from bool are automatic and should be transparent. Overloaded operators help to preserve the illusion.
(On a typical system, this bit reference is 64 times the size of an actual bit. Ha.)
All bits set to zero.
Initial bits bitwise-copied from a single word (others set to zero).
Use a subset of a string.
- Parameters
-
| __s | A string of 0 and 1 characters. |
| __position | Index of the first character in __s to use; defaults to zero. |
- Exceptions
-
| std::out_of_range | If pos is bigger the size of __s. |
| std::invalid_argument | If a character appears in the string which is neither 0 nor 1. |
Use a subset of a string.
- Parameters
-
| __s | A string of 0 and 1 characters. |
| __position | Index of the first character in __s to use. |
| __n | The number of characters to copy. |
- Exceptions
-
| std::out_of_range | If __position is bigger the size of __s. |
| std::invalid_argument | If a character appears in the string which is neither 0 nor 1. |
Operations on bitsets.
- Parameters
-
| __rhs | A same-sized bitset. |
These should be self-explanatory.
Operations on bitsets.
- Parameters
-
| __position | The number of places to shift. |
These should be self-explanatory.
These versions of single-bit set, reset, flip, and test are extensions from the SGI version. They do no range checking.
Sets every bit to true.
Sets a given bit to a particular value.
- Parameters
-
| __position | The index of the bit. |
| __val | Either true or false, defaults to true. |
- Exceptions
-
| std::out_of_range | If pos is bigger the size of the set. |
Sets every bit to false.
Sets a given bit to false.
- Parameters
-
| __position | The index of the bit. |
- Exceptions
-
| std::out_of_range | If pos is bigger the size of the set. |
Same as writing set(pos,false).
Toggles every bit to its opposite value.
Toggles a given bit to its opposite value.
- Parameters
-
| __position | The index of the bit. |
- Exceptions
-
| std::out_of_range | If pos is bigger the size of the set. |
See the no-argument flip().
Array-indexing support.
- Parameters
-
| __position | Index into the bitset. |
- Returns
- A bool for a const bitset. For non-const bitsets, an instance of the reference proxy class.
- Note
- These operators do no range checking and throw no exceptions, as required by DR 11 to the standard.
_GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already resolves DR 11 (items 1 and 2), but does not do the range-checking required by that DR's resolution. -pme The DR has since been changed: range-checking is a precondition (users' responsibility), and these functions must not throw. -pme
Returns a numerical interpretation of the bitset.
- Returns
- The integral equivalent of the bits.
- Exceptions
-
| std::overflow_error | If there are too many bits to be represented in an unsigned long. |
Returns a character interpretation of the bitset.
- Returns
- The string equivalent of the bits.
Note the ordering of the bits: decreasing character positions correspond to increasing bit positions (see the main class notes for an example).
Returns the number of bits which are set.
Returns the total number of bits.
These comparisons for equality/inequality are, well, bitwise.
Tests the value of a bit.
- Parameters
-
| __position | The index of a bit. |
- Returns
- The value at pos.
- Exceptions
-
| std::out_of_range | If pos is bigger the size of the set. |
Tests whether all the bits are on.
- Returns
- True if all the bits are set.
Tests whether any of the bits are on.
- Returns
- True if at least one bit is set.
Tests whether any of the bits are on.
- Returns
- True if none of the bits are set.
Self-explanatory.
Finds the index of the first "on" bit.
- Returns
- The index of the first bit set, or size() if not found.
- See Also
- _Find_next
Finds the index of the next "on" bit after prev.
- Returns
- The index of the next bit set, or size() if not found.
- Parameters
-
| __prev | Where to start searching. |
- See Also
- _Find_first
Global bitwise operations on bitsets.
- Parameters
-
| __x | A bitset. |
| __y | A bitset of the same size as __x. |
- Returns
- A new bitset.
These should be self-explanatory.
Global I/O operators for bitsets.
Direct I/O between streams and bitsets is supported. Output is straightforward. Input will skip whitespace, only accept 0 and 1 characters, and will only extract as many digits as the bitset will hold.
Class std::bitset wrapper with performance instrumentation.
65 operator=(
bool __x) _GLIBCXX_NOEXCEPT
67 *
static_cast<_Base_ref*
>(
this) = __x;
72 operator=(
const reference& __x) _GLIBCXX_NOEXCEPT
74 *
static_cast<_Base_ref*
>(
this) = __x;
79 operator~() const _GLIBCXX_NOEXCEPT
81 return ~(*
static_cast<const _Base_ref*
>(
this));
84 operator bool() const _GLIBCXX_NOEXCEPT
86 return *
static_cast<const _Base_ref*
>(
this);
90 flip() _GLIBCXX_NOEXCEPT
98 _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT
101 #if __cplusplus >= 201103L
102 constexpr bitset(
unsigned long long __val) noexcept
104 bitset(
unsigned long __val)
108 template<
typename _CharT,
typename _Traits,
typename _Alloc>
110 bitset(
const std::basic_string<_CharT, _Traits, _Alloc>& __str,
111 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
113 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
114 __n = (std::basic_string<_CharT, _Traits, _Alloc>::npos))
115 : _Base(__str, __pos, __n) { }
119 template<
class _CharT,
class _Traits,
class _Alloc>
120 bitset(
const std::basic_string<_CharT, _Traits, _Alloc>& __str,
121 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
123 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
125 _CharT __zero, _CharT __one = _CharT(
'1'))
126 : _Base(__str, __pos, __n, __zero, __one) { }
128 bitset(
const _Base& __x) : _Base(__x) { }
130 #if __cplusplus >= 201103L
131 template<
typename _CharT>
133 bitset(
const _CharT* __str,
134 typename std::basic_string<_CharT>::size_type __n
135 = std::basic_string<_CharT>::npos,
136 _CharT __zero = _CharT(
'0'), _CharT __one = _CharT(
'1'))
137 : _Base(__str, __n, __zero, __one) { }
142 operator&=(
const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
149 operator|=(
const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
156 operator^=(
const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
163 operator<<=(
size_t __pos) _GLIBCXX_NOEXCEPT
170 operator>>=(
size_t __pos) _GLIBCXX_NOEXCEPT
177 set() _GLIBCXX_NOEXCEPT
186 set(
size_t __pos,
bool __val =
true)
188 _Base::set(__pos, __val);
193 reset() _GLIBCXX_NOEXCEPT
207 operator~() const _GLIBCXX_NOEXCEPT
208 {
return bitset(~_M_base()); }
211 flip() _GLIBCXX_NOEXCEPT
228 operator[](
size_t __pos)
230 return reference(_M_base()[__pos],
this);
235 _GLIBCXX_CONSTEXPR
bool
236 operator[](
size_t __pos)
const
238 return _Base::operator[](__pos);
241 using _Base::to_ulong;
242 #if __cplusplus >= 201103L
243 using _Base::to_ullong;
246 template <
typename _CharT,
typename _Traits,
typename _Alloc>
247 std::basic_string<_CharT, _Traits, _Alloc>
249 {
return _M_base().template to_string<_CharT, _Traits, _Alloc>(); }
253 template<
class _CharT,
class _Traits,
class _Alloc>
254 std::basic_string<_CharT, _Traits, _Alloc>
255 to_string(_CharT __zero, _CharT __one = _CharT(
'1'))
const
257 return _M_base().template
258 to_string<_CharT, _Traits, _Alloc>(__zero, __one);
263 template<
typename _CharT,
typename _Traits>
264 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
266 {
return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
270 template<
class _CharT,
class _Traits>
271 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
272 to_string(_CharT __zero, _CharT __one = _CharT(
'1'))
const
273 {
return to_string<_CharT, _Traits,
274 std::allocator<_CharT> >(__zero, __one); }
276 template<
typename _CharT>
277 std::basic_string<_CharT, std::char_traits<_CharT>,
278 std::allocator<_CharT> >
281 return to_string<_CharT, std::char_traits<_CharT>,
282 std::allocator<_CharT> >();
285 template<
class _CharT>
286 std::basic_string<_CharT, std::char_traits<_CharT>,
287 std::allocator<_CharT> >
288 to_string(_CharT __zero, _CharT __one = _CharT(
'1'))
const
290 return to_string<_CharT, std::char_traits<_CharT>,
291 std::allocator<_CharT> >(__zero, __one);
294 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
297 return to_string<char,std::char_traits<char>,std::allocator<char> >();
300 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
301 to_string(
char __zero,
char __one =
'1')
const
303 return to_string<char, std::char_traits<char>,
304 std::allocator<char> >(__zero, __one);
311 operator==(
const bitset<_Nb>& __rhs)
const _GLIBCXX_NOEXCEPT
312 {
return _M_base() == __rhs; }
315 operator!=(
const bitset<_Nb>& __rhs)
const _GLIBCXX_NOEXCEPT
316 {
return _M_base() != __rhs; }
324 operator<<(
size_t __pos)
const _GLIBCXX_NOEXCEPT
325 {
return bitset<_Nb>(_M_base() << __pos); }
328 operator>>(
size_t __pos)
const _GLIBCXX_NOEXCEPT
329 {
return bitset<_Nb>(_M_base() >> __pos); }
332 _M_base() _GLIBCXX_NOEXCEPT
336 _M_base() const _GLIBCXX_NOEXCEPT
342 operator&(
const bitset<_Nb>& __x,
const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
343 {
return bitset<_Nb>(__x) &= __y; }
347 operator|(
const bitset<_Nb>& __x,
const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
348 {
return bitset<_Nb>(__x) |= __y; }
352 operator^(
const bitset<_Nb>& __x,
const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
353 {
return bitset<_Nb>(__x) ^= __y; }
355 template<
typename _CharT,
typename _Traits,
size_t _Nb>
356 std::basic_istream<_CharT, _Traits>&
357 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
358 {
return __is >> __x._M_base(); }
360 template<
typename _CharT,
typename _Traits,
size_t _Nb>
361 std::basic_ostream<_CharT, _Traits>&
362 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
363 const bitset<_Nb>& __x)
364 {
return __os << __x._M_base(); }
367 #if __cplusplus >= 201103L
371 struct hash<__profile::bitset<_Nb>>
372 :
public __hash_base<size_t, __profile::bitset<_Nb>>
375 operator()(
const __profile::bitset<_Nb>& __b)
const noexcept
376 {
return std::hash<_GLIBCXX_STD_C::bitset<_Nb>>()(__b._M_base()); }
383 bool operator==(const exception_ptr &, const exception_ptr &) _GLIBCXX_USE_NOEXCEPT __attribute__((__pure__))
#define bool
Definition: stdbool.h:33
std::basic_istream< _CharT, _Traits > & operator>>(std::basic_istream< _CharT, _Traits > &__is, basic_string< _CharT, _Traits, _Allocator > &__str)
Definition: string:1122
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const basic_string< _CharT, _Traits, _Allocator > &__str)
Definition: string:1116
bool operator!=(const exception_ptr &, const exception_ptr &) _GLIBCXX_USE_NOEXCEPT __attribute__((__pure__))