3// Copyright (C) 2001-2023 Free Software Foundation, Inc.
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
27 * Silicon Graphics Computer Systems, Inc.
29 * Permission to use, copy, modify, distribute and sell this software
30 * and its documentation for any purpose is hereby granted without fee,
31 * provided that the above copyright notice appear in all copies and
32 * that both that copyright notice and this permission notice appear
33 * in supporting documentation. Silicon Graphics makes no
34 * representations about the suitability of this software for any
35 * purpose. It is provided "as is" without express or implied warranty.
38/** @file include/bitset
39 * This is a Standard C++ Library header.
42#ifndef _GLIBCXX_BITSET
43#define _GLIBCXX_BITSET 1
45#pragma GCC system_header
47#include <bits/functexcept.h> // For invalid_argument, out_of_range,
49#include <bits/stl_algobase.h> // For std::fill
54# include <bits/cxxabi_forced.h>
57#if __cplusplus >= 201103L
58# include <bits/functional_hash.h>
61#define _GLIBCXX_BITSET_BITS_PER_WORD (__CHAR_BIT__ * __SIZEOF_LONG__)
62#define _GLIBCXX_BITSET_WORDS(__n) \
63 ((__n) / _GLIBCXX_BITSET_BITS_PER_WORD + \
64 ((__n) % _GLIBCXX_BITSET_BITS_PER_WORD == 0 ? 0 : 1))
66#define _GLIBCXX_BITSET_BITS_PER_ULL (__CHAR_BIT__ * __SIZEOF_LONG_LONG__)
68namespace std _GLIBCXX_VISIBILITY(default)
70_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
72#if __cplusplus > 202002L && _GLIBCXX_HOSTED
73# define __cpp_lib_constexpr_bitset 202202L
77 * Base class, general case. It is a class invariant that _Nw will be
80 * See documentation for bitset.
85 typedef unsigned long _WordT;
87 /// 0 is the least significant word.
90 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
93#if __cplusplus >= 201103L
94 constexpr _Base_bitset(unsigned long long __val) noexcept
96#if __SIZEOF_LONG_LONG__ > __SIZEOF_LONG__
97 , _WordT(__val >> _GLIBCXX_BITSET_BITS_PER_WORD)
101 _Base_bitset(unsigned long __val)
106 static _GLIBCXX_CONSTEXPR size_t
107 _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
108 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
110 static _GLIBCXX_CONSTEXPR size_t
111 _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
112 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
114 static _GLIBCXX_CONSTEXPR size_t
115 _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
116 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
118 static _GLIBCXX_CONSTEXPR _WordT
119 _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
120 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
122 _GLIBCXX14_CONSTEXPR _WordT&
123 _M_getword(size_t __pos) _GLIBCXX_NOEXCEPT
124 { return _M_w[_S_whichword(__pos)]; }
126 _GLIBCXX_CONSTEXPR _WordT
127 _M_getword(size_t __pos) const _GLIBCXX_NOEXCEPT
128 { return _M_w[_S_whichword(__pos)]; }
130#if __cplusplus >= 201103L
131 constexpr const _WordT*
132 _M_getdata() const noexcept
136 _GLIBCXX23_CONSTEXPR _WordT&
137 _M_hiword() _GLIBCXX_NOEXCEPT
138 { return _M_w[_Nw - 1]; }
140 _GLIBCXX_CONSTEXPR _WordT
141 _M_hiword() const _GLIBCXX_NOEXCEPT
142 { return _M_w[_Nw - 1]; }
144 _GLIBCXX23_CONSTEXPR void
145 _M_do_and(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
147 for (size_t __i = 0; __i < _Nw; __i++)
148 _M_w[__i] &= __x._M_w[__i];
151 _GLIBCXX14_CONSTEXPR void
152 _M_do_or(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
154 for (size_t __i = 0; __i < _Nw; __i++)
155 _M_w[__i] |= __x._M_w[__i];
158 _GLIBCXX14_CONSTEXPR void
159 _M_do_xor(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
161 for (size_t __i = 0; __i < _Nw; __i++)
162 _M_w[__i] ^= __x._M_w[__i];
165 _GLIBCXX14_CONSTEXPR void
166 _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT;
168 _GLIBCXX14_CONSTEXPR void
169 _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT;
171 _GLIBCXX14_CONSTEXPR void
172 _M_do_flip() _GLIBCXX_NOEXCEPT
174 for (size_t __i = 0; __i < _Nw; __i++)
175 _M_w[__i] = ~_M_w[__i];
178 _GLIBCXX14_CONSTEXPR void
179 _M_do_set() _GLIBCXX_NOEXCEPT
181 for (size_t __i = 0; __i < _Nw; __i++)
182 _M_w[__i] = ~static_cast<_WordT>(0);
185 _GLIBCXX14_CONSTEXPR void
186 _M_do_reset() _GLIBCXX_NOEXCEPT
188#if __cplusplus >= 201402L
189 if (__builtin_is_constant_evaluated())
191 for (_WordT& __w : _M_w)
196 __builtin_memset(_M_w, 0, _Nw * sizeof(_WordT));
199 _GLIBCXX14_CONSTEXPR bool
200 _M_is_equal(const _Base_bitset<_Nw>& __x) const _GLIBCXX_NOEXCEPT
202 for (size_t __i = 0; __i < _Nw; ++__i)
203 if (_M_w[__i] != __x._M_w[__i])
209 _GLIBCXX14_CONSTEXPR bool
210 _M_are_all() const _GLIBCXX_NOEXCEPT
212 for (size_t __i = 0; __i < _Nw - 1; __i++)
213 if (_M_w[__i] != ~static_cast<_WordT>(0))
215 return _M_hiword() == (~static_cast<_WordT>(0)
216 >> (_Nw * _GLIBCXX_BITSET_BITS_PER_WORD
220 _GLIBCXX14_CONSTEXPR bool
221 _M_is_any() const _GLIBCXX_NOEXCEPT
223 for (size_t __i = 0; __i < _Nw; __i++)
224 if (_M_w[__i] != static_cast<_WordT>(0))
229 _GLIBCXX14_CONSTEXPR size_t
230 _M_do_count() const _GLIBCXX_NOEXCEPT
233 for (size_t __i = 0; __i < _Nw; __i++)
234 __result += __builtin_popcountl(_M_w[__i]);
238 _GLIBCXX14_CONSTEXPR unsigned long
239 _M_do_to_ulong() const;
241#if __cplusplus >= 201103L
242 _GLIBCXX14_CONSTEXPR unsigned long long
243 _M_do_to_ullong() const;
246 // find first "on" bit
247 _GLIBCXX14_CONSTEXPR size_t
248 _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT;
250 // find the next "on" bit that follows "prev"
251 _GLIBCXX14_CONSTEXPR size_t
252 _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT;
255 // Definitions of non-inline functions from _Base_bitset.
257 _GLIBCXX14_CONSTEXPR void
258 _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT
260 if (__builtin_expect(__shift != 0, 1))
262 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
263 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
266 for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
267 _M_w[__n] = _M_w[__n - __wshift];
270 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
272 for (size_t __n = _Nw - 1; __n > __wshift; --__n)
273 _M_w[__n] = ((_M_w[__n - __wshift] << __offset)
274 | (_M_w[__n - __wshift - 1] >> __sub_offset));
275 _M_w[__wshift] = _M_w[0] << __offset;
278 std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
283 _GLIBCXX14_CONSTEXPR void
284 _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT
286 if (__builtin_expect(__shift != 0, 1))
288 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
289 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
290 const size_t __limit = _Nw - __wshift - 1;
293 for (size_t __n = 0; __n <= __limit; ++__n)
294 _M_w[__n] = _M_w[__n + __wshift];
297 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
299 for (size_t __n = 0; __n < __limit; ++__n)
300 _M_w[__n] = ((_M_w[__n + __wshift] >> __offset)
301 | (_M_w[__n + __wshift + 1] << __sub_offset));
302 _M_w[__limit] = _M_w[_Nw-1] >> __offset;
305 std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
310 _GLIBCXX14_CONSTEXPR unsigned long
311 _Base_bitset<_Nw>::_M_do_to_ulong() const
313 for (size_t __i = 1; __i < _Nw; ++__i)
315 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
319#if __cplusplus >= 201103L
321 _GLIBCXX14_CONSTEXPR unsigned long long
322 _Base_bitset<_Nw>::_M_do_to_ullong() const
324 const bool __dw = sizeof(unsigned long long) > sizeof(unsigned long);
325 for (size_t __i = 1 + __dw; __i < _Nw; ++__i)
327 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ullong"));
330 return _M_w[0] + (static_cast<unsigned long long>(_M_w[1])
331 << _GLIBCXX_BITSET_BITS_PER_WORD);
337 _GLIBCXX14_CONSTEXPR size_t
339 _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT
341 for (size_t __i = 0; __i < _Nw; __i++)
343 _WordT __thisword = _M_w[__i];
344 if (__thisword != static_cast<_WordT>(0))
345 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
346 + __builtin_ctzl(__thisword));
348 // not found, so return an indication of failure.
353 _GLIBCXX14_CONSTEXPR size_t
355 _M_do_find_next(size_t __prev, size_t __not_found) const _GLIBCXX_NOEXCEPT
357 // make bound inclusive
360 // check out of bounds
361 if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
365 size_t __i = _S_whichword(__prev);
366 _WordT __thisword = _M_w[__i];
368 // mask off bits below bound
369 __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
371 if (__thisword != static_cast<_WordT>(0))
372 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
373 + __builtin_ctzl(__thisword));
375 // check subsequent words
377 for (; __i < _Nw; __i++)
379 __thisword = _M_w[__i];
380 if (__thisword != static_cast<_WordT>(0))
381 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
382 + __builtin_ctzl(__thisword));
384 // not found, so return an indication of failure.
386 } // end _M_do_find_next
389 * Base class, specialization for a single word.
391 * See documentation for bitset.
394 struct _Base_bitset<1>
396 typedef unsigned long _WordT;
399 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
403#if __cplusplus >= 201103L
404 constexpr _Base_bitset(unsigned long long __val) noexcept
406 _Base_bitset(unsigned long __val)
411 static _GLIBCXX_CONSTEXPR size_t
412 _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
413 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
415 static _GLIBCXX_CONSTEXPR size_t
416 _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
417 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
419 static _GLIBCXX_CONSTEXPR size_t
420 _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
421 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
423 static _GLIBCXX_CONSTEXPR _WordT
424 _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
425 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
427 _GLIBCXX14_CONSTEXPR _WordT&
428 _M_getword(size_t) _GLIBCXX_NOEXCEPT
431 _GLIBCXX_CONSTEXPR _WordT
432 _M_getword(size_t) const _GLIBCXX_NOEXCEPT
435#if __cplusplus >= 201103L
436 constexpr const _WordT*
437 _M_getdata() const noexcept
441 _GLIBCXX14_CONSTEXPR _WordT&
442 _M_hiword() _GLIBCXX_NOEXCEPT
445 _GLIBCXX_CONSTEXPR _WordT
446 _M_hiword() const _GLIBCXX_NOEXCEPT
449 _GLIBCXX14_CONSTEXPR void
450 _M_do_and(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
451 { _M_w &= __x._M_w; }
453 _GLIBCXX14_CONSTEXPR void
454 _M_do_or(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
455 { _M_w |= __x._M_w; }
457 _GLIBCXX14_CONSTEXPR void
458 _M_do_xor(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
459 { _M_w ^= __x._M_w; }
461 _GLIBCXX14_CONSTEXPR void
462 _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT
463 { _M_w <<= __shift; }
465 _GLIBCXX14_CONSTEXPR void
466 _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT
467 { _M_w >>= __shift; }
469 _GLIBCXX14_CONSTEXPR void
470 _M_do_flip() _GLIBCXX_NOEXCEPT
473 _GLIBCXX14_CONSTEXPR void
474 _M_do_set() _GLIBCXX_NOEXCEPT
475 { _M_w = ~static_cast<_WordT>(0); }
477 _GLIBCXX14_CONSTEXPR void
478 _M_do_reset() _GLIBCXX_NOEXCEPT
481 _GLIBCXX14_CONSTEXPR bool
482 _M_is_equal(const _Base_bitset<1>& __x) const _GLIBCXX_NOEXCEPT
483 { return _M_w == __x._M_w; }
486 _GLIBCXX14_CONSTEXPR bool
487 _M_are_all() const _GLIBCXX_NOEXCEPT
488 { return _M_w == (~static_cast<_WordT>(0)
489 >> (_GLIBCXX_BITSET_BITS_PER_WORD - _Nb)); }
491 _GLIBCXX14_CONSTEXPR bool
492 _M_is_any() const _GLIBCXX_NOEXCEPT
493 { return _M_w != 0; }
495 _GLIBCXX14_CONSTEXPR size_t
496 _M_do_count() const _GLIBCXX_NOEXCEPT
497 { return __builtin_popcountl(_M_w); }
499 _GLIBCXX14_CONSTEXPR unsigned long
500 _M_do_to_ulong() const _GLIBCXX_NOEXCEPT
503#if __cplusplus >= 201103L
504 constexpr unsigned long long
505 _M_do_to_ullong() const noexcept
509 _GLIBCXX14_CONSTEXPR size_t
510 _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT
513 return __builtin_ctzl(_M_w);
518 // find the next "on" bit that follows "prev"
519 _GLIBCXX14_CONSTEXPR size_t
520 _M_do_find_next(size_t __prev, size_t __not_found) const
524 if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
527 _WordT __x = _M_w >> __prev;
529 return __builtin_ctzl(__x) + __prev;
536 * Base class, specialization for no storage (zero-length %bitset).
538 * See documentation for bitset.
541 struct _Base_bitset<0>
543 typedef unsigned long _WordT;
545 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
548#if __cplusplus >= 201103L
549 constexpr _Base_bitset(unsigned long long) noexcept
551 _Base_bitset(unsigned long)
555 static _GLIBCXX_CONSTEXPR size_t
556 _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
557 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
559 static _GLIBCXX_CONSTEXPR size_t
560 _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
561 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
563 static _GLIBCXX_CONSTEXPR size_t
564 _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
565 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
567 static _GLIBCXX_CONSTEXPR _WordT
568 _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
569 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
571 // This would normally give access to the data. The bounds-checking
572 // in the bitset class will prevent the user from getting this far,
573 // but this must fail if the user calls _Unchecked_set directly.
574 // Let's not penalize zero-length users unless they actually
575 // make an unchecked call; all the memory ugliness is therefore
576 // localized to this single should-never-get-this-far function.
577 __attribute__((__noreturn__))
579 _M_getword(size_t) _GLIBCXX_NOEXCEPT
580 { __throw_out_of_range(__N("_Base_bitset::_M_getword")); }
582 _GLIBCXX_CONSTEXPR _WordT
583 _M_getword(size_t) const _GLIBCXX_NOEXCEPT
586 _GLIBCXX_CONSTEXPR _WordT
587 _M_hiword() const _GLIBCXX_NOEXCEPT
590 _GLIBCXX14_CONSTEXPR void
591 _M_do_and(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
594 _GLIBCXX14_CONSTEXPR void
595 _M_do_or(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
598 _GLIBCXX14_CONSTEXPR void
599 _M_do_xor(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
602 _GLIBCXX14_CONSTEXPR void
603 _M_do_left_shift(size_t) _GLIBCXX_NOEXCEPT
606 _GLIBCXX14_CONSTEXPR void
607 _M_do_right_shift(size_t) _GLIBCXX_NOEXCEPT
610 _GLIBCXX14_CONSTEXPR void
611 _M_do_flip() _GLIBCXX_NOEXCEPT
614 _GLIBCXX14_CONSTEXPR void
615 _M_do_set() _GLIBCXX_NOEXCEPT
618 _GLIBCXX14_CONSTEXPR void
619 _M_do_reset() _GLIBCXX_NOEXCEPT
622 // Are all empty bitsets equal to each other? Are they equal to
623 // themselves? How to compare a thing which has no state? What is
624 // the sound of one zero-length bitset clapping?
625 _GLIBCXX_CONSTEXPR bool
626 _M_is_equal(const _Base_bitset<0>&) const _GLIBCXX_NOEXCEPT
630 _GLIBCXX_CONSTEXPR bool
631 _M_are_all() const _GLIBCXX_NOEXCEPT
634 _GLIBCXX_CONSTEXPR bool
635 _M_is_any() const _GLIBCXX_NOEXCEPT
638 _GLIBCXX_CONSTEXPR size_t
639 _M_do_count() const _GLIBCXX_NOEXCEPT
642 _GLIBCXX_CONSTEXPR unsigned long
643 _M_do_to_ulong() const _GLIBCXX_NOEXCEPT
646#if __cplusplus >= 201103L
647 constexpr unsigned long long
648 _M_do_to_ullong() const noexcept
652 // Normally "not found" is the size, but that could also be
653 // misinterpreted as an index in this corner case. Oh well.
654 _GLIBCXX_CONSTEXPR size_t
655 _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT
658 _GLIBCXX_CONSTEXPR size_t
659 _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT
664 // Helper class to zero out the unused high-order bits in the highest word.
665 template<size_t _Extrabits>
668 typedef unsigned long _WordT;
670 static _GLIBCXX14_CONSTEXPR void
671 _S_do_sanitize(_WordT& __val) _GLIBCXX_NOEXCEPT
672 { __val &= ~((~static_cast<_WordT>(0)) << _Extrabits); }
678 typedef unsigned long _WordT;
680 static _GLIBCXX14_CONSTEXPR void
681 _S_do_sanitize(_WordT) _GLIBCXX_NOEXCEPT { }
684#if __cplusplus >= 201103L
685 template<size_t _Nb, bool = (_Nb < _GLIBCXX_BITSET_BITS_PER_ULL)>
688 static constexpr unsigned long long
689 _S_do_sanitize_val(unsigned long long __val)
694 struct _Sanitize_val<_Nb, true>
696 static constexpr unsigned long long
697 _S_do_sanitize_val(unsigned long long __val)
698 { return __val & ~((~static_cast<unsigned long long>(0)) << _Nb); }
704 template<typename _CharT>
705 using __string = std::basic_string<_CharT>;
707 template<typename _CharT>
710 using size_type = size_t;
711 static constexpr size_type npos = size_type(-1);
715 static _GLIBCXX14_CONSTEXPR size_t
716 length(const _CharT* __s) noexcept
724 static constexpr bool
725 eq(_CharT __l, _CharT __r) noexcept
726 { return __l == __r; }
730 } // namespace __bitset
734 * @brief The %bitset class represents a @e fixed-size sequence of bits.
737 * (Note that %bitset does @e not meet the formal requirements of a
738 * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
740 * The template argument, @a Nb, may be any non-negative number,
741 * specifying the number of bits (e.g., "0", "12", "1024*1024").
743 * In the general unoptimized case, storage is allocated in word-sized
744 * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
745 * words will be used for storage. B - Nb%B bits are unused. (They are
746 * the high-order bits in the highest word.) It is a class invariant
747 * that those unused bits are always zero.
749 * If you think of %bitset as <em>a simple array of bits</em>, be
750 * aware that your mental picture is reversed: a %bitset behaves
751 * the same way as bits in integers do, with the bit at index 0 in
752 * the <em>least significant / right-hand</em> position, and the bit at
753 * index Nb-1 in the <em>most significant / left-hand</em> position.
754 * Thus, unlike other containers, a %bitset's index <em>counts from
755 * right to left</em>, to put it very loosely.
757 * This behavior is preserved when translating to and from strings. For
758 * example, the first line of the following program probably prints
759 * <em>b('a') is 0001100001</em> on a modern ASCII system.
763 * #include <iostream>
766 * using namespace std;
773 * cout << "b('a') is " << b << endl;
777 * string str = s.str();
778 * cout << "index 3 in the string is " << str[3] << " but\n"
779 * << "index 3 in the bitset is " << b[3] << endl;
784 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_containers.html
785 * for a description of extensions.
787 * Most of the actual code isn't contained in %bitset<> itself, but in the
788 * base class _Base_bitset. The base class works with whole words, not with
789 * individual bits. This allows us to specialize _Base_bitset for the
790 * important special case where the %bitset is only a single word.
792 * Extra confusion can result due to the fact that the storage for
793 * _Base_bitset @e is a regular array, and is indexed as such. This is
794 * carefully encapsulated.
798 : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
801 typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
802 typedef unsigned long _WordT;
805 template<class _CharT, class _Traits, class _Alloc>
808 _M_check_initial_position(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
809 size_t __position) const
811 if (__position > __s.size())
812 __throw_out_of_range_fmt(__N("bitset::bitset: __position "
813 "(which is %zu) > __s.size() "
815 __position, __s.size());
820 void _M_check(size_t __position, const char *__s) const
822 if (__position >= _Nb)
823 __throw_out_of_range_fmt(__N("%s: __position (which is %zu) "
824 ">= _Nb (which is %zu)"),
825 __s, __position, _Nb);
830 _M_do_sanitize() _GLIBCXX_NOEXCEPT
832 typedef _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD> __sanitize_type;
833 __sanitize_type::_S_do_sanitize(this->_M_hiword());
836#if __cplusplus >= 201103L
837 friend struct std::hash<bitset>;
842 * This encapsulates the concept of a single bit. An instance of this
843 * class is a proxy for an actual bit; this way the individual bit
844 * operations are done as faster word-size bitwise instructions.
846 * Most users will never need to use this class directly; conversions
847 * to and from bool are automatic and should be transparent. Overloaded
848 * operators help to preserve the illusion.
850 * (On a typical system, this <em>bit %reference</em> is 64
851 * times the size of an actual bit. Ha.)
865 reference(bitset& __b, size_t __pos) _GLIBCXX_NOEXCEPT
867 _M_wp = &__b._M_getword(__pos);
868 _M_bpos = _Base::_S_whichbit(__pos);
871#if __cplusplus >= 201103L
872 reference(const reference&) = default;
875#if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
878 ~reference() _GLIBCXX_NOEXCEPT
884 operator=(bool __x) _GLIBCXX_NOEXCEPT
887 *_M_wp |= _Base::_S_maskbit(_M_bpos);
889 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
893 // For b[i] = b[__j];
896 operator=(const reference& __j) _GLIBCXX_NOEXCEPT
898 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
899 *_M_wp |= _Base::_S_maskbit(_M_bpos);
901 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
908 operator~() const _GLIBCXX_NOEXCEPT
909 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
913 operator bool() const _GLIBCXX_NOEXCEPT
914 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
919 flip() _GLIBCXX_NOEXCEPT
921 *_M_wp ^= _Base::_S_maskbit(_M_bpos);
925 friend class reference;
927 // 23.3.5.1 constructors:
928 /// All bits set to zero.
929 _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT
932 /// Initial bits bitwise-copied from a single word (others set to zero).
933#if __cplusplus >= 201103L
934 constexpr bitset(unsigned long long __val) noexcept
935 : _Base(_Sanitize_val<_Nb>::_S_do_sanitize_val(__val)) { }
937 bitset(unsigned long __val)
939 { _M_do_sanitize(); }
944 * Use a subset of a string.
945 * @param __s A string of @a 0 and @a 1 characters.
946 * @param __position Index of the first character in @a __s to use;
948 * @throw std::out_of_range If @a pos is bigger the size of @a __s.
949 * @throw std::invalid_argument If a character appears in the string
950 * which is neither @a 0 nor @a 1.
952 template<class _CharT, class _Traits, class _Alloc>
955 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
956 size_t __position = 0)
959 _M_check_initial_position(__s, __position);
960 _M_copy_from_string(__s, __position,
961 std::basic_string<_CharT, _Traits, _Alloc>::npos,
962 _CharT('0'), _CharT('1'));
966 * Use a subset of a string.
967 * @param __s A string of @a 0 and @a 1 characters.
968 * @param __position Index of the first character in @a __s to use.
969 * @param __n The number of characters to copy.
970 * @throw std::out_of_range If @a __position is bigger the size
972 * @throw std::invalid_argument If a character appears in the string
973 * which is neither @a 0 nor @a 1.
975 template<class _CharT, class _Traits, class _Alloc>
977 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
978 size_t __position, size_t __n)
981 _M_check_initial_position(__s, __position);
982 _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1'));
985 // _GLIBCXX_RESOLVE_LIB_DEFECTS
986 // 396. what are characters zero and one.
987 template<class _CharT, class _Traits, class _Alloc>
989 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
990 size_t __position, size_t __n,
991 _CharT __zero, _CharT __one = _CharT('1'))
994 _M_check_initial_position(__s, __position);
995 _M_copy_from_string(__s, __position, __n, __zero, __one);
999#if __cplusplus >= 201103L
1001 * Construct from a character %array.
1002 * @param __str An %array of characters @a zero and @a one.
1003 * @param __n The number of characters to use.
1004 * @param __zero The character corresponding to the value 0.
1005 * @param __one The character corresponding to the value 1.
1006 * @throw std::invalid_argument If a character appears in the string
1007 * which is neither @a __zero nor @a __one.
1009 template<typename _CharT>
1010 [[__gnu__::__nonnull__]]
1011 _GLIBCXX23_CONSTEXPR
1013 bitset(const _CharT* __str,
1014 typename __bitset::__string<_CharT>::size_type __n
1015 = __bitset::__string<_CharT>::npos,
1016 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
1021 __throw_logic_error(__N("bitset::bitset(const _CharT*, ...)"));
1023 using _Traits = typename __bitset::__string<_CharT>::traits_type;
1025 if (__n == __bitset::__string<_CharT>::npos)
1026 __n = _Traits::length(__str);
1027 _M_copy_from_ptr<_CharT, _Traits>(__str, __n, 0, __n, __zero, __one);
1031 // 23.3.5.2 bitset operations:
1034 * Operations on bitsets.
1035 * @param __rhs A same-sized bitset.
1037 * These should be self-explanatory.
1039 _GLIBCXX23_CONSTEXPR
1041 operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
1043 this->_M_do_and(__rhs);
1047 _GLIBCXX23_CONSTEXPR
1049 operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
1051 this->_M_do_or(__rhs);
1055 _GLIBCXX23_CONSTEXPR
1057 operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
1059 this->_M_do_xor(__rhs);
1066 * Operations on bitsets.
1067 * @param __position The number of places to shift.
1069 * These should be self-explanatory.
1071 _GLIBCXX23_CONSTEXPR
1073 operator<<=(size_t __position) _GLIBCXX_NOEXCEPT
1075 if (__builtin_expect(__position < _Nb, 1))
1077 this->_M_do_left_shift(__position);
1078 this->_M_do_sanitize();
1081 this->_M_do_reset();
1085 _GLIBCXX23_CONSTEXPR
1087 operator>>=(size_t __position) _GLIBCXX_NOEXCEPT
1089 if (__builtin_expect(__position < _Nb, 1))
1091 this->_M_do_right_shift(__position);
1092 this->_M_do_sanitize();
1095 this->_M_do_reset();
1102 * These versions of single-bit set, reset, flip, and test are
1103 * extensions from the SGI version. They do no range checking.
1104 * @ingroup SGIextensions
1106 _GLIBCXX23_CONSTEXPR
1108 _Unchecked_set(size_t __pos) _GLIBCXX_NOEXCEPT
1110 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
1114 _GLIBCXX23_CONSTEXPR
1116 _Unchecked_set(size_t __pos, int __val) _GLIBCXX_NOEXCEPT
1119 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
1121 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1125 _GLIBCXX23_CONSTEXPR
1127 _Unchecked_reset(size_t __pos) _GLIBCXX_NOEXCEPT
1129 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1133 _GLIBCXX23_CONSTEXPR
1135 _Unchecked_flip(size_t __pos) _GLIBCXX_NOEXCEPT
1137 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
1141 _GLIBCXX_CONSTEXPR bool
1142 _Unchecked_test(size_t __pos) const _GLIBCXX_NOEXCEPT
1143 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
1144 != static_cast<_WordT>(0)); }
1147 // Set, reset, and flip.
1149 * @brief Sets every bit to true.
1151 _GLIBCXX23_CONSTEXPR
1153 set() _GLIBCXX_NOEXCEPT
1156 this->_M_do_sanitize();
1161 * @brief Sets a given bit to a particular value.
1162 * @param __position The index of the bit.
1163 * @param __val Either true or false, defaults to true.
1164 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1166 _GLIBCXX23_CONSTEXPR
1168 set(size_t __position, bool __val = true)
1170 this->_M_check(__position, __N("bitset::set"));
1171 return _Unchecked_set(__position, __val);
1175 * @brief Sets every bit to false.
1177 _GLIBCXX23_CONSTEXPR
1179 reset() _GLIBCXX_NOEXCEPT
1181 this->_M_do_reset();
1186 * @brief Sets a given bit to false.
1187 * @param __position The index of the bit.
1188 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1190 * Same as writing @c set(pos,false).
1192 _GLIBCXX23_CONSTEXPR
1194 reset(size_t __position)
1196 this->_M_check(__position, __N("bitset::reset"));
1197 return _Unchecked_reset(__position);
1201 * @brief Toggles every bit to its opposite value.
1203 _GLIBCXX23_CONSTEXPR
1205 flip() _GLIBCXX_NOEXCEPT
1208 this->_M_do_sanitize();
1213 * @brief Toggles a given bit to its opposite value.
1214 * @param __position The index of the bit.
1215 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1217 _GLIBCXX23_CONSTEXPR
1219 flip(size_t __position)
1221 this->_M_check(__position, __N("bitset::flip"));
1222 return _Unchecked_flip(__position);
1225 /// See the no-argument flip().
1226 _GLIBCXX23_CONSTEXPR
1228 operator~() const _GLIBCXX_NOEXCEPT
1229 { return bitset<_Nb>(*this).flip(); }
1233 * @brief Array-indexing support.
1234 * @param __position Index into the %bitset.
1235 * @return A bool for a <em>const %bitset</em>. For non-const
1236 * bitsets, an instance of the reference proxy class.
1237 * @note These operators do no range checking and throw no exceptions,
1238 * as required by DR 11 to the standard.
1240 * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
1241 * resolves DR 11 (items 1 and 2), but does not do the range-checking
1242 * required by that DR's resolution. -pme
1243 * The DR has since been changed: range-checking is a precondition
1244 * (users' responsibility), and these functions must not throw. -pme
1246 _GLIBCXX23_CONSTEXPR
1248 operator[](size_t __position)
1249 { return reference(*this, __position); }
1251 _GLIBCXX_CONSTEXPR bool
1252 operator[](size_t __position) const
1253 { return _Unchecked_test(__position); }
1257 * @brief Returns a numerical interpretation of the %bitset.
1258 * @return The integral equivalent of the bits.
1259 * @throw std::overflow_error If there are too many bits to be
1260 * represented in an @c unsigned @c long.
1262 _GLIBCXX23_CONSTEXPR
1265 { return this->_M_do_to_ulong(); }
1267#if __cplusplus >= 201103L
1268 _GLIBCXX23_CONSTEXPR
1271 { return this->_M_do_to_ullong(); }
1276 * @brief Returns a character interpretation of the %bitset.
1277 * @return The string equivalent of the bits.
1279 * Note the ordering of the bits: decreasing character positions
1280 * correspond to increasing bit positions (see the main class notes for
1283 template<class _CharT, class _Traits, class _Alloc>
1284 _GLIBCXX23_CONSTEXPR
1285 std::basic_string<_CharT, _Traits, _Alloc>
1288 std::basic_string<_CharT, _Traits, _Alloc> __result;
1289 _M_copy_to_string(__result, _CharT('0'), _CharT('1'));
1293 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1294 // 396. what are characters zero and one.
1295 template<class _CharT, class _Traits, class _Alloc>
1296 _GLIBCXX23_CONSTEXPR
1297 std::basic_string<_CharT, _Traits, _Alloc>
1298 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1300 std::basic_string<_CharT, _Traits, _Alloc> __result;
1301 _M_copy_to_string(__result, __zero, __one);
1305 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1306 // 434. bitset::to_string() hard to use.
1307 template<class _CharT, class _Traits>
1308 _GLIBCXX23_CONSTEXPR
1309 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1311 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
1313 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1314 // 853. to_string needs updating with zero and one.
1315 template<class _CharT, class _Traits>
1316 _GLIBCXX23_CONSTEXPR
1317 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1318 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1319 { return to_string<_CharT, _Traits,
1320 std::allocator<_CharT> >(__zero, __one); }
1322 template<class _CharT>
1323 _GLIBCXX23_CONSTEXPR
1324 std::basic_string<_CharT, std::char_traits<_CharT>,
1325 std::allocator<_CharT> >
1328 return to_string<_CharT, std::char_traits<_CharT>,
1329 std::allocator<_CharT> >();
1332 template<class _CharT>
1333 _GLIBCXX23_CONSTEXPR
1334 std::basic_string<_CharT, std::char_traits<_CharT>,
1335 std::allocator<_CharT> >
1336 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1338 return to_string<_CharT, std::char_traits<_CharT>,
1339 std::allocator<_CharT> >(__zero, __one);
1342 _GLIBCXX23_CONSTEXPR
1343 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1346 return to_string<char, std::char_traits<char>,
1347 std::allocator<char> >();
1350 _GLIBCXX23_CONSTEXPR
1351 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1352 to_string(char __zero, char __one = '1') const
1354 return to_string<char, std::char_traits<char>,
1355 std::allocator<char> >(__zero, __one);
1359 /// Returns the number of bits which are set.
1360 _GLIBCXX23_CONSTEXPR
1362 count() const _GLIBCXX_NOEXCEPT
1363 { return this->_M_do_count(); }
1365 /// Returns the total number of bits.
1366 _GLIBCXX_CONSTEXPR size_t
1367 size() const _GLIBCXX_NOEXCEPT
1371 /// These comparisons for equality/inequality are, well, @e bitwise.
1372 _GLIBCXX23_CONSTEXPR
1374 operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
1375 { return this->_M_is_equal(__rhs); }
1377#if __cpp_impl_three_way_comparison < 201907L
1378 _GLIBCXX23_CONSTEXPR
1380 operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
1381 { return !this->_M_is_equal(__rhs); }
1386 * @brief Tests the value of a bit.
1387 * @param __position The index of a bit.
1388 * @return The value at @a pos.
1389 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1391 _GLIBCXX23_CONSTEXPR
1393 test(size_t __position) const
1395 this->_M_check(__position, __N("bitset::test"));
1396 return _Unchecked_test(__position);
1399 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1400 // DR 693. std::bitset::all() missing.
1402 * @brief Tests whether all the bits are on.
1403 * @return True if all the bits are set.
1405 _GLIBCXX23_CONSTEXPR
1407 all() const _GLIBCXX_NOEXCEPT
1408 { return this->template _M_are_all<_Nb>(); }
1411 * @brief Tests whether any of the bits are on.
1412 * @return True if at least one bit is set.
1414 _GLIBCXX23_CONSTEXPR
1416 any() const _GLIBCXX_NOEXCEPT
1417 { return this->_M_is_any(); }
1420 * @brief Tests whether any of the bits are on.
1421 * @return True if none of the bits are set.
1423 _GLIBCXX23_CONSTEXPR
1425 none() const _GLIBCXX_NOEXCEPT
1426 { return !this->_M_is_any(); }
1429 /// Self-explanatory.
1430 _GLIBCXX23_CONSTEXPR
1432 operator<<(size_t __position) const _GLIBCXX_NOEXCEPT
1433 { return bitset<_Nb>(*this) <<= __position; }
1435 _GLIBCXX23_CONSTEXPR
1437 operator>>(size_t __position) const _GLIBCXX_NOEXCEPT
1438 { return bitset<_Nb>(*this) >>= __position; }
1442 * @brief Finds the index of the first "on" bit.
1443 * @return The index of the first bit set, or size() if not found.
1444 * @ingroup SGIextensions
1447 _GLIBCXX23_CONSTEXPR
1449 _Find_first() const _GLIBCXX_NOEXCEPT
1450 { return this->_M_do_find_first(_Nb); }
1453 * @brief Finds the index of the next "on" bit after prev.
1454 * @return The index of the next bit set, or size() if not found.
1455 * @param __prev Where to start searching.
1456 * @ingroup SGIextensions
1459 _GLIBCXX23_CONSTEXPR
1461 _Find_next(size_t __prev) const _GLIBCXX_NOEXCEPT
1462 { return this->_M_do_find_next(__prev, _Nb); }
1465 // Helper functions for string operations.
1466 template<class _CharT, class _Traits>
1467 _GLIBCXX23_CONSTEXPR
1469 _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
1473 template<class _CharT, class _Traits, class _Alloc>
1474 _GLIBCXX23_CONSTEXPR
1476 _M_copy_from_string(const std::basic_string<_CharT,
1477 _Traits, _Alloc>& __s, size_t __pos, size_t __n,
1478 _CharT __zero, _CharT __one)
1479 { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n,
1482 template<class _CharT, class _Traits, class _Alloc>
1483 _GLIBCXX23_CONSTEXPR
1485 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&,
1486 _CharT, _CharT) const;
1488 template<class _CharT, class _Traits, size_t _Nb2>
1489 friend std::basic_istream<_CharT, _Traits>&
1490 operator>>(std::basic_istream<_CharT, _Traits>&, bitset<_Nb2>&);
1492 template <class _CharT, class _Traits, size_t _Nb2>
1493 friend std::basic_ostream<_CharT, _Traits>&
1494 operator<<(std::basic_ostream<_CharT, _Traits>&, const bitset<_Nb2>&);
1498 // Definitions of non-inline member functions.
1499 template<size_t _Nb>
1500 template<class _CharT, class _Traits>
1501 _GLIBCXX23_CONSTEXPR
1504 _M_copy_from_ptr(const _CharT* __s, size_t __len,
1505 size_t __pos, size_t __n, _CharT __zero, _CharT __one)
1508 const size_t __nbits = std::min(_Nb, std::min(__n, size_t(__len - __pos)));
1509 for (size_t __i = __nbits; __i > 0; --__i)
1511 const _CharT __c = __s[__pos + __nbits - __i];
1512 if (_Traits::eq(__c, __zero))
1514 else if (_Traits::eq(__c, __one))
1515 _Unchecked_set(__i - 1);
1517 __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
1522 template<size_t _Nb>
1523 template<class _CharT, class _Traits, class _Alloc>
1524 _GLIBCXX23_CONSTEXPR
1527 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s,
1528 _CharT __zero, _CharT __one) const
1530 __s.assign(_Nb, __zero);
1531 size_t __n = this->_Find_first();
1534 __s[_Nb - __n - 1] = __one;
1535 __n = _Find_next(__n);
1540 // 23.3.5.3 bitset operations:
1543 * @brief Global bitwise operations on bitsets.
1544 * @param __x A bitset.
1545 * @param __y A bitset of the same size as @a __x.
1546 * @return A new bitset.
1548 * These should be self-explanatory.
1550 template<size_t _Nb>
1551 _GLIBCXX23_CONSTEXPR
1553 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1555 bitset<_Nb> __result(__x);
1560 template<size_t _Nb>
1561 _GLIBCXX23_CONSTEXPR
1563 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1565 bitset<_Nb> __result(__x);
1570 template <size_t _Nb>
1571 _GLIBCXX23_CONSTEXPR
1573 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1575 bitset<_Nb> __result(__x);
1584 * @brief Global I/O operators for bitsets.
1586 * Direct I/O between streams and bitsets is supported. Output is
1587 * straightforward. Input will skip whitespace, only accept @a 0 and @a 1
1588 * characters, and will only extract as many digits as the %bitset will
1591 template<class _CharT, class _Traits, size_t _Nb>
1592 std::basic_istream<_CharT, _Traits>&
1593 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
1595 typedef typename _Traits::char_type char_type;
1596 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1597 typedef typename __istream_type::ios_base __ios_base;
1601 static _GLIBCXX_CONSTEXPR bool _S_use_alloca() { return _Nb <= 256; }
1603 explicit _Buffer(_CharT* __p) : _M_ptr(__p) { }
1607 if _GLIBCXX17_CONSTEXPR (!_S_use_alloca())
1611 _CharT* const _M_ptr;
1614 if _GLIBCXX17_CONSTEXPR (_Buffer::_S_use_alloca())
1615 __ptr = (_CharT*)__builtin_alloca(_Nb);
1617 __ptr = new _CharT[_Nb];
1618 const _Buffer __buf(__ptr);
1620 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1621 // 303. Bitset input operator underspecified
1622 const char_type __zero = __is.widen('0');
1623 const char_type __one = __is.widen('1');
1625 typename __ios_base::iostate __state = __ios_base::goodbit;
1626 typename __istream_type::sentry __sentry(__is);
1631 for (size_t __i = _Nb; __i > 0; --__i)
1633 static typename _Traits::int_type __eof = _Traits::eof();
1635 typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
1636 if (_Traits::eq_int_type(__c1, __eof))
1638 __state |= __ios_base::eofbit;
1643 const char_type __c2 = _Traits::to_char_type(__c1);
1644 if (_Traits::eq(__c2, __zero))
1646 else if (_Traits::eq(__c2, __one))
1649 eq_int_type(__is.rdbuf()->sputbackc(__c2),
1652 __state |= __ios_base::failbit;
1658 __catch(__cxxabiv1::__forced_unwind&)
1660 __is._M_setstate(__ios_base::badbit);
1661 __throw_exception_again;
1664 { __is._M_setstate(__ios_base::badbit); }
1667 if _GLIBCXX17_CONSTEXPR (_Nb)
1669 if (size_t __len = __ptr - __buf._M_ptr)
1670 __x.template _M_copy_from_ptr<_CharT, _Traits>(__buf._M_ptr, __len,
1674 __state |= __ios_base::failbit;
1677 __is.setstate(__state);
1681 template <class _CharT, class _Traits, size_t _Nb>
1682 std::basic_ostream<_CharT, _Traits>&
1683 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1684 const bitset<_Nb>& __x)
1686 std::basic_string<_CharT, _Traits> __tmp;
1688 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1689 // 396. what are characters zero and one.
1690 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc());
1691 __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
1692 return __os << __tmp;
1697_GLIBCXX_END_NAMESPACE_CONTAINER
1700#undef _GLIBCXX_BITSET_WORDS
1701#undef _GLIBCXX_BITSET_BITS_PER_WORD
1702#undef _GLIBCXX_BITSET_BITS_PER_ULL
1704#if __cplusplus >= 201103L
1706namespace std _GLIBCXX_VISIBILITY(default)
1708_GLIBCXX_BEGIN_NAMESPACE_VERSION
1711 /// std::hash specialization for bitset.
1712 template<size_t _Nb>
1713 struct hash<_GLIBCXX_STD_C::bitset<_Nb>>
1714 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<_Nb>>
1717 operator()(const _GLIBCXX_STD_C::bitset<_Nb>& __b) const noexcept
1719 const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__;
1720 return std::_Hash_impl::hash(__b._M_getdata(), __clength);
1725 struct hash<_GLIBCXX_STD_C::bitset<0>>
1726 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<0>>
1729 operator()(const _GLIBCXX_STD_C::bitset<0>&) const noexcept
1733_GLIBCXX_END_NAMESPACE_VERSION
1738#if defined _GLIBCXX_DEBUG && _GLIBCXX_HOSTED
1739# include <debug/bitset>
1742#endif /* _GLIBCXX_BITSET */