3// Copyright (C) 2001-2024 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_want_constexpr_bitset
62#include <bits/version.h>
64#define _GLIBCXX_BITSET_BITS_PER_WORD (__CHAR_BIT__ * __SIZEOF_LONG__)
65#define _GLIBCXX_BITSET_WORDS(__n) \
66 ((__n) / _GLIBCXX_BITSET_BITS_PER_WORD + \
67 ((__n) % _GLIBCXX_BITSET_BITS_PER_WORD == 0 ? 0 : 1))
69#define _GLIBCXX_BITSET_BITS_PER_ULL (__CHAR_BIT__ * __SIZEOF_LONG_LONG__)
71namespace std _GLIBCXX_VISIBILITY(default)
73_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
76 * Base class, general case. It is a class invariant that _Nw will be
79 * See documentation for bitset.
84 typedef unsigned long _WordT;
86 /// 0 is the least significant word.
89 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
92#if __cplusplus >= 201103L
93 constexpr _Base_bitset(unsigned long long __val) noexcept
95#if __SIZEOF_LONG_LONG__ > __SIZEOF_LONG__
96 , _WordT(__val >> _GLIBCXX_BITSET_BITS_PER_WORD)
100 _Base_bitset(unsigned long __val)
105 static _GLIBCXX_CONSTEXPR size_t
106 _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
107 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
109 static _GLIBCXX_CONSTEXPR size_t
110 _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
111 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
113 static _GLIBCXX_CONSTEXPR size_t
114 _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
115 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
117 static _GLIBCXX_CONSTEXPR _WordT
118 _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
119 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
121 _GLIBCXX14_CONSTEXPR _WordT&
122 _M_getword(size_t __pos) _GLIBCXX_NOEXCEPT
123 { return _M_w[_S_whichword(__pos)]; }
125 _GLIBCXX_CONSTEXPR _WordT
126 _M_getword(size_t __pos) const _GLIBCXX_NOEXCEPT
127 { return _M_w[_S_whichword(__pos)]; }
129#if __cplusplus >= 201103L
130 constexpr const _WordT*
131 _M_getdata() const noexcept
135 _GLIBCXX23_CONSTEXPR _WordT&
136 _M_hiword() _GLIBCXX_NOEXCEPT
137 { return _M_w[_Nw - 1]; }
139 _GLIBCXX_CONSTEXPR _WordT
140 _M_hiword() const _GLIBCXX_NOEXCEPT
141 { return _M_w[_Nw - 1]; }
143 _GLIBCXX23_CONSTEXPR void
144 _M_do_and(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
146 for (size_t __i = 0; __i < _Nw; __i++)
147 _M_w[__i] &= __x._M_w[__i];
150 _GLIBCXX14_CONSTEXPR void
151 _M_do_or(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
153 for (size_t __i = 0; __i < _Nw; __i++)
154 _M_w[__i] |= __x._M_w[__i];
157 _GLIBCXX14_CONSTEXPR void
158 _M_do_xor(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
160 for (size_t __i = 0; __i < _Nw; __i++)
161 _M_w[__i] ^= __x._M_w[__i];
164 _GLIBCXX14_CONSTEXPR void
165 _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT;
167 _GLIBCXX14_CONSTEXPR void
168 _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT;
170 _GLIBCXX14_CONSTEXPR void
171 _M_do_flip() _GLIBCXX_NOEXCEPT
173 for (size_t __i = 0; __i < _Nw; __i++)
174 _M_w[__i] = ~_M_w[__i];
177 _GLIBCXX14_CONSTEXPR void
178 _M_do_set() _GLIBCXX_NOEXCEPT
180 for (size_t __i = 0; __i < _Nw; __i++)
181 _M_w[__i] = ~static_cast<_WordT>(0);
184 _GLIBCXX14_CONSTEXPR void
185 _M_do_reset() _GLIBCXX_NOEXCEPT
187#if __cplusplus >= 201402L
188 if (__builtin_is_constant_evaluated())
190 for (_WordT& __w : _M_w)
195 __builtin_memset(_M_w, 0, _Nw * sizeof(_WordT));
198 _GLIBCXX14_CONSTEXPR bool
199 _M_is_equal(const _Base_bitset<_Nw>& __x) const _GLIBCXX_NOEXCEPT
201 for (size_t __i = 0; __i < _Nw; ++__i)
202 if (_M_w[__i] != __x._M_w[__i])
208 _GLIBCXX14_CONSTEXPR bool
209 _M_are_all() const _GLIBCXX_NOEXCEPT
211 for (size_t __i = 0; __i < _Nw - 1; __i++)
212 if (_M_w[__i] != ~static_cast<_WordT>(0))
214 return _M_hiword() == (~static_cast<_WordT>(0)
215 >> (_Nw * _GLIBCXX_BITSET_BITS_PER_WORD
219 _GLIBCXX14_CONSTEXPR bool
220 _M_is_any() const _GLIBCXX_NOEXCEPT
222 for (size_t __i = 0; __i < _Nw; __i++)
223 if (_M_w[__i] != static_cast<_WordT>(0))
228 _GLIBCXX14_CONSTEXPR size_t
229 _M_do_count() const _GLIBCXX_NOEXCEPT
232 for (size_t __i = 0; __i < _Nw; __i++)
233 __result += __builtin_popcountl(_M_w[__i]);
237 _GLIBCXX14_CONSTEXPR unsigned long
238 _M_do_to_ulong() const;
240#if __cplusplus >= 201103L
241 _GLIBCXX14_CONSTEXPR unsigned long long
242 _M_do_to_ullong() const;
245 // find first "on" bit
246 _GLIBCXX14_CONSTEXPR size_t
247 _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT;
249 // find the next "on" bit that follows "prev"
250 _GLIBCXX14_CONSTEXPR size_t
251 _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT;
254 // Definitions of non-inline functions from _Base_bitset.
256 _GLIBCXX14_CONSTEXPR void
257 _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT
259 if (__builtin_expect(__shift != 0, 1))
261 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
262 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
265 for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
266 _M_w[__n] = _M_w[__n - __wshift];
269 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
271 for (size_t __n = _Nw - 1; __n > __wshift; --__n)
272 _M_w[__n] = ((_M_w[__n - __wshift] << __offset)
273 | (_M_w[__n - __wshift - 1] >> __sub_offset));
274 _M_w[__wshift] = _M_w[0] << __offset;
277 std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
282 _GLIBCXX14_CONSTEXPR void
283 _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT
285 if (__builtin_expect(__shift != 0, 1))
287 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
288 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
289 const size_t __limit = _Nw - __wshift - 1;
292 for (size_t __n = 0; __n <= __limit; ++__n)
293 _M_w[__n] = _M_w[__n + __wshift];
296 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
298 for (size_t __n = 0; __n < __limit; ++__n)
299 _M_w[__n] = ((_M_w[__n + __wshift] >> __offset)
300 | (_M_w[__n + __wshift + 1] << __sub_offset));
301 _M_w[__limit] = _M_w[_Nw-1] >> __offset;
304 std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
309 _GLIBCXX14_CONSTEXPR unsigned long
310 _Base_bitset<_Nw>::_M_do_to_ulong() const
312 for (size_t __i = 1; __i < _Nw; ++__i)
314 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
318#if __cplusplus >= 201103L
320 _GLIBCXX14_CONSTEXPR unsigned long long
321 _Base_bitset<_Nw>::_M_do_to_ullong() const
323 const bool __dw = sizeof(unsigned long long) > sizeof(unsigned long);
324 for (size_t __i = 1 + __dw; __i < _Nw; ++__i)
326 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ullong"));
329 return _M_w[0] + (static_cast<unsigned long long>(_M_w[1])
330 << _GLIBCXX_BITSET_BITS_PER_WORD);
336 _GLIBCXX14_CONSTEXPR size_t
338 _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT
340 for (size_t __i = 0; __i < _Nw; __i++)
342 _WordT __thisword = _M_w[__i];
343 if (__thisword != static_cast<_WordT>(0))
344 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
345 + __builtin_ctzl(__thisword));
347 // not found, so return an indication of failure.
352 _GLIBCXX14_CONSTEXPR size_t
354 _M_do_find_next(size_t __prev, size_t __not_found) const _GLIBCXX_NOEXCEPT
356 // make bound inclusive
359 // check out of bounds
360 if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
364 size_t __i = _S_whichword(__prev);
365 _WordT __thisword = _M_w[__i];
367 // mask off bits below bound
368 __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
370 if (__thisword != static_cast<_WordT>(0))
371 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
372 + __builtin_ctzl(__thisword));
374 // check subsequent words
376 for (; __i < _Nw; __i++)
378 __thisword = _M_w[__i];
379 if (__thisword != static_cast<_WordT>(0))
380 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
381 + __builtin_ctzl(__thisword));
383 // not found, so return an indication of failure.
385 } // end _M_do_find_next
388 * Base class, specialization for a single word.
390 * See documentation for bitset.
393 struct _Base_bitset<1>
395 typedef unsigned long _WordT;
398 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
402#if __cplusplus >= 201103L
403 constexpr _Base_bitset(unsigned long long __val) noexcept
405 _Base_bitset(unsigned long __val)
410 static _GLIBCXX_CONSTEXPR size_t
411 _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
412 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
414 static _GLIBCXX_CONSTEXPR size_t
415 _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
416 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
418 static _GLIBCXX_CONSTEXPR size_t
419 _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
420 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
422 static _GLIBCXX_CONSTEXPR _WordT
423 _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
424 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
426 _GLIBCXX14_CONSTEXPR _WordT&
427 _M_getword(size_t) _GLIBCXX_NOEXCEPT
430 _GLIBCXX_CONSTEXPR _WordT
431 _M_getword(size_t) const _GLIBCXX_NOEXCEPT
434#if __cplusplus >= 201103L
435 constexpr const _WordT*
436 _M_getdata() const noexcept
440 _GLIBCXX14_CONSTEXPR _WordT&
441 _M_hiword() _GLIBCXX_NOEXCEPT
444 _GLIBCXX_CONSTEXPR _WordT
445 _M_hiword() const _GLIBCXX_NOEXCEPT
448 _GLIBCXX14_CONSTEXPR void
449 _M_do_and(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
450 { _M_w &= __x._M_w; }
452 _GLIBCXX14_CONSTEXPR void
453 _M_do_or(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
454 { _M_w |= __x._M_w; }
456 _GLIBCXX14_CONSTEXPR void
457 _M_do_xor(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
458 { _M_w ^= __x._M_w; }
460 _GLIBCXX14_CONSTEXPR void
461 _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT
462 { _M_w <<= __shift; }
464 _GLIBCXX14_CONSTEXPR void
465 _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT
466 { _M_w >>= __shift; }
468 _GLIBCXX14_CONSTEXPR void
469 _M_do_flip() _GLIBCXX_NOEXCEPT
472 _GLIBCXX14_CONSTEXPR void
473 _M_do_set() _GLIBCXX_NOEXCEPT
474 { _M_w = ~static_cast<_WordT>(0); }
476 _GLIBCXX14_CONSTEXPR void
477 _M_do_reset() _GLIBCXX_NOEXCEPT
480 _GLIBCXX14_CONSTEXPR bool
481 _M_is_equal(const _Base_bitset<1>& __x) const _GLIBCXX_NOEXCEPT
482 { return _M_w == __x._M_w; }
485 _GLIBCXX14_CONSTEXPR bool
486 _M_are_all() const _GLIBCXX_NOEXCEPT
487 { return _M_w == (~static_cast<_WordT>(0)
488 >> (_GLIBCXX_BITSET_BITS_PER_WORD - _Nb)); }
490 _GLIBCXX14_CONSTEXPR bool
491 _M_is_any() const _GLIBCXX_NOEXCEPT
492 { return _M_w != 0; }
494 _GLIBCXX14_CONSTEXPR size_t
495 _M_do_count() const _GLIBCXX_NOEXCEPT
496 { return __builtin_popcountl(_M_w); }
498 _GLIBCXX14_CONSTEXPR unsigned long
499 _M_do_to_ulong() const _GLIBCXX_NOEXCEPT
502#if __cplusplus >= 201103L
503 constexpr unsigned long long
504 _M_do_to_ullong() const noexcept
508 _GLIBCXX14_CONSTEXPR size_t
509 _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT
512 return __builtin_ctzl(_M_w);
517 // find the next "on" bit that follows "prev"
518 _GLIBCXX14_CONSTEXPR size_t
519 _M_do_find_next(size_t __prev, size_t __not_found) const
523 if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
526 _WordT __x = _M_w >> __prev;
528 return __builtin_ctzl(__x) + __prev;
535 * Base class, specialization for no storage (zero-length %bitset).
537 * See documentation for bitset.
540 struct _Base_bitset<0>
542 typedef unsigned long _WordT;
544 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
547#if __cplusplus >= 201103L
548 constexpr _Base_bitset(unsigned long long) noexcept
550 _Base_bitset(unsigned long)
554 static _GLIBCXX_CONSTEXPR size_t
555 _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
556 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
558 static _GLIBCXX_CONSTEXPR size_t
559 _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
560 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
562 static _GLIBCXX_CONSTEXPR size_t
563 _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
564 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
566 static _GLIBCXX_CONSTEXPR _WordT
567 _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
568 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
570 // This would normally give access to the data. The bounds-checking
571 // in the bitset class will prevent the user from getting this far,
572 // but this must fail if the user calls _Unchecked_set directly.
573 // Let's not penalize zero-length users unless they actually
574 // make an unchecked call; all the memory ugliness is therefore
575 // localized to this single should-never-get-this-far function.
576 __attribute__((__noreturn__))
578 _M_getword(size_t) _GLIBCXX_NOEXCEPT
579 { __throw_out_of_range(__N("_Base_bitset::_M_getword")); }
581 _GLIBCXX_CONSTEXPR _WordT
582 _M_getword(size_t) const _GLIBCXX_NOEXCEPT
585 _GLIBCXX_CONSTEXPR _WordT
586 _M_hiword() const _GLIBCXX_NOEXCEPT
589 _GLIBCXX14_CONSTEXPR void
590 _M_do_and(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
593 _GLIBCXX14_CONSTEXPR void
594 _M_do_or(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
597 _GLIBCXX14_CONSTEXPR void
598 _M_do_xor(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
601 _GLIBCXX14_CONSTEXPR void
602 _M_do_left_shift(size_t) _GLIBCXX_NOEXCEPT
605 _GLIBCXX14_CONSTEXPR void
606 _M_do_right_shift(size_t) _GLIBCXX_NOEXCEPT
609 _GLIBCXX14_CONSTEXPR void
610 _M_do_flip() _GLIBCXX_NOEXCEPT
613 _GLIBCXX14_CONSTEXPR void
614 _M_do_set() _GLIBCXX_NOEXCEPT
617 _GLIBCXX14_CONSTEXPR void
618 _M_do_reset() _GLIBCXX_NOEXCEPT
621 // Are all empty bitsets equal to each other? Are they equal to
622 // themselves? How to compare a thing which has no state? What is
623 // the sound of one zero-length bitset clapping?
624 _GLIBCXX_CONSTEXPR bool
625 _M_is_equal(const _Base_bitset<0>&) const _GLIBCXX_NOEXCEPT
629 _GLIBCXX_CONSTEXPR bool
630 _M_are_all() const _GLIBCXX_NOEXCEPT
633 _GLIBCXX_CONSTEXPR bool
634 _M_is_any() const _GLIBCXX_NOEXCEPT
637 _GLIBCXX_CONSTEXPR size_t
638 _M_do_count() const _GLIBCXX_NOEXCEPT
641 _GLIBCXX_CONSTEXPR unsigned long
642 _M_do_to_ulong() const _GLIBCXX_NOEXCEPT
645#if __cplusplus >= 201103L
646 constexpr unsigned long long
647 _M_do_to_ullong() const noexcept
651 // Normally "not found" is the size, but that could also be
652 // misinterpreted as an index in this corner case. Oh well.
653 _GLIBCXX_CONSTEXPR size_t
654 _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT
657 _GLIBCXX_CONSTEXPR size_t
658 _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT
663 // Helper class to zero out the unused high-order bits in the highest word.
664 template<size_t _Extrabits>
667 typedef unsigned long _WordT;
669 static _GLIBCXX14_CONSTEXPR void
670 _S_do_sanitize(_WordT& __val) _GLIBCXX_NOEXCEPT
671 { __val &= ~((~static_cast<_WordT>(0)) << _Extrabits); }
677 typedef unsigned long _WordT;
679 static _GLIBCXX14_CONSTEXPR void
680 _S_do_sanitize(_WordT) _GLIBCXX_NOEXCEPT { }
683#if __cplusplus >= 201103L
684 template<size_t _Nb, bool = (_Nb < _GLIBCXX_BITSET_BITS_PER_ULL)>
687 static constexpr unsigned long long
688 _S_do_sanitize_val(unsigned long long __val)
693 struct _Sanitize_val<_Nb, true>
695 static constexpr unsigned long long
696 _S_do_sanitize_val(unsigned long long __val)
697 { return __val & ~((~static_cast<unsigned long long>(0)) << _Nb); }
703 template<typename _CharT>
704 using __string = std::basic_string<_CharT>;
706 template<typename _CharT>
709 using size_type = size_t;
710 static constexpr size_type npos = size_type(-1);
714 static _GLIBCXX14_CONSTEXPR size_t
715 length(const _CharT* __s) noexcept
723 static constexpr bool
724 eq(_CharT __l, _CharT __r) noexcept
725 { return __l == __r; }
729 } // namespace __bitset
733 * @brief The %bitset class represents a @e fixed-size sequence of bits.
736 * (Note that %bitset does @e not meet the formal requirements of a
737 * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
739 * The template argument, @a Nb, may be any non-negative number,
740 * specifying the number of bits (e.g., "0", "12", "1024*1024").
742 * In the general unoptimized case, storage is allocated in word-sized
743 * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
744 * words will be used for storage. B - Nb%B bits are unused. (They are
745 * the high-order bits in the highest word.) It is a class invariant
746 * that those unused bits are always zero.
748 * If you think of %bitset as <em>a simple array of bits</em>, be
749 * aware that your mental picture is reversed: a %bitset behaves
750 * the same way as bits in integers do, with the bit at index 0 in
751 * the <em>least significant / right-hand</em> position, and the bit at
752 * index Nb-1 in the <em>most significant / left-hand</em> position.
753 * Thus, unlike other containers, a %bitset's index <em>counts from
754 * right to left</em>, to put it very loosely.
756 * This behavior is preserved when translating to and from strings. For
757 * example, the first line of the following program probably prints
758 * <em>b('a') is 0001100001</em> on a modern ASCII system.
762 * #include <iostream>
765 * using namespace std;
772 * cout << "b('a') is " << b << endl;
776 * string str = s.str();
777 * cout << "index 3 in the string is " << str[3] << " but\n"
778 * << "index 3 in the bitset is " << b[3] << endl;
783 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_containers.html
784 * for a description of extensions.
786 * Most of the actual code isn't contained in %bitset<> itself, but in the
787 * base class _Base_bitset. The base class works with whole words, not with
788 * individual bits. This allows us to specialize _Base_bitset for the
789 * important special case where the %bitset is only a single word.
791 * Extra confusion can result due to the fact that the storage for
792 * _Base_bitset @e is a regular array, and is indexed as such. This is
793 * carefully encapsulated.
797 : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
800 typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
801 typedef unsigned long _WordT;
804 template<class _CharT, class _Traits, class _Alloc>
807 _M_check_initial_position(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
808 size_t __position) const
810 if (__position > __s.size())
811 __throw_out_of_range_fmt(__N("bitset::bitset: __position "
812 "(which is %zu) > __s.size() "
814 __position, __s.size());
819 void _M_check(size_t __position, const char *__s) const
821 if (__position >= _Nb)
822 __throw_out_of_range_fmt(__N("%s: __position (which is %zu) "
823 ">= _Nb (which is %zu)"),
824 __s, __position, _Nb);
829 _M_do_sanitize() _GLIBCXX_NOEXCEPT
831 typedef _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD> __sanitize_type;
832 __sanitize_type::_S_do_sanitize(this->_M_hiword());
835#if __cplusplus >= 201103L
836 friend struct std::hash<bitset>;
841 * This encapsulates the concept of a single bit. An instance of this
842 * class is a proxy for an actual bit; this way the individual bit
843 * operations are done as faster word-size bitwise instructions.
845 * Most users will never need to use this class directly; conversions
846 * to and from bool are automatic and should be transparent. Overloaded
847 * operators help to preserve the illusion.
849 * (On a typical system, this <em>bit %reference</em> is 64
850 * times the size of an actual bit. Ha.)
864 reference(bitset& __b, size_t __pos) _GLIBCXX_NOEXCEPT
866 _M_wp = &__b._M_getword(__pos);
867 _M_bpos = _Base::_S_whichbit(__pos);
870#if __cplusplus >= 201103L
871 reference(const reference&) = default;
874#if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
877 ~reference() _GLIBCXX_NOEXCEPT
883 operator=(bool __x) _GLIBCXX_NOEXCEPT
886 *_M_wp |= _Base::_S_maskbit(_M_bpos);
888 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
892 // For b[i] = b[__j];
895 operator=(const reference& __j) _GLIBCXX_NOEXCEPT
897 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
898 *_M_wp |= _Base::_S_maskbit(_M_bpos);
900 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
907 operator~() const _GLIBCXX_NOEXCEPT
908 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
912 operator bool() const _GLIBCXX_NOEXCEPT
913 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
918 flip() _GLIBCXX_NOEXCEPT
920 *_M_wp ^= _Base::_S_maskbit(_M_bpos);
924 friend class reference;
926 // 23.3.5.1 constructors:
927 /// All bits set to zero.
928 _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT
931 /// Initial bits bitwise-copied from a single word (others set to zero).
932#if __cplusplus >= 201103L
933 constexpr bitset(unsigned long long __val) noexcept
934 : _Base(_Sanitize_val<_Nb>::_S_do_sanitize_val(__val)) { }
936 bitset(unsigned long __val)
938 { _M_do_sanitize(); }
943 * Use a subset of a string.
944 * @param __s A string of @a 0 and @a 1 characters.
945 * @param __position Index of the first character in @a __s to use;
947 * @throw std::out_of_range If @a pos is bigger the size of @a __s.
948 * @throw std::invalid_argument If a character appears in the string
949 * which is neither @a 0 nor @a 1.
951 template<class _CharT, class _Traits, class _Alloc>
954 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
955 size_t __position = 0)
958 _M_check_initial_position(__s, __position);
959 _M_copy_from_string(__s, __position,
960 std::basic_string<_CharT, _Traits, _Alloc>::npos,
961 _CharT('0'), _CharT('1'));
965 * Use a subset of a string.
966 * @param __s A string of @a 0 and @a 1 characters.
967 * @param __position Index of the first character in @a __s to use.
968 * @param __n The number of characters to copy.
969 * @throw std::out_of_range If @a __position is bigger the size
971 * @throw std::invalid_argument If a character appears in the string
972 * which is neither @a 0 nor @a 1.
974 template<class _CharT, class _Traits, class _Alloc>
976 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
977 size_t __position, size_t __n)
980 _M_check_initial_position(__s, __position);
981 _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1'));
984 // _GLIBCXX_RESOLVE_LIB_DEFECTS
985 // 396. what are characters zero and one.
986 template<class _CharT, class _Traits, class _Alloc>
988 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
989 size_t __position, size_t __n,
990 _CharT __zero, _CharT __one = _CharT('1'))
993 _M_check_initial_position(__s, __position);
994 _M_copy_from_string(__s, __position, __n, __zero, __one);
998#if __cplusplus >= 201103L
1000 * Construct from a character %array.
1001 * @param __str An %array of characters @a zero and @a one.
1002 * @param __n The number of characters to use.
1003 * @param __zero The character corresponding to the value 0.
1004 * @param __one The character corresponding to the value 1.
1005 * @throw std::invalid_argument If a character appears in the string
1006 * which is neither @a __zero nor @a __one.
1008 template<typename _CharT>
1009 [[__gnu__::__nonnull__]]
1010 _GLIBCXX23_CONSTEXPR
1012 bitset(const _CharT* __str,
1013 typename __bitset::__string<_CharT>::size_type __n
1014 = __bitset::__string<_CharT>::npos,
1015 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
1020 __throw_logic_error(__N("bitset::bitset(const _CharT*, ...)"));
1022 using _Traits = typename __bitset::__string<_CharT>::traits_type;
1024 if (__n == __bitset::__string<_CharT>::npos)
1025 __n = _Traits::length(__str);
1026 _M_copy_from_ptr<_CharT, _Traits>(__str, __n, 0, __n, __zero, __one);
1030 // 23.3.5.2 bitset operations:
1033 * Operations on bitsets.
1034 * @param __rhs A same-sized bitset.
1036 * These should be self-explanatory.
1038 _GLIBCXX23_CONSTEXPR
1040 operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
1042 this->_M_do_and(__rhs);
1046 _GLIBCXX23_CONSTEXPR
1048 operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
1050 this->_M_do_or(__rhs);
1054 _GLIBCXX23_CONSTEXPR
1056 operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
1058 this->_M_do_xor(__rhs);
1065 * Operations on bitsets.
1066 * @param __position The number of places to shift.
1068 * These should be self-explanatory.
1070 _GLIBCXX23_CONSTEXPR
1072 operator<<=(size_t __position) _GLIBCXX_NOEXCEPT
1074 if (__builtin_expect(__position < _Nb, 1))
1076 this->_M_do_left_shift(__position);
1077 this->_M_do_sanitize();
1080 this->_M_do_reset();
1084 _GLIBCXX23_CONSTEXPR
1086 operator>>=(size_t __position) _GLIBCXX_NOEXCEPT
1088 if (__builtin_expect(__position < _Nb, 1))
1090 this->_M_do_right_shift(__position);
1091 this->_M_do_sanitize();
1094 this->_M_do_reset();
1101 * These versions of single-bit set, reset, flip, and test are
1102 * extensions from the SGI version. They do no range checking.
1103 * @ingroup SGIextensions
1105 _GLIBCXX23_CONSTEXPR
1107 _Unchecked_set(size_t __pos) _GLIBCXX_NOEXCEPT
1109 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
1113 _GLIBCXX23_CONSTEXPR
1115 _Unchecked_set(size_t __pos, int __val) _GLIBCXX_NOEXCEPT
1118 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
1120 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1124 _GLIBCXX23_CONSTEXPR
1126 _Unchecked_reset(size_t __pos) _GLIBCXX_NOEXCEPT
1128 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1132 _GLIBCXX23_CONSTEXPR
1134 _Unchecked_flip(size_t __pos) _GLIBCXX_NOEXCEPT
1136 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
1140 _GLIBCXX_CONSTEXPR bool
1141 _Unchecked_test(size_t __pos) const _GLIBCXX_NOEXCEPT
1142 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
1143 != static_cast<_WordT>(0)); }
1146 // Set, reset, and flip.
1148 * @brief Sets every bit to true.
1150 _GLIBCXX23_CONSTEXPR
1152 set() _GLIBCXX_NOEXCEPT
1155 this->_M_do_sanitize();
1160 * @brief Sets a given bit to a particular value.
1161 * @param __position The index of the bit.
1162 * @param __val Either true or false, defaults to true.
1163 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1165 _GLIBCXX23_CONSTEXPR
1167 set(size_t __position, bool __val = true)
1169 this->_M_check(__position, __N("bitset::set"));
1170 return _Unchecked_set(__position, __val);
1174 * @brief Sets every bit to false.
1176 _GLIBCXX23_CONSTEXPR
1178 reset() _GLIBCXX_NOEXCEPT
1180 this->_M_do_reset();
1185 * @brief Sets a given bit to false.
1186 * @param __position The index of the bit.
1187 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1189 * Same as writing @c set(pos,false).
1191 _GLIBCXX23_CONSTEXPR
1193 reset(size_t __position)
1195 this->_M_check(__position, __N("bitset::reset"));
1196 return _Unchecked_reset(__position);
1200 * @brief Toggles every bit to its opposite value.
1202 _GLIBCXX23_CONSTEXPR
1204 flip() _GLIBCXX_NOEXCEPT
1207 this->_M_do_sanitize();
1212 * @brief Toggles a given bit to its opposite value.
1213 * @param __position The index of the bit.
1214 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1216 _GLIBCXX23_CONSTEXPR
1218 flip(size_t __position)
1220 this->_M_check(__position, __N("bitset::flip"));
1221 return _Unchecked_flip(__position);
1224 /// See the no-argument flip().
1225 _GLIBCXX23_CONSTEXPR
1227 operator~() const _GLIBCXX_NOEXCEPT
1228 { return bitset<_Nb>(*this).flip(); }
1232 * @brief Array-indexing support.
1233 * @param __position Index into the %bitset.
1234 * @return A bool for a <em>const %bitset</em>. For non-const
1235 * bitsets, an instance of the reference proxy class.
1236 * @note These operators do no range checking and throw no exceptions,
1237 * as required by DR 11 to the standard.
1239 * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
1240 * resolves DR 11 (items 1 and 2), but does not do the range-checking
1241 * required by that DR's resolution. -pme
1242 * The DR has since been changed: range-checking is a precondition
1243 * (users' responsibility), and these functions must not throw. -pme
1245 _GLIBCXX23_CONSTEXPR
1247 operator[](size_t __position)
1248 { return reference(*this, __position); }
1250 _GLIBCXX_CONSTEXPR bool
1251 operator[](size_t __position) const
1252 { return _Unchecked_test(__position); }
1256 * @brief Returns a numerical interpretation of the %bitset.
1257 * @return The integral equivalent of the bits.
1258 * @throw std::overflow_error If there are too many bits to be
1259 * represented in an @c unsigned @c long.
1261 _GLIBCXX23_CONSTEXPR
1264 { return this->_M_do_to_ulong(); }
1266#if __cplusplus >= 201103L
1267 _GLIBCXX23_CONSTEXPR
1270 { return this->_M_do_to_ullong(); }
1275 * @brief Returns a character interpretation of the %bitset.
1276 * @return The string equivalent of the bits.
1278 * Note the ordering of the bits: decreasing character positions
1279 * correspond to increasing bit positions (see the main class notes for
1282 template<class _CharT, class _Traits, class _Alloc>
1283 _GLIBCXX23_CONSTEXPR
1284 std::basic_string<_CharT, _Traits, _Alloc>
1287 std::basic_string<_CharT, _Traits, _Alloc> __result;
1288 _M_copy_to_string(__result, _CharT('0'), _CharT('1'));
1292 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1293 // 396. what are characters zero and one.
1294 template<class _CharT, class _Traits, class _Alloc>
1295 _GLIBCXX23_CONSTEXPR
1296 std::basic_string<_CharT, _Traits, _Alloc>
1297 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1299 std::basic_string<_CharT, _Traits, _Alloc> __result;
1300 _M_copy_to_string(__result, __zero, __one);
1304 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1305 // 434. bitset::to_string() hard to use.
1306 template<class _CharT, class _Traits>
1307 _GLIBCXX23_CONSTEXPR
1308 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1310 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
1312 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1313 // 853. to_string needs updating with zero and one.
1314 template<class _CharT, class _Traits>
1315 _GLIBCXX23_CONSTEXPR
1316 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1317 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1318 { return to_string<_CharT, _Traits,
1319 std::allocator<_CharT> >(__zero, __one); }
1321 template<class _CharT>
1322 _GLIBCXX23_CONSTEXPR
1323 std::basic_string<_CharT, std::char_traits<_CharT>,
1324 std::allocator<_CharT> >
1327 return to_string<_CharT, std::char_traits<_CharT>,
1328 std::allocator<_CharT> >();
1331 template<class _CharT>
1332 _GLIBCXX23_CONSTEXPR
1333 std::basic_string<_CharT, std::char_traits<_CharT>,
1334 std::allocator<_CharT> >
1335 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1337 return to_string<_CharT, std::char_traits<_CharT>,
1338 std::allocator<_CharT> >(__zero, __one);
1341 _GLIBCXX23_CONSTEXPR
1342 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1345 return to_string<char, std::char_traits<char>,
1346 std::allocator<char> >();
1349 _GLIBCXX23_CONSTEXPR
1350 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1351 to_string(char __zero, char __one = '1') const
1353 return to_string<char, std::char_traits<char>,
1354 std::allocator<char> >(__zero, __one);
1358 /// Returns the number of bits which are set.
1359 _GLIBCXX23_CONSTEXPR
1361 count() const _GLIBCXX_NOEXCEPT
1362 { return this->_M_do_count(); }
1364 /// Returns the total number of bits.
1365 _GLIBCXX_CONSTEXPR size_t
1366 size() const _GLIBCXX_NOEXCEPT
1370 /// These comparisons for equality/inequality are, well, @e bitwise.
1371 _GLIBCXX23_CONSTEXPR
1373 operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
1374 { return this->_M_is_equal(__rhs); }
1376#if __cpp_impl_three_way_comparison < 201907L
1377 _GLIBCXX23_CONSTEXPR
1379 operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
1380 { return !this->_M_is_equal(__rhs); }
1385 * @brief Tests the value of a bit.
1386 * @param __position The index of a bit.
1387 * @return The value at @a pos.
1388 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1390 _GLIBCXX23_CONSTEXPR
1392 test(size_t __position) const
1394 this->_M_check(__position, __N("bitset::test"));
1395 return _Unchecked_test(__position);
1398 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1399 // DR 693. std::bitset::all() missing.
1401 * @brief Tests whether all the bits are on.
1402 * @return True if all the bits are set.
1404 _GLIBCXX23_CONSTEXPR
1406 all() const _GLIBCXX_NOEXCEPT
1407 { return this->template _M_are_all<_Nb>(); }
1410 * @brief Tests whether any of the bits are on.
1411 * @return True if at least one bit is set.
1413 _GLIBCXX23_CONSTEXPR
1415 any() const _GLIBCXX_NOEXCEPT
1416 { return this->_M_is_any(); }
1419 * @brief Tests whether any of the bits are on.
1420 * @return True if none of the bits are set.
1422 _GLIBCXX23_CONSTEXPR
1424 none() const _GLIBCXX_NOEXCEPT
1425 { return !this->_M_is_any(); }
1428 /// Self-explanatory.
1429 _GLIBCXX23_CONSTEXPR
1431 operator<<(size_t __position) const _GLIBCXX_NOEXCEPT
1432 { return bitset<_Nb>(*this) <<= __position; }
1434 _GLIBCXX23_CONSTEXPR
1436 operator>>(size_t __position) const _GLIBCXX_NOEXCEPT
1437 { return bitset<_Nb>(*this) >>= __position; }
1441 * @brief Finds the index of the first "on" bit.
1442 * @return The index of the first bit set, or size() if not found.
1443 * @ingroup SGIextensions
1446 _GLIBCXX23_CONSTEXPR
1448 _Find_first() const _GLIBCXX_NOEXCEPT
1449 { return this->_M_do_find_first(_Nb); }
1452 * @brief Finds the index of the next "on" bit after prev.
1453 * @return The index of the next bit set, or size() if not found.
1454 * @param __prev Where to start searching.
1455 * @ingroup SGIextensions
1458 _GLIBCXX23_CONSTEXPR
1460 _Find_next(size_t __prev) const _GLIBCXX_NOEXCEPT
1461 { return this->_M_do_find_next(__prev, _Nb); }
1464 // Helper functions for string operations.
1465 template<class _CharT, class _Traits>
1466 _GLIBCXX23_CONSTEXPR
1468 _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
1472 template<class _CharT, class _Traits, class _Alloc>
1473 _GLIBCXX23_CONSTEXPR
1475 _M_copy_from_string(const std::basic_string<_CharT,
1476 _Traits, _Alloc>& __s, size_t __pos, size_t __n,
1477 _CharT __zero, _CharT __one)
1478 { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n,
1481 template<class _CharT, class _Traits, class _Alloc>
1482 _GLIBCXX23_CONSTEXPR
1484 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&,
1485 _CharT, _CharT) const;
1487 template<class _CharT, class _Traits, size_t _Nb2>
1488 friend std::basic_istream<_CharT, _Traits>&
1489 operator>>(std::basic_istream<_CharT, _Traits>&, bitset<_Nb2>&);
1491 template <class _CharT, class _Traits, size_t _Nb2>
1492 friend std::basic_ostream<_CharT, _Traits>&
1493 operator<<(std::basic_ostream<_CharT, _Traits>&, const bitset<_Nb2>&);
1497 // Definitions of non-inline member functions.
1498 template<size_t _Nb>
1499 template<class _CharT, class _Traits>
1500 _GLIBCXX23_CONSTEXPR
1503 _M_copy_from_ptr(const _CharT* __s, size_t __len,
1504 size_t __pos, size_t __n, _CharT __zero, _CharT __one)
1507 const size_t __nbits = std::min(_Nb, std::min(__n, size_t(__len - __pos)));
1508 for (size_t __i = __nbits; __i > 0; --__i)
1510 const _CharT __c = __s[__pos + __nbits - __i];
1511 if (_Traits::eq(__c, __zero))
1513 else if (_Traits::eq(__c, __one))
1514 _Unchecked_set(__i - 1);
1516 __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
1521 template<size_t _Nb>
1522 template<class _CharT, class _Traits, class _Alloc>
1523 _GLIBCXX23_CONSTEXPR
1526 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s,
1527 _CharT __zero, _CharT __one) const
1529 __s.assign(_Nb, __zero);
1530 size_t __n = this->_Find_first();
1533 __s[_Nb - __n - 1] = __one;
1534 __n = _Find_next(__n);
1539 // 23.3.5.3 bitset operations:
1542 * @brief Global bitwise operations on bitsets.
1543 * @param __x A bitset.
1544 * @param __y A bitset of the same size as @a __x.
1545 * @return A new bitset.
1547 * These should be self-explanatory.
1549 template<size_t _Nb>
1550 _GLIBCXX23_CONSTEXPR
1552 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1554 bitset<_Nb> __result(__x);
1559 template<size_t _Nb>
1560 _GLIBCXX23_CONSTEXPR
1562 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1564 bitset<_Nb> __result(__x);
1569 template <size_t _Nb>
1570 _GLIBCXX23_CONSTEXPR
1572 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1574 bitset<_Nb> __result(__x);
1583 * @brief Global I/O operators for bitsets.
1585 * Direct I/O between streams and bitsets is supported. Output is
1586 * straightforward. Input will skip whitespace, only accept @a 0 and @a 1
1587 * characters, and will only extract as many digits as the %bitset will
1590 template<class _CharT, class _Traits, size_t _Nb>
1591 std::basic_istream<_CharT, _Traits>&
1592 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
1594 typedef typename _Traits::char_type char_type;
1595 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1596 typedef typename __istream_type::ios_base __ios_base;
1600 static _GLIBCXX_CONSTEXPR bool _S_use_alloca() { return _Nb <= 256; }
1602 explicit _Buffer(_CharT* __p) : _M_ptr(__p) { }
1606 if _GLIBCXX17_CONSTEXPR (!_S_use_alloca())
1610 _CharT* const _M_ptr;
1613 if _GLIBCXX17_CONSTEXPR (_Buffer::_S_use_alloca())
1614 __ptr = (_CharT*)__builtin_alloca(_Nb);
1616 __ptr = new _CharT[_Nb];
1617 const _Buffer __buf(__ptr);
1619 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1620 // 303. Bitset input operator underspecified
1621 const char_type __zero = __is.widen('0');
1622 const char_type __one = __is.widen('1');
1624 typename __ios_base::iostate __state = __ios_base::goodbit;
1625 typename __istream_type::sentry __sentry(__is);
1630 for (size_t __i = _Nb; __i > 0; --__i)
1632 static typename _Traits::int_type __eof = _Traits::eof();
1634 typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
1635 if (_Traits::eq_int_type(__c1, __eof))
1637 __state |= __ios_base::eofbit;
1642 const char_type __c2 = _Traits::to_char_type(__c1);
1643 if (_Traits::eq(__c2, __zero))
1645 else if (_Traits::eq(__c2, __one))
1648 eq_int_type(__is.rdbuf()->sputbackc(__c2),
1651 __state |= __ios_base::failbit;
1657 __catch(__cxxabiv1::__forced_unwind&)
1659 __is._M_setstate(__ios_base::badbit);
1660 __throw_exception_again;
1663 { __is._M_setstate(__ios_base::badbit); }
1666 if _GLIBCXX17_CONSTEXPR (_Nb)
1668 if (size_t __len = __ptr - __buf._M_ptr)
1669 __x.template _M_copy_from_ptr<_CharT, _Traits>(__buf._M_ptr, __len,
1673 __state |= __ios_base::failbit;
1676 __is.setstate(__state);
1680 template <class _CharT, class _Traits, size_t _Nb>
1681 std::basic_ostream<_CharT, _Traits>&
1682 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1683 const bitset<_Nb>& __x)
1685 std::basic_string<_CharT, _Traits> __tmp;
1687 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1688 // 396. what are characters zero and one.
1689 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc());
1690 __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
1691 return __os << __tmp;
1696_GLIBCXX_END_NAMESPACE_CONTAINER
1699#undef _GLIBCXX_BITSET_WORDS
1700#undef _GLIBCXX_BITSET_BITS_PER_WORD
1701#undef _GLIBCXX_BITSET_BITS_PER_ULL
1703#if __cplusplus >= 201103L
1705namespace std _GLIBCXX_VISIBILITY(default)
1707_GLIBCXX_BEGIN_NAMESPACE_VERSION
1710 /// std::hash specialization for bitset.
1711 template<size_t _Nb>
1712 struct hash<_GLIBCXX_STD_C::bitset<_Nb>>
1713 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<_Nb>>
1716 operator()(const _GLIBCXX_STD_C::bitset<_Nb>& __b) const noexcept
1718 const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__;
1719 return std::_Hash_impl::hash(__b._M_getdata(), __clength);
1724 struct hash<_GLIBCXX_STD_C::bitset<0>>
1725 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<0>>
1728 operator()(const _GLIBCXX_STD_C::bitset<0>&) const noexcept
1732_GLIBCXX_END_NAMESPACE_VERSION
1737#if defined _GLIBCXX_DEBUG && _GLIBCXX_HOSTED
1738# include <debug/bitset>
1741#endif /* _GLIBCXX_BITSET */