1 // <bitset> -*- C++ -*-
3 // Copyright (C) 2001-2025 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 #ifdef _GLIBCXX_SYSHDR
46 #pragma GCC system_header
49 #include <bits/functexcept.h> // For invalid_argument, out_of_range,
51 #include <bits/stl_algobase.h> // For std::fill
56 # include <bits/cxxabi_forced.h>
59 #if __cplusplus >= 201103L
60 # include <bits/functional_hash.h>
63 #define __glibcxx_want_constexpr_bitset
64 #include <bits/version.h>
66 #define _GLIBCXX_BITSET_BITS_PER_WORD (__CHAR_BIT__ * __SIZEOF_LONG__)
67 #define _GLIBCXX_BITSET_WORDS(__n) \
68 ((__n) / _GLIBCXX_BITSET_BITS_PER_WORD + \
69 ((__n) % _GLIBCXX_BITSET_BITS_PER_WORD == 0 ? 0 : 1))
71 #define _GLIBCXX_BITSET_BITS_PER_ULL (__CHAR_BIT__ * __SIZEOF_LONG_LONG__)
73 namespace std _GLIBCXX_VISIBILITY(default)
75 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
78 * Base class, general case. It is a class invariant that _Nw will be
81 * See documentation for bitset.
86 typedef unsigned long _WordT;
88 /// 0 is the least significant word.
91 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
94 #if __cplusplus >= 201103L
95 constexpr _Base_bitset(unsigned long long __val) noexcept
97 #if __SIZEOF_LONG_LONG__ > __SIZEOF_LONG__
98 , _WordT(__val >> _GLIBCXX_BITSET_BITS_PER_WORD)
102 _Base_bitset(unsigned long __val)
107 static _GLIBCXX_CONSTEXPR size_t
108 _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
109 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
111 static _GLIBCXX_CONSTEXPR size_t
112 _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
113 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
115 static _GLIBCXX_CONSTEXPR size_t
116 _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
117 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
119 static _GLIBCXX_CONSTEXPR _WordT
120 _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
121 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
123 _GLIBCXX14_CONSTEXPR _WordT&
124 _M_getword(size_t __pos) _GLIBCXX_NOEXCEPT
125 { return _M_w[_S_whichword(__pos)]; }
127 _GLIBCXX_CONSTEXPR _WordT
128 _M_getword(size_t __pos) const _GLIBCXX_NOEXCEPT
129 { return _M_w[_S_whichword(__pos)]; }
131 #if __cplusplus >= 201103L
132 constexpr const _WordT*
133 _M_getdata() const noexcept
137 _GLIBCXX23_CONSTEXPR _WordT&
138 _M_hiword() _GLIBCXX_NOEXCEPT
139 { return _M_w[_Nw - 1]; }
141 _GLIBCXX_CONSTEXPR _WordT
142 _M_hiword() const _GLIBCXX_NOEXCEPT
143 { return _M_w[_Nw - 1]; }
145 _GLIBCXX23_CONSTEXPR void
146 _M_do_and(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
148 for (size_t __i = 0; __i < _Nw; __i++)
149 _M_w[__i] &= __x._M_w[__i];
152 _GLIBCXX14_CONSTEXPR void
153 _M_do_or(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
155 for (size_t __i = 0; __i < _Nw; __i++)
156 _M_w[__i] |= __x._M_w[__i];
159 _GLIBCXX14_CONSTEXPR void
160 _M_do_xor(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
162 for (size_t __i = 0; __i < _Nw; __i++)
163 _M_w[__i] ^= __x._M_w[__i];
166 _GLIBCXX14_CONSTEXPR void
167 _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT;
169 _GLIBCXX14_CONSTEXPR void
170 _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT;
172 _GLIBCXX14_CONSTEXPR void
173 _M_do_flip() _GLIBCXX_NOEXCEPT
175 for (size_t __i = 0; __i < _Nw; __i++)
176 _M_w[__i] = ~_M_w[__i];
179 _GLIBCXX14_CONSTEXPR void
180 _M_do_set() _GLIBCXX_NOEXCEPT
182 #if __cplusplus >= 201402L
183 if (__builtin_is_constant_evaluated())
185 for (_WordT& __w : _M_w)
186 __w = ~static_cast<_WordT>(0);;
190 __builtin_memset(_M_w, 0xFF, _Nw * sizeof(_WordT));
193 _GLIBCXX14_CONSTEXPR void
194 _M_do_reset() _GLIBCXX_NOEXCEPT
196 #if __cplusplus >= 201402L
197 if (__builtin_is_constant_evaluated())
199 for (_WordT& __w : _M_w)
204 __builtin_memset(_M_w, 0, _Nw * sizeof(_WordT));
207 _GLIBCXX14_CONSTEXPR bool
208 _M_is_equal(const _Base_bitset<_Nw>& __x) const _GLIBCXX_NOEXCEPT
210 #if __cplusplus >= 201402L
211 if (__builtin_is_constant_evaluated())
213 for (size_t __i = 0; __i < _Nw; ++__i)
214 if (_M_w[__i] != __x._M_w[__i])
219 return !__builtin_memcmp(_M_w, __x._M_w, _Nw * sizeof(_WordT));
223 _GLIBCXX14_CONSTEXPR bool
224 _M_are_all() const _GLIBCXX_NOEXCEPT
226 for (size_t __i = 0; __i < _Nw - 1; __i++)
227 if (_M_w[__i] != ~static_cast<_WordT>(0))
229 return _M_hiword() == (~static_cast<_WordT>(0)
230 >> (_Nw * _GLIBCXX_BITSET_BITS_PER_WORD
234 _GLIBCXX14_CONSTEXPR bool
235 _M_is_any() const _GLIBCXX_NOEXCEPT
237 for (size_t __i = 0; __i < _Nw; __i++)
238 if (_M_w[__i] != static_cast<_WordT>(0))
243 _GLIBCXX14_CONSTEXPR size_t
244 _M_do_count() const _GLIBCXX_NOEXCEPT
247 for (size_t __i = 0; __i < _Nw; __i++)
248 __result += __builtin_popcountl(_M_w[__i]);
252 _GLIBCXX14_CONSTEXPR unsigned long
253 _M_do_to_ulong() const;
255 #if __cplusplus >= 201103L
256 _GLIBCXX14_CONSTEXPR unsigned long long
257 _M_do_to_ullong() const;
260 // find first "on" bit
261 _GLIBCXX14_CONSTEXPR size_t
262 _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT;
264 // find the next "on" bit that follows "prev"
265 _GLIBCXX14_CONSTEXPR size_t
266 _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT;
269 // Definitions of non-inline functions from _Base_bitset.
271 _GLIBCXX14_CONSTEXPR void
272 _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT
274 if (__builtin_expect(__shift != 0, 1))
276 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
277 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
280 for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
281 _M_w[__n] = _M_w[__n - __wshift];
284 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
286 for (size_t __n = _Nw - 1; __n > __wshift; --__n)
287 _M_w[__n] = ((_M_w[__n - __wshift] << __offset)
288 | (_M_w[__n - __wshift - 1] >> __sub_offset));
289 _M_w[__wshift] = _M_w[0] << __offset;
292 std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
297 _GLIBCXX14_CONSTEXPR void
298 _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT
300 if (__builtin_expect(__shift != 0, 1))
302 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
303 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
304 const size_t __limit = _Nw - __wshift - 1;
307 for (size_t __n = 0; __n <= __limit; ++__n)
308 _M_w[__n] = _M_w[__n + __wshift];
311 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
313 for (size_t __n = 0; __n < __limit; ++__n)
314 _M_w[__n] = ((_M_w[__n + __wshift] >> __offset)
315 | (_M_w[__n + __wshift + 1] << __sub_offset));
316 _M_w[__limit] = _M_w[_Nw-1] >> __offset;
319 std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
324 _GLIBCXX14_CONSTEXPR unsigned long
325 _Base_bitset<_Nw>::_M_do_to_ulong() const
327 for (size_t __i = 1; __i < _Nw; ++__i)
329 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
333 #if __cplusplus >= 201103L
335 _GLIBCXX14_CONSTEXPR unsigned long long
336 _Base_bitset<_Nw>::_M_do_to_ullong() const
338 #if __SIZEOF_LONG_LONG__ == __SIZEOF_LONG__
339 return _M_do_to_ulong();
341 for (size_t __i = 2; __i < _Nw; ++__i)
343 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ullong"));
345 return _M_w[0] + (static_cast<unsigned long long>(_M_w[1])
346 << _GLIBCXX_BITSET_BITS_PER_WORD);
352 _GLIBCXX14_CONSTEXPR size_t
354 _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT
356 for (size_t __i = 0; __i < _Nw; __i++)
358 _WordT __thisword = _M_w[__i];
359 if (__thisword != static_cast<_WordT>(0))
360 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
361 + __builtin_ctzl(__thisword));
363 // not found, so return an indication of failure.
368 _GLIBCXX14_CONSTEXPR size_t
370 _M_do_find_next(size_t __prev, size_t __not_found) const _GLIBCXX_NOEXCEPT
372 // make bound inclusive
375 // check out of bounds
376 if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
380 size_t __i = _S_whichword(__prev);
381 _WordT __thisword = _M_w[__i];
383 // mask off bits below bound
384 __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
386 if (__thisword != static_cast<_WordT>(0))
387 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
388 + __builtin_ctzl(__thisword));
390 // check subsequent words
392 for (; __i < _Nw; __i++)
394 __thisword = _M_w[__i];
395 if (__thisword != static_cast<_WordT>(0))
396 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
397 + __builtin_ctzl(__thisword));
399 // not found, so return an indication of failure.
401 } // end _M_do_find_next
404 * Base class, specialization for a single word.
406 * See documentation for bitset.
409 struct _Base_bitset<1>
411 typedef unsigned long _WordT;
414 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
418 #if __cplusplus >= 201103L
419 constexpr _Base_bitset(unsigned long long __val) noexcept
421 _Base_bitset(unsigned long __val)
426 static _GLIBCXX_CONSTEXPR size_t
427 _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
428 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
430 static _GLIBCXX_CONSTEXPR size_t
431 _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
432 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
434 static _GLIBCXX_CONSTEXPR size_t
435 _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
436 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
438 static _GLIBCXX_CONSTEXPR _WordT
439 _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
440 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
442 _GLIBCXX14_CONSTEXPR _WordT&
443 _M_getword(size_t) _GLIBCXX_NOEXCEPT
446 _GLIBCXX_CONSTEXPR _WordT
447 _M_getword(size_t) const _GLIBCXX_NOEXCEPT
450 #if __cplusplus >= 201103L
451 constexpr const _WordT*
452 _M_getdata() const noexcept
456 _GLIBCXX14_CONSTEXPR _WordT&
457 _M_hiword() _GLIBCXX_NOEXCEPT
460 _GLIBCXX_CONSTEXPR _WordT
461 _M_hiword() const _GLIBCXX_NOEXCEPT
464 _GLIBCXX14_CONSTEXPR void
465 _M_do_and(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
466 { _M_w &= __x._M_w; }
468 _GLIBCXX14_CONSTEXPR void
469 _M_do_or(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
470 { _M_w |= __x._M_w; }
472 _GLIBCXX14_CONSTEXPR void
473 _M_do_xor(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
474 { _M_w ^= __x._M_w; }
476 _GLIBCXX14_CONSTEXPR void
477 _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT
478 { _M_w <<= __shift; }
480 _GLIBCXX14_CONSTEXPR void
481 _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT
482 { _M_w >>= __shift; }
484 _GLIBCXX14_CONSTEXPR void
485 _M_do_flip() _GLIBCXX_NOEXCEPT
488 _GLIBCXX14_CONSTEXPR void
489 _M_do_set() _GLIBCXX_NOEXCEPT
490 { _M_w = ~static_cast<_WordT>(0); }
492 _GLIBCXX14_CONSTEXPR void
493 _M_do_reset() _GLIBCXX_NOEXCEPT
496 _GLIBCXX14_CONSTEXPR bool
497 _M_is_equal(const _Base_bitset<1>& __x) const _GLIBCXX_NOEXCEPT
498 { return _M_w == __x._M_w; }
501 _GLIBCXX14_CONSTEXPR bool
502 _M_are_all() const _GLIBCXX_NOEXCEPT
503 { return _M_w == (~static_cast<_WordT>(0)
504 >> (_GLIBCXX_BITSET_BITS_PER_WORD - _Nb)); }
506 _GLIBCXX14_CONSTEXPR bool
507 _M_is_any() const _GLIBCXX_NOEXCEPT
508 { return _M_w != 0; }
510 _GLIBCXX14_CONSTEXPR size_t
511 _M_do_count() const _GLIBCXX_NOEXCEPT
512 { return __builtin_popcountl(_M_w); }
514 _GLIBCXX14_CONSTEXPR unsigned long
515 _M_do_to_ulong() const _GLIBCXX_NOEXCEPT
518 #if __cplusplus >= 201103L
519 constexpr unsigned long long
520 _M_do_to_ullong() const noexcept
524 _GLIBCXX14_CONSTEXPR size_t
525 _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT
528 return __builtin_ctzl(_M_w);
533 // find the next "on" bit that follows "prev"
534 _GLIBCXX14_CONSTEXPR size_t
535 _M_do_find_next(size_t __prev, size_t __not_found) const
539 if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
542 _WordT __x = _M_w >> __prev;
544 return __builtin_ctzl(__x) + __prev;
551 * Base class, specialization for no storage (zero-length %bitset).
553 * See documentation for bitset.
556 struct _Base_bitset<0>
558 typedef unsigned long _WordT;
560 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
563 #if __cplusplus >= 201103L
564 constexpr _Base_bitset(unsigned long long) noexcept
566 _Base_bitset(unsigned long)
570 static _GLIBCXX_CONSTEXPR size_t
571 _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
572 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
574 static _GLIBCXX_CONSTEXPR size_t
575 _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
576 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
578 static _GLIBCXX_CONSTEXPR size_t
579 _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
580 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
582 static _GLIBCXX_CONSTEXPR _WordT
583 _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
584 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
586 // This would normally give access to the data. The bounds-checking
587 // in the bitset class will prevent the user from getting this far,
588 // but this must fail if the user calls _Unchecked_set directly.
589 // Let's not penalize zero-length users unless they actually
590 // make an unchecked call; all the memory ugliness is therefore
591 // localized to this single should-never-get-this-far function.
592 __attribute__((__noreturn__))
594 _M_getword(size_t) _GLIBCXX_NOEXCEPT
595 { __throw_out_of_range(__N("_Base_bitset::_M_getword")); }
597 _GLIBCXX_CONSTEXPR _WordT
598 _M_getword(size_t) const _GLIBCXX_NOEXCEPT
601 _GLIBCXX_CONSTEXPR _WordT
602 _M_hiword() const _GLIBCXX_NOEXCEPT
605 _GLIBCXX14_CONSTEXPR void
606 _M_do_and(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
609 _GLIBCXX14_CONSTEXPR void
610 _M_do_or(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
613 _GLIBCXX14_CONSTEXPR void
614 _M_do_xor(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
617 _GLIBCXX14_CONSTEXPR void
618 _M_do_left_shift(size_t) _GLIBCXX_NOEXCEPT
621 _GLIBCXX14_CONSTEXPR void
622 _M_do_right_shift(size_t) _GLIBCXX_NOEXCEPT
625 _GLIBCXX14_CONSTEXPR void
626 _M_do_flip() _GLIBCXX_NOEXCEPT
629 _GLIBCXX14_CONSTEXPR void
630 _M_do_set() _GLIBCXX_NOEXCEPT
633 _GLIBCXX14_CONSTEXPR void
634 _M_do_reset() _GLIBCXX_NOEXCEPT
637 // Are all empty bitsets equal to each other? Are they equal to
638 // themselves? How to compare a thing which has no state? What is
639 // the sound of one zero-length bitset clapping?
640 _GLIBCXX_CONSTEXPR bool
641 _M_is_equal(const _Base_bitset<0>&) const _GLIBCXX_NOEXCEPT
645 _GLIBCXX_CONSTEXPR bool
646 _M_are_all() const _GLIBCXX_NOEXCEPT
649 _GLIBCXX_CONSTEXPR bool
650 _M_is_any() const _GLIBCXX_NOEXCEPT
653 _GLIBCXX_CONSTEXPR size_t
654 _M_do_count() const _GLIBCXX_NOEXCEPT
657 _GLIBCXX_CONSTEXPR unsigned long
658 _M_do_to_ulong() const _GLIBCXX_NOEXCEPT
661 #if __cplusplus >= 201103L
662 constexpr unsigned long long
663 _M_do_to_ullong() const noexcept
667 // Normally "not found" is the size, but that could also be
668 // misinterpreted as an index in this corner case. Oh well.
669 _GLIBCXX_CONSTEXPR size_t
670 _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT
673 _GLIBCXX_CONSTEXPR size_t
674 _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT
679 // Helper class to zero out the unused high-order bits in the highest word.
680 template<size_t _Extrabits>
683 typedef unsigned long _WordT;
685 static _GLIBCXX14_CONSTEXPR void
686 _S_do_sanitize(_WordT& __val) _GLIBCXX_NOEXCEPT
687 { __val &= ~((~static_cast<_WordT>(0)) << _Extrabits); }
693 typedef unsigned long _WordT;
695 static _GLIBCXX14_CONSTEXPR void
696 _S_do_sanitize(_WordT) _GLIBCXX_NOEXCEPT { }
699 #if __cplusplus >= 201103L
700 template<size_t _Nb, bool = (_Nb < _GLIBCXX_BITSET_BITS_PER_ULL)>
703 static constexpr unsigned long long
704 _S_do_sanitize_val(unsigned long long __val)
709 struct _Sanitize_val<_Nb, true>
711 static constexpr unsigned long long
712 _S_do_sanitize_val(unsigned long long __val)
713 { return __val & ~((~static_cast<unsigned long long>(0)) << _Nb); }
719 template<typename _CharT>
720 using __string = std::basic_string<_CharT>;
722 template<typename _CharT>
725 using size_type = size_t;
726 static constexpr size_type npos = size_type(-1);
730 static _GLIBCXX14_CONSTEXPR size_t
731 length(const _CharT* __s) noexcept
739 static constexpr bool
740 eq(_CharT __l, _CharT __r) noexcept
741 { return __l == __r; }
745 } // namespace __bitset
749 * @brief The %bitset class represents a @e fixed-size sequence of bits.
752 * (Note that %bitset does @e not meet the formal requirements of a
753 * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
755 * The template argument, @a Nb, may be any non-negative number,
756 * specifying the number of bits (e.g., "0", "12", "1024*1024").
758 * In the general unoptimized case, storage is allocated in word-sized
759 * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
760 * words will be used for storage. B - Nb%B bits are unused. (They are
761 * the high-order bits in the highest word.) It is a class invariant
762 * that those unused bits are always zero.
764 * If you think of %bitset as <em>a simple array of bits</em>, be
765 * aware that your mental picture is reversed: a %bitset behaves
766 * the same way as bits in integers do, with the bit at index 0 in
767 * the <em>least significant / right-hand</em> position, and the bit at
768 * index Nb-1 in the <em>most significant / left-hand</em> position.
769 * Thus, unlike other containers, a %bitset's index <em>counts from
770 * right to left</em>, to put it very loosely.
772 * This behavior is preserved when translating to and from strings. For
773 * example, the first line of the following program probably prints
774 * <em>b('a') is 0001100001</em> on a modern ASCII system.
778 * #include <iostream>
781 * using namespace std;
788 * cout << "b('a') is " << b << endl;
792 * string str = s.str();
793 * cout << "index 3 in the string is " << str[3] << " but\n"
794 * << "index 3 in the bitset is " << b[3] << endl;
799 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_containers.html
800 * for a description of extensions.
802 * Most of the actual code isn't contained in %bitset<> itself, but in the
803 * base class _Base_bitset. The base class works with whole words, not with
804 * individual bits. This allows us to specialize _Base_bitset for the
805 * important special case where the %bitset is only a single word.
807 * Extra confusion can result due to the fact that the storage for
808 * _Base_bitset @e is a regular array, and is indexed as such. This is
809 * carefully encapsulated.
813 : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
816 typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
817 typedef unsigned long _WordT;
820 template<class _CharT, class _Traits, class _Alloc>
823 _M_check_initial_position(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
824 size_t __position) const
826 if (__position > __s.size())
827 __throw_out_of_range_fmt(__N("bitset::bitset: __position "
828 "(which is %zu) > __s.size() "
830 __position, __s.size());
835 void _M_check(size_t __position, const char *__s) const
837 if (__position >= _Nb)
838 __throw_out_of_range_fmt(__N("%s: __position (which is %zu) "
839 ">= _Nb (which is %zu)"),
840 __s, __position, _Nb);
845 _M_do_sanitize() _GLIBCXX_NOEXCEPT
847 typedef _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD> __sanitize_type;
848 __sanitize_type::_S_do_sanitize(this->_M_hiword());
851 #if __cplusplus >= 201103L
852 friend struct std::hash<bitset>;
857 * This encapsulates the concept of a single bit. An instance of this
858 * class is a proxy for an actual bit; this way the individual bit
859 * operations are done as faster word-size bitwise instructions.
861 * Most users will never need to use this class directly; conversions
862 * to and from bool are automatic and should be transparent. Overloaded
863 * operators help to preserve the illusion.
865 * (On a typical system, this <em>bit %reference</em> is 64
866 * times the size of an actual bit. Ha.)
876 reference(bitset& __b, size_t __pos) _GLIBCXX_NOEXCEPT
878 _M_wp = &__b._M_getword(__pos);
879 _M_bpos = _Base::_S_whichbit(__pos);
883 #if __cplusplus >= 201103L
884 reference(const reference&) = default;
887 #if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
890 ~reference() _GLIBCXX_NOEXCEPT
896 operator=(bool __x) _GLIBCXX_NOEXCEPT
899 *_M_wp |= _Base::_S_maskbit(_M_bpos);
901 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
905 // For b[i] = b[__j];
908 operator=(const reference& __j) _GLIBCXX_NOEXCEPT
910 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
911 *_M_wp |= _Base::_S_maskbit(_M_bpos);
913 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
920 operator~() const _GLIBCXX_NOEXCEPT
921 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
925 operator bool() const _GLIBCXX_NOEXCEPT
926 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
931 flip() _GLIBCXX_NOEXCEPT
933 *_M_wp ^= _Base::_S_maskbit(_M_bpos);
937 friend class reference;
939 // 23.3.5.1 constructors:
940 /// All bits set to zero.
941 _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT
944 /// Initial bits bitwise-copied from a single word (others set to zero).
945 #if __cplusplus >= 201103L
946 constexpr bitset(unsigned long long __val) noexcept
947 : _Base(_Sanitize_val<_Nb>::_S_do_sanitize_val(__val)) { }
949 bitset(unsigned long __val)
951 { _M_do_sanitize(); }
956 * Use a subset of a string.
957 * @param __s A string of @a 0 and @a 1 characters.
958 * @param __position Index of the first character in @a __s to use;
960 * @throw std::out_of_range If @a pos is bigger the size of @a __s.
961 * @throw std::invalid_argument If a character appears in the string
962 * which is neither @a 0 nor @a 1.
964 template<class _CharT, class _Traits, class _Alloc>
967 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
968 size_t __position = 0)
971 _M_check_initial_position(__s, __position);
972 _M_copy_from_string(__s, __position,
973 std::basic_string<_CharT, _Traits, _Alloc>::npos,
974 _CharT('0'), _CharT('1'));
978 * Use a subset of a string.
979 * @param __s A string of @a 0 and @a 1 characters.
980 * @param __position Index of the first character in @a __s to use.
981 * @param __n The number of characters to copy.
982 * @throw std::out_of_range If @a __position is bigger the size
984 * @throw std::invalid_argument If a character appears in the string
985 * which is neither @a 0 nor @a 1.
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)
993 _M_check_initial_position(__s, __position);
994 _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1'));
997 // _GLIBCXX_RESOLVE_LIB_DEFECTS
998 // 396. what are characters zero and one.
999 template<class _CharT, class _Traits, class _Alloc>
1000 _GLIBCXX23_CONSTEXPR
1001 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
1002 size_t __position, size_t __n,
1003 _CharT __zero, _CharT __one = _CharT('1'))
1006 _M_check_initial_position(__s, __position);
1007 _M_copy_from_string(__s, __position, __n, __zero, __one);
1011 #if __cplusplus >= 201103L
1013 * Construct from a character %array.
1014 * @param __str An %array of characters @a zero and @a one.
1015 * @param __n The number of characters to use.
1016 * @param __zero The character corresponding to the value 0.
1017 * @param __one The character corresponding to the value 1.
1018 * @throw std::invalid_argument If a character appears in the string
1019 * which is neither @a __zero nor @a __one.
1021 template<typename _CharT>
1022 [[__gnu__::__nonnull__]]
1023 _GLIBCXX23_CONSTEXPR
1025 bitset(const _CharT* __str,
1026 typename __bitset::__string<_CharT>::size_type __n
1027 = __bitset::__string<_CharT>::npos,
1028 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
1033 __throw_logic_error(__N("bitset::bitset(const _CharT*, ...)"));
1035 using _Traits = typename __bitset::__string<_CharT>::traits_type;
1037 if (__n == __bitset::__string<_CharT>::npos)
1038 __n = _Traits::length(__str);
1039 _M_copy_from_ptr<_CharT, _Traits>(__str, __n, 0, __n, __zero, __one);
1043 // 23.3.5.2 bitset operations:
1046 * Operations on bitsets.
1047 * @param __rhs A same-sized bitset.
1049 * These should be self-explanatory.
1051 _GLIBCXX23_CONSTEXPR
1053 operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
1055 this->_M_do_and(__rhs);
1059 _GLIBCXX23_CONSTEXPR
1061 operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
1063 this->_M_do_or(__rhs);
1067 _GLIBCXX23_CONSTEXPR
1069 operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
1071 this->_M_do_xor(__rhs);
1078 * Operations on bitsets.
1079 * @param __position The number of places to shift.
1081 * These should be self-explanatory.
1083 _GLIBCXX23_CONSTEXPR
1085 operator<<=(size_t __position) _GLIBCXX_NOEXCEPT
1087 if (__builtin_expect(__position < _Nb, 1))
1089 this->_M_do_left_shift(__position);
1090 this->_M_do_sanitize();
1093 this->_M_do_reset();
1097 _GLIBCXX23_CONSTEXPR
1099 operator>>=(size_t __position) _GLIBCXX_NOEXCEPT
1101 if (__builtin_expect(__position < _Nb, 1))
1102 this->_M_do_right_shift(__position);
1104 this->_M_do_reset();
1111 * These versions of single-bit set, reset, flip, and test are
1112 * extensions from the SGI version. They do no range checking.
1113 * @ingroup SGIextensions
1115 _GLIBCXX23_CONSTEXPR
1117 _Unchecked_set(size_t __pos) _GLIBCXX_NOEXCEPT
1119 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
1123 _GLIBCXX23_CONSTEXPR
1125 _Unchecked_set(size_t __pos, int __val) _GLIBCXX_NOEXCEPT
1128 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
1130 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1134 _GLIBCXX23_CONSTEXPR
1136 _Unchecked_reset(size_t __pos) _GLIBCXX_NOEXCEPT
1138 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1142 _GLIBCXX23_CONSTEXPR
1144 _Unchecked_flip(size_t __pos) _GLIBCXX_NOEXCEPT
1146 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
1150 _GLIBCXX_CONSTEXPR bool
1151 _Unchecked_test(size_t __pos) const _GLIBCXX_NOEXCEPT
1152 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
1153 != static_cast<_WordT>(0)); }
1156 // Set, reset, and flip.
1158 * @brief Sets every bit to true.
1160 _GLIBCXX23_CONSTEXPR
1162 set() _GLIBCXX_NOEXCEPT
1165 this->_M_do_sanitize();
1170 * @brief Sets a given bit to a particular value.
1171 * @param __position The index of the bit.
1172 * @param __val Either true or false, defaults to true.
1173 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1175 _GLIBCXX23_CONSTEXPR
1177 set(size_t __position, bool __val = true)
1179 this->_M_check(__position, __N("bitset::set"));
1180 return _Unchecked_set(__position, __val);
1184 * @brief Sets every bit to false.
1186 _GLIBCXX23_CONSTEXPR
1188 reset() _GLIBCXX_NOEXCEPT
1190 this->_M_do_reset();
1195 * @brief Sets a given bit to false.
1196 * @param __position The index of the bit.
1197 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1199 * Same as writing @c set(pos,false).
1201 _GLIBCXX23_CONSTEXPR
1203 reset(size_t __position)
1205 this->_M_check(__position, __N("bitset::reset"));
1206 return _Unchecked_reset(__position);
1210 * @brief Toggles every bit to its opposite value.
1212 _GLIBCXX23_CONSTEXPR
1214 flip() _GLIBCXX_NOEXCEPT
1217 this->_M_do_sanitize();
1222 * @brief Toggles a given bit to its opposite value.
1223 * @param __position The index of the bit.
1224 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1226 _GLIBCXX23_CONSTEXPR
1228 flip(size_t __position)
1230 this->_M_check(__position, __N("bitset::flip"));
1231 return _Unchecked_flip(__position);
1234 /// See the no-argument flip().
1235 _GLIBCXX23_CONSTEXPR
1237 operator~() const _GLIBCXX_NOEXCEPT
1238 { return bitset<_Nb>(*this).flip(); }
1242 * @brief Array-indexing support.
1243 * @param __position Index into the %bitset.
1244 * @return A bool for a <em>const %bitset</em>. For non-const
1245 * bitsets, an instance of the reference proxy class.
1246 * @note These operators do no range checking and throw no exceptions,
1247 * as required by DR 11 to the standard.
1249 * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
1250 * resolves DR 11 (items 1 and 2), but does not do the range-checking
1251 * required by that DR's resolution. -pme
1252 * The DR has since been changed: range-checking is a precondition
1253 * (users' responsibility), and these functions must not throw. -pme
1255 _GLIBCXX23_CONSTEXPR
1257 operator[](size_t __position)
1258 { return reference(*this, __position); }
1260 _GLIBCXX_CONSTEXPR bool
1261 operator[](size_t __position) const
1262 { return _Unchecked_test(__position); }
1266 * @brief Returns a numerical interpretation of the %bitset.
1267 * @return The integral equivalent of the bits.
1268 * @throw std::overflow_error If there are too many bits to be
1269 * represented in an @c unsigned @c long.
1271 _GLIBCXX23_CONSTEXPR
1274 { return this->_M_do_to_ulong(); }
1276 #if __cplusplus >= 201103L
1277 _GLIBCXX23_CONSTEXPR
1280 { return this->_M_do_to_ullong(); }
1285 * @brief Returns a character interpretation of the %bitset.
1286 * @return The string equivalent of the bits.
1288 * Note the ordering of the bits: decreasing character positions
1289 * correspond to increasing bit positions (see the main class notes for
1292 template<class _CharT, class _Traits, class _Alloc>
1293 _GLIBCXX23_CONSTEXPR
1294 std::basic_string<_CharT, _Traits, _Alloc>
1297 std::basic_string<_CharT, _Traits, _Alloc> __result;
1298 _M_copy_to_string(__result, _CharT('0'), _CharT('1'));
1302 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1303 // 396. what are characters zero and one.
1304 template<class _CharT, class _Traits, class _Alloc>
1305 _GLIBCXX23_CONSTEXPR
1306 std::basic_string<_CharT, _Traits, _Alloc>
1307 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1309 std::basic_string<_CharT, _Traits, _Alloc> __result;
1310 _M_copy_to_string(__result, __zero, __one);
1314 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1315 // 434. bitset::to_string() hard to use.
1316 template<class _CharT, class _Traits>
1317 _GLIBCXX23_CONSTEXPR
1318 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1320 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
1322 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1323 // 853. to_string needs updating with zero and one.
1324 template<class _CharT, class _Traits>
1325 _GLIBCXX23_CONSTEXPR
1326 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1327 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1328 { return to_string<_CharT, _Traits,
1329 std::allocator<_CharT> >(__zero, __one); }
1331 template<class _CharT>
1332 _GLIBCXX23_CONSTEXPR
1333 std::basic_string<_CharT, std::char_traits<_CharT>,
1334 std::allocator<_CharT> >
1337 return to_string<_CharT, std::char_traits<_CharT>,
1338 std::allocator<_CharT> >();
1341 template<class _CharT>
1342 _GLIBCXX23_CONSTEXPR
1343 std::basic_string<_CharT, std::char_traits<_CharT>,
1344 std::allocator<_CharT> >
1345 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1347 return to_string<_CharT, std::char_traits<_CharT>,
1348 std::allocator<_CharT> >(__zero, __one);
1351 _GLIBCXX23_CONSTEXPR
1352 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1355 return to_string<char, std::char_traits<char>,
1356 std::allocator<char> >();
1359 _GLIBCXX23_CONSTEXPR
1360 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1361 to_string(char __zero, char __one = '1') const
1363 return to_string<char, std::char_traits<char>,
1364 std::allocator<char> >(__zero, __one);
1368 /// Returns the number of bits which are set.
1369 _GLIBCXX23_CONSTEXPR
1371 count() const _GLIBCXX_NOEXCEPT
1372 { return this->_M_do_count(); }
1374 /// Returns the total number of bits.
1375 _GLIBCXX_CONSTEXPR size_t
1376 size() const _GLIBCXX_NOEXCEPT
1380 /// These comparisons for equality/inequality are, well, @e bitwise.
1381 _GLIBCXX23_CONSTEXPR
1383 operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
1384 { return this->_M_is_equal(__rhs); }
1386 #if __cpp_impl_three_way_comparison < 201907L
1387 _GLIBCXX23_CONSTEXPR
1389 operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
1390 { return !this->_M_is_equal(__rhs); }
1395 * @brief Tests the value of a bit.
1396 * @param __position The index of a bit.
1397 * @return The value at @a pos.
1398 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1400 _GLIBCXX23_CONSTEXPR
1402 test(size_t __position) const
1404 this->_M_check(__position, __N("bitset::test"));
1405 return _Unchecked_test(__position);
1408 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1409 // DR 693. std::bitset::all() missing.
1411 * @brief Tests whether all the bits are on.
1412 * @return True if all the bits are set.
1414 _GLIBCXX23_CONSTEXPR
1416 all() const _GLIBCXX_NOEXCEPT
1417 { return this->template _M_are_all<_Nb>(); }
1420 * @brief Tests whether any of the bits are on.
1421 * @return True if at least one bit is set.
1423 _GLIBCXX23_CONSTEXPR
1425 any() const _GLIBCXX_NOEXCEPT
1426 { return this->_M_is_any(); }
1429 * @brief Tests whether any of the bits are on.
1430 * @return True if none of the bits are set.
1432 _GLIBCXX23_CONSTEXPR
1434 none() const _GLIBCXX_NOEXCEPT
1435 { return !this->_M_is_any(); }
1438 /// Self-explanatory.
1439 _GLIBCXX23_CONSTEXPR
1441 operator<<(size_t __position) const _GLIBCXX_NOEXCEPT
1442 { return bitset<_Nb>(*this) <<= __position; }
1444 _GLIBCXX23_CONSTEXPR
1446 operator>>(size_t __position) const _GLIBCXX_NOEXCEPT
1447 { return bitset<_Nb>(*this) >>= __position; }
1451 * @brief Finds the index of the first "on" bit.
1452 * @return The index of the first bit set, or size() if not found.
1453 * @ingroup SGIextensions
1456 _GLIBCXX23_CONSTEXPR
1458 _Find_first() const _GLIBCXX_NOEXCEPT
1459 { return this->_M_do_find_first(_Nb); }
1462 * @brief Finds the index of the next "on" bit after prev.
1463 * @return The index of the next bit set, or size() if not found.
1464 * @param __prev Where to start searching.
1465 * @ingroup SGIextensions
1468 _GLIBCXX23_CONSTEXPR
1470 _Find_next(size_t __prev) const _GLIBCXX_NOEXCEPT
1471 { return this->_M_do_find_next(__prev, _Nb); }
1474 // Helper functions for string operations.
1475 template<class _CharT, class _Traits>
1476 _GLIBCXX23_CONSTEXPR
1478 _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
1482 template<class _CharT, class _Traits, class _Alloc>
1483 _GLIBCXX23_CONSTEXPR
1485 _M_copy_from_string(const std::basic_string<_CharT,
1486 _Traits, _Alloc>& __s, size_t __pos, size_t __n,
1487 _CharT __zero, _CharT __one)
1488 { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n,
1491 template<class _CharT, class _Traits, class _Alloc>
1492 _GLIBCXX23_CONSTEXPR
1494 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&,
1495 _CharT, _CharT) const;
1497 template<class _CharT, class _Traits, size_t _Nb2>
1498 friend std::basic_istream<_CharT, _Traits>&
1499 operator>>(std::basic_istream<_CharT, _Traits>&, bitset<_Nb2>&);
1501 template <class _CharT, class _Traits, size_t _Nb2>
1502 friend std::basic_ostream<_CharT, _Traits>&
1503 operator<<(std::basic_ostream<_CharT, _Traits>&, const bitset<_Nb2>&);
1507 // Definitions of non-inline member functions.
1508 template<size_t _Nb>
1509 template<class _CharT, class _Traits>
1510 _GLIBCXX23_CONSTEXPR
1513 _M_copy_from_ptr(const _CharT* __s, size_t __len,
1514 size_t __pos, size_t __n, _CharT __zero, _CharT __one)
1517 const size_t __nbits = std::min(_Nb, std::min(__n, size_t(__len - __pos)));
1518 for (size_t __i = __nbits; __i > 0; --__i)
1520 const _CharT __c = __s[__pos + __nbits - __i];
1521 if (_Traits::eq(__c, __zero))
1523 else if (_Traits::eq(__c, __one))
1524 _Unchecked_set(__i - 1);
1526 __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
1531 template<size_t _Nb>
1532 template<class _CharT, class _Traits, class _Alloc>
1533 _GLIBCXX23_CONSTEXPR
1536 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s,
1537 _CharT __zero, _CharT __one) const
1539 __s.assign(_Nb, __zero);
1540 size_t __n = this->_Find_first();
1543 __s[_Nb - __n - 1] = __one;
1544 __n = _Find_next(__n);
1549 // 23.3.5.3 bitset operations:
1552 * @brief Global bitwise operations on bitsets.
1553 * @param __x A bitset.
1554 * @param __y A bitset of the same size as @a __x.
1555 * @return A new bitset.
1557 * These should be self-explanatory.
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);
1579 template <size_t _Nb>
1580 _GLIBCXX23_CONSTEXPR
1582 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1584 bitset<_Nb> __result(__x);
1593 * @brief Global I/O operators for bitsets.
1595 * Direct I/O between streams and bitsets is supported. Output is
1596 * straightforward. Input will skip whitespace, only accept @a 0 and @a 1
1597 * characters, and will only extract as many digits as the %bitset will
1600 template<class _CharT, class _Traits, size_t _Nb>
1601 std::basic_istream<_CharT, _Traits>&
1602 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
1604 typedef typename _Traits::char_type char_type;
1605 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1606 typedef typename __istream_type::ios_base __ios_base;
1610 static _GLIBCXX_CONSTEXPR bool _S_use_alloca() { return _Nb <= 256; }
1612 explicit _Buffer(_CharT* __p) : _M_ptr(__p) { }
1616 if _GLIBCXX17_CONSTEXPR (!_S_use_alloca())
1620 _CharT* const _M_ptr;
1623 if _GLIBCXX17_CONSTEXPR (_Buffer::_S_use_alloca())
1624 __ptr = (_CharT*)__builtin_alloca(_Nb);
1626 __ptr = new _CharT[_Nb];
1627 const _Buffer __buf(__ptr);
1629 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1630 // 303. Bitset input operator underspecified
1631 const char_type __zero = __is.widen('0');
1632 const char_type __one = __is.widen('1');
1634 typename __ios_base::iostate __state = __ios_base::goodbit;
1635 typename __istream_type::sentry __sentry(__is);
1640 for (size_t __i = _Nb; __i > 0; --__i)
1642 static typename _Traits::int_type __eof = _Traits::eof();
1644 typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
1645 if (_Traits::eq_int_type(__c1, __eof))
1647 __state |= __ios_base::eofbit;
1652 const char_type __c2 = _Traits::to_char_type(__c1);
1653 if (_Traits::eq(__c2, __zero))
1655 else if (_Traits::eq(__c2, __one))
1658 eq_int_type(__is.rdbuf()->sputbackc(__c2),
1661 __state |= __ios_base::failbit;
1667 __catch(__cxxabiv1::__forced_unwind&)
1669 __is._M_setstate(__ios_base::badbit);
1670 __throw_exception_again;
1673 { __is._M_setstate(__ios_base::badbit); }
1676 if _GLIBCXX17_CONSTEXPR (_Nb)
1678 if (size_t __len = __ptr - __buf._M_ptr)
1679 __x.template _M_copy_from_ptr<_CharT, _Traits>(__buf._M_ptr, __len,
1683 __state |= __ios_base::failbit;
1686 __is.setstate(__state);
1690 template <class _CharT, class _Traits, size_t _Nb>
1691 std::basic_ostream<_CharT, _Traits>&
1692 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1693 const bitset<_Nb>& __x)
1695 std::basic_string<_CharT, _Traits> __tmp;
1697 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1698 // 396. what are characters zero and one.
1699 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc());
1700 __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
1701 return __os << __tmp;
1706 _GLIBCXX_END_NAMESPACE_CONTAINER
1709 #undef _GLIBCXX_BITSET_WORDS
1710 #undef _GLIBCXX_BITSET_BITS_PER_WORD
1711 #undef _GLIBCXX_BITSET_BITS_PER_ULL
1713 #if __cplusplus >= 201103L
1715 namespace std _GLIBCXX_VISIBILITY(default)
1717 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1720 /// std::hash specialization for bitset.
1721 template<size_t _Nb>
1722 struct hash<_GLIBCXX_STD_C::bitset<_Nb>>
1723 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<_Nb>>
1726 operator()(const _GLIBCXX_STD_C::bitset<_Nb>& __b) const noexcept
1728 const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__;
1729 return std::_Hash_impl::hash(__b._M_getdata(), __clength);
1734 struct hash<_GLIBCXX_STD_C::bitset<0>>
1735 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<0>>
1738 operator()(const _GLIBCXX_STD_C::bitset<0>&) const noexcept
1742 _GLIBCXX_END_NAMESPACE_VERSION
1747 #if defined _GLIBCXX_DEBUG && _GLIBCXX_HOSTED
1748 # include <debug/bitset>
1751 #endif /* _GLIBCXX_BITSET */