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
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#define __glibcxx_want_bitset // ...construct from string_view
65#include <bits/version.h>
67#ifdef __cpp_lib_bitset // ...construct from string_view
68# include <string_view>
71#define _GLIBCXX_BITSET_BITS_PER_WORD (__CHAR_BIT__ * __SIZEOF_LONG__)
72#define _GLIBCXX_BITSET_WORDS(__n) \
73 ((__n) / _GLIBCXX_BITSET_BITS_PER_WORD + \
74 ((__n) % _GLIBCXX_BITSET_BITS_PER_WORD == 0 ? 0 : 1))
76#define _GLIBCXX_BITSET_BITS_PER_ULL (__CHAR_BIT__ * __SIZEOF_LONG_LONG__)
78namespace std _GLIBCXX_VISIBILITY(default)
80_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
83 * Base class, general case. It is a class invariant that _Nw will be
86 * See documentation for bitset.
91 typedef unsigned long _WordT;
93 /// 0 is the least significant word.
96 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
99#if __cplusplus >= 201103L
100 constexpr _Base_bitset(unsigned long long __val) noexcept
101 : _M_w{ _WordT(__val)
102#if __SIZEOF_LONG_LONG__ > __SIZEOF_LONG__
103 , _WordT(__val >> _GLIBCXX_BITSET_BITS_PER_WORD)
107 _Base_bitset(unsigned long __val)
112 static _GLIBCXX_CONSTEXPR size_t
113 _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
114 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
116 static _GLIBCXX_CONSTEXPR size_t
117 _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
118 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
120 static _GLIBCXX_CONSTEXPR size_t
121 _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
122 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
124 static _GLIBCXX_CONSTEXPR _WordT
125 _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
126 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
128 _GLIBCXX14_CONSTEXPR _WordT&
129 _M_getword(size_t __pos) _GLIBCXX_NOEXCEPT
130 { return _M_w[_S_whichword(__pos)]; }
132 _GLIBCXX_CONSTEXPR _WordT
133 _M_getword(size_t __pos) const _GLIBCXX_NOEXCEPT
134 { return _M_w[_S_whichword(__pos)]; }
136#if __cplusplus >= 201103L
137 constexpr const _WordT*
138 _M_getdata() const noexcept
142 _GLIBCXX23_CONSTEXPR _WordT&
143 _M_hiword() _GLIBCXX_NOEXCEPT
144 { return _M_w[_Nw - 1]; }
146 _GLIBCXX_CONSTEXPR _WordT
147 _M_hiword() const _GLIBCXX_NOEXCEPT
148 { return _M_w[_Nw - 1]; }
150 _GLIBCXX23_CONSTEXPR void
151 _M_do_and(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_or(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_xor(const _Base_bitset<_Nw>& __x) _GLIBCXX_NOEXCEPT
167 for (size_t __i = 0; __i < _Nw; __i++)
168 _M_w[__i] ^= __x._M_w[__i];
171 _GLIBCXX14_CONSTEXPR void
172 _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT;
174 _GLIBCXX14_CONSTEXPR void
175 _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT;
177 _GLIBCXX14_CONSTEXPR void
178 _M_do_flip() _GLIBCXX_NOEXCEPT
180 for (size_t __i = 0; __i < _Nw; __i++)
181 _M_w[__i] = ~_M_w[__i];
184 _GLIBCXX14_CONSTEXPR void
185 _M_do_set() _GLIBCXX_NOEXCEPT
187#if __cplusplus >= 201402L
188 if (__builtin_is_constant_evaluated())
190 for (_WordT& __w : _M_w)
191 __w = ~static_cast<_WordT>(0);;
195 __builtin_memset(_M_w, 0xFF, _Nw * sizeof(_WordT));
198 _GLIBCXX14_CONSTEXPR void
199 _M_do_reset() _GLIBCXX_NOEXCEPT
201#if __cplusplus >= 201402L
202 if (__builtin_is_constant_evaluated())
204 for (_WordT& __w : _M_w)
209 __builtin_memset(_M_w, 0, _Nw * sizeof(_WordT));
212 _GLIBCXX14_CONSTEXPR bool
213 _M_is_equal(const _Base_bitset<_Nw>& __x) const _GLIBCXX_NOEXCEPT
215#if __cplusplus >= 201402L
216 if (__builtin_is_constant_evaluated())
218 for (size_t __i = 0; __i < _Nw; ++__i)
219 if (_M_w[__i] != __x._M_w[__i])
224 return !__builtin_memcmp(_M_w, __x._M_w, _Nw * sizeof(_WordT));
228 _GLIBCXX14_CONSTEXPR bool
229 _M_are_all() const _GLIBCXX_NOEXCEPT
231 for (size_t __i = 0; __i < _Nw - 1; __i++)
232 if (_M_w[__i] != ~static_cast<_WordT>(0))
234 return _M_hiword() == (~static_cast<_WordT>(0)
235 >> (_Nw * _GLIBCXX_BITSET_BITS_PER_WORD
239 _GLIBCXX14_CONSTEXPR bool
240 _M_is_any() const _GLIBCXX_NOEXCEPT
242 for (size_t __i = 0; __i < _Nw; __i++)
243 if (_M_w[__i] != static_cast<_WordT>(0))
248 _GLIBCXX14_CONSTEXPR size_t
249 _M_do_count() const _GLIBCXX_NOEXCEPT
252 for (size_t __i = 0; __i < _Nw; __i++)
253 __result += __builtin_popcountl(_M_w[__i]);
257 _GLIBCXX14_CONSTEXPR unsigned long
258 _M_do_to_ulong() const;
260#if __cplusplus >= 201103L
261 _GLIBCXX14_CONSTEXPR unsigned long long
262 _M_do_to_ullong() const;
265 // find first "on" bit
266 _GLIBCXX14_CONSTEXPR size_t
267 _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT;
269 // find the next "on" bit that follows "prev"
270 _GLIBCXX14_CONSTEXPR size_t
271 _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT;
274 // Definitions of non-inline functions from _Base_bitset.
276 _GLIBCXX14_CONSTEXPR void
277 _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT
279 if (__builtin_expect(__shift != 0, 1))
281 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
282 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
285 for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
286 _M_w[__n] = _M_w[__n - __wshift];
289 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
291 for (size_t __n = _Nw - 1; __n > __wshift; --__n)
292 _M_w[__n] = ((_M_w[__n - __wshift] << __offset)
293 | (_M_w[__n - __wshift - 1] >> __sub_offset));
294 _M_w[__wshift] = _M_w[0] << __offset;
297 std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
302 _GLIBCXX14_CONSTEXPR void
303 _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT
305 if (__builtin_expect(__shift != 0, 1))
307 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
308 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
309 const size_t __limit = _Nw - __wshift - 1;
312 for (size_t __n = 0; __n <= __limit; ++__n)
313 _M_w[__n] = _M_w[__n + __wshift];
316 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
318 for (size_t __n = 0; __n < __limit; ++__n)
319 _M_w[__n] = ((_M_w[__n + __wshift] >> __offset)
320 | (_M_w[__n + __wshift + 1] << __sub_offset));
321 _M_w[__limit] = _M_w[_Nw-1] >> __offset;
324 std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
329 _GLIBCXX14_CONSTEXPR unsigned long
330 _Base_bitset<_Nw>::_M_do_to_ulong() const
332 for (size_t __i = 1; __i < _Nw; ++__i)
334 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
338#if __cplusplus >= 201103L
340 _GLIBCXX14_CONSTEXPR unsigned long long
341 _Base_bitset<_Nw>::_M_do_to_ullong() const
343#if __SIZEOF_LONG_LONG__ == __SIZEOF_LONG__
344 return _M_do_to_ulong();
346 for (size_t __i = 2; __i < _Nw; ++__i)
348 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ullong"));
350 return _M_w[0] + (static_cast<unsigned long long>(_M_w[1])
351 << _GLIBCXX_BITSET_BITS_PER_WORD);
357 _GLIBCXX14_CONSTEXPR size_t
359 _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT
361 for (size_t __i = 0; __i < _Nw; __i++)
363 _WordT __thisword = _M_w[__i];
364 if (__thisword != static_cast<_WordT>(0))
365 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
366 + __builtin_ctzl(__thisword));
368 // not found, so return an indication of failure.
373 _GLIBCXX14_CONSTEXPR size_t
375 _M_do_find_next(size_t __prev, size_t __not_found) const _GLIBCXX_NOEXCEPT
377 // make bound inclusive
380 // check out of bounds
381 if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
385 size_t __i = _S_whichword(__prev);
386 _WordT __thisword = _M_w[__i];
388 // mask off bits below bound
389 __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
391 if (__thisword != static_cast<_WordT>(0))
392 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
393 + __builtin_ctzl(__thisword));
395 // check subsequent words
397 for (; __i < _Nw; __i++)
399 __thisword = _M_w[__i];
400 if (__thisword != static_cast<_WordT>(0))
401 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
402 + __builtin_ctzl(__thisword));
404 // not found, so return an indication of failure.
406 } // end _M_do_find_next
409 * Base class, specialization for a single word.
411 * See documentation for bitset.
414 struct _Base_bitset<1>
416 typedef unsigned long _WordT;
419 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
423#if __cplusplus >= 201103L
424 constexpr _Base_bitset(unsigned long long __val) noexcept
426 _Base_bitset(unsigned long __val)
431 static _GLIBCXX_CONSTEXPR size_t
432 _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
433 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
435 static _GLIBCXX_CONSTEXPR size_t
436 _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
437 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
439 static _GLIBCXX_CONSTEXPR size_t
440 _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
441 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
443 static _GLIBCXX_CONSTEXPR _WordT
444 _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
445 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
447 _GLIBCXX14_CONSTEXPR _WordT&
448 _M_getword(size_t) _GLIBCXX_NOEXCEPT
451 _GLIBCXX_CONSTEXPR _WordT
452 _M_getword(size_t) const _GLIBCXX_NOEXCEPT
455#if __cplusplus >= 201103L
456 constexpr const _WordT*
457 _M_getdata() const noexcept
461 _GLIBCXX14_CONSTEXPR _WordT&
462 _M_hiword() _GLIBCXX_NOEXCEPT
465 _GLIBCXX_CONSTEXPR _WordT
466 _M_hiword() const _GLIBCXX_NOEXCEPT
469 _GLIBCXX14_CONSTEXPR void
470 _M_do_and(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
471 { _M_w &= __x._M_w; }
473 _GLIBCXX14_CONSTEXPR void
474 _M_do_or(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
475 { _M_w |= __x._M_w; }
477 _GLIBCXX14_CONSTEXPR void
478 _M_do_xor(const _Base_bitset<1>& __x) _GLIBCXX_NOEXCEPT
479 { _M_w ^= __x._M_w; }
481 _GLIBCXX14_CONSTEXPR void
482 _M_do_left_shift(size_t __shift) _GLIBCXX_NOEXCEPT
483 { _M_w <<= __shift; }
485 _GLIBCXX14_CONSTEXPR void
486 _M_do_right_shift(size_t __shift) _GLIBCXX_NOEXCEPT
487 { _M_w >>= __shift; }
489 _GLIBCXX14_CONSTEXPR void
490 _M_do_flip() _GLIBCXX_NOEXCEPT
493 _GLIBCXX14_CONSTEXPR void
494 _M_do_set() _GLIBCXX_NOEXCEPT
495 { _M_w = ~static_cast<_WordT>(0); }
497 _GLIBCXX14_CONSTEXPR void
498 _M_do_reset() _GLIBCXX_NOEXCEPT
501 _GLIBCXX14_CONSTEXPR bool
502 _M_is_equal(const _Base_bitset<1>& __x) const _GLIBCXX_NOEXCEPT
503 { return _M_w == __x._M_w; }
506 _GLIBCXX14_CONSTEXPR bool
507 _M_are_all() const _GLIBCXX_NOEXCEPT
508 { return _M_w == (~static_cast<_WordT>(0)
509 >> (_GLIBCXX_BITSET_BITS_PER_WORD - _Nb)); }
511 _GLIBCXX14_CONSTEXPR bool
512 _M_is_any() const _GLIBCXX_NOEXCEPT
513 { return _M_w != 0; }
515 _GLIBCXX14_CONSTEXPR size_t
516 _M_do_count() const _GLIBCXX_NOEXCEPT
517 { return __builtin_popcountl(_M_w); }
519 _GLIBCXX14_CONSTEXPR unsigned long
520 _M_do_to_ulong() const _GLIBCXX_NOEXCEPT
523#if __cplusplus >= 201103L
524 constexpr unsigned long long
525 _M_do_to_ullong() const noexcept
529 _GLIBCXX14_CONSTEXPR size_t
530 _M_do_find_first(size_t __not_found) const _GLIBCXX_NOEXCEPT
533 return __builtin_ctzl(_M_w);
538 // find the next "on" bit that follows "prev"
539 _GLIBCXX14_CONSTEXPR size_t
540 _M_do_find_next(size_t __prev, size_t __not_found) const
544 if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
547 _WordT __x = _M_w >> __prev;
549 return __builtin_ctzl(__x) + __prev;
556 * Base class, specialization for no storage (zero-length %bitset).
558 * See documentation for bitset.
561 struct _Base_bitset<0>
563 typedef unsigned long _WordT;
565 _GLIBCXX_CONSTEXPR _Base_bitset() _GLIBCXX_NOEXCEPT
568#if __cplusplus >= 201103L
569 constexpr _Base_bitset(unsigned long long) noexcept
571 _Base_bitset(unsigned long)
575 static _GLIBCXX_CONSTEXPR size_t
576 _S_whichword(size_t __pos) _GLIBCXX_NOEXCEPT
577 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
579 static _GLIBCXX_CONSTEXPR size_t
580 _S_whichbyte(size_t __pos) _GLIBCXX_NOEXCEPT
581 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
583 static _GLIBCXX_CONSTEXPR size_t
584 _S_whichbit(size_t __pos) _GLIBCXX_NOEXCEPT
585 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
587 static _GLIBCXX_CONSTEXPR _WordT
588 _S_maskbit(size_t __pos) _GLIBCXX_NOEXCEPT
589 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
591 // This would normally give access to the data. The bounds-checking
592 // in the bitset class will prevent the user from getting this far,
593 // but this must fail if the user calls _Unchecked_set directly.
594 // Let's not penalize zero-length users unless they actually
595 // make an unchecked call; all the memory ugliness is therefore
596 // localized to this single should-never-get-this-far function.
597 __attribute__((__noreturn__))
599 _M_getword(size_t) _GLIBCXX_NOEXCEPT
600 { __throw_out_of_range(__N("_Base_bitset::_M_getword")); }
602 _GLIBCXX_CONSTEXPR _WordT
603 _M_getword(size_t) const _GLIBCXX_NOEXCEPT
606 _GLIBCXX_CONSTEXPR _WordT
607 _M_hiword() const _GLIBCXX_NOEXCEPT
610 _GLIBCXX14_CONSTEXPR void
611 _M_do_and(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
614 _GLIBCXX14_CONSTEXPR void
615 _M_do_or(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
618 _GLIBCXX14_CONSTEXPR void
619 _M_do_xor(const _Base_bitset<0>&) _GLIBCXX_NOEXCEPT
622 _GLIBCXX14_CONSTEXPR void
623 _M_do_left_shift(size_t) _GLIBCXX_NOEXCEPT
626 _GLIBCXX14_CONSTEXPR void
627 _M_do_right_shift(size_t) _GLIBCXX_NOEXCEPT
630 _GLIBCXX14_CONSTEXPR void
631 _M_do_flip() _GLIBCXX_NOEXCEPT
634 _GLIBCXX14_CONSTEXPR void
635 _M_do_set() _GLIBCXX_NOEXCEPT
638 _GLIBCXX14_CONSTEXPR void
639 _M_do_reset() _GLIBCXX_NOEXCEPT
642 // Are all empty bitsets equal to each other? Are they equal to
643 // themselves? How to compare a thing which has no state? What is
644 // the sound of one zero-length bitset clapping?
645 _GLIBCXX_CONSTEXPR bool
646 _M_is_equal(const _Base_bitset<0>&) const _GLIBCXX_NOEXCEPT
650 _GLIBCXX_CONSTEXPR bool
651 _M_are_all() const _GLIBCXX_NOEXCEPT
654 _GLIBCXX_CONSTEXPR bool
655 _M_is_any() const _GLIBCXX_NOEXCEPT
658 _GLIBCXX_CONSTEXPR size_t
659 _M_do_count() const _GLIBCXX_NOEXCEPT
662 _GLIBCXX_CONSTEXPR unsigned long
663 _M_do_to_ulong() const _GLIBCXX_NOEXCEPT
666#if __cplusplus >= 201103L
667 constexpr unsigned long long
668 _M_do_to_ullong() const noexcept
672 // Normally "not found" is the size, but that could also be
673 // misinterpreted as an index in this corner case. Oh well.
674 _GLIBCXX_CONSTEXPR size_t
675 _M_do_find_first(size_t) const _GLIBCXX_NOEXCEPT
678 _GLIBCXX_CONSTEXPR size_t
679 _M_do_find_next(size_t, size_t) const _GLIBCXX_NOEXCEPT
684 // Helper class to zero out the unused high-order bits in the highest word.
685 template<size_t _Extrabits>
688 typedef unsigned long _WordT;
690 static _GLIBCXX14_CONSTEXPR void
691 _S_do_sanitize(_WordT& __val) _GLIBCXX_NOEXCEPT
692 { __val &= ~((~static_cast<_WordT>(0)) << _Extrabits); }
698 typedef unsigned long _WordT;
700 static _GLIBCXX14_CONSTEXPR void
701 _S_do_sanitize(_WordT) _GLIBCXX_NOEXCEPT { }
704#if __cplusplus >= 201103L
705 template<size_t _Nb, bool = (_Nb < _GLIBCXX_BITSET_BITS_PER_ULL)>
708 static constexpr unsigned long long
709 _S_do_sanitize_val(unsigned long long __val)
714 struct _Sanitize_val<_Nb, true>
716 static constexpr unsigned long long
717 _S_do_sanitize_val(unsigned long long __val)
718 { return __val & ~((~static_cast<unsigned long long>(0)) << _Nb); }
723#ifdef __cpp_lib_bitset // ...construct from string_view
724 template<typename _CharT>
725 using __string = std::basic_string_view<_CharT>;
727 template<typename _CharT>
728 using __string = std::basic_string<_CharT>;
730 template<typename _CharT>
733 using size_type = size_t;
734 static constexpr size_type npos = size_type(-1);
738 static _GLIBCXX14_CONSTEXPR size_t
739 length(const _CharT* __s) noexcept
747 static constexpr bool
748 eq(_CharT __l, _CharT __r) noexcept
749 { return __l == __r; }
753 } // namespace __bitset
757 * @brief The %bitset class represents a @e fixed-size sequence of bits.
760 * (Note that %bitset does @e not meet the formal requirements of a
761 * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
763 * The template argument, `Nb`, may be any non-negative number,
764 * specifying the number of bits (e.g., "0", "12", "1024*1024").
766 * In the general unoptimized case, storage is allocated in word-sized
767 * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
768 * words will be used for storage. B - Nb%B bits are unused. (They are
769 * the high-order bits in the highest word.) It is a class invariant
770 * that those unused bits are always zero.
772 * If you think of %bitset as <em>a simple array of bits</em>, be
773 * aware that your mental picture is reversed: a %bitset behaves
774 * the same way as bits in integers do, with the bit at index 0 in
775 * the <em>least significant / right-hand</em> position, and the bit at
776 * index Nb-1 in the <em>most significant / left-hand</em> position.
777 * Thus, unlike other containers, a %bitset's index <em>counts from
778 * right to left</em>, to put it very loosely.
780 * This behavior is preserved when translating to and from strings. For
781 * example, the first line of the following program probably prints
782 * <em>b('a') is 0001100001</em> on a modern ASCII system.
786 * #include <iostream>
789 * using namespace std;
796 * cout << "b('a') is " << b << endl;
800 * string str = s.str();
801 * cout << "index 3 in the string is " << str[3] << " but\n"
802 * << "index 3 in the bitset is " << b[3] << endl;
807 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_containers.html
808 * for a description of extensions.
810 * Most of the actual code isn't contained in %bitset<> itself, but in the
811 * base class _Base_bitset. The base class works with whole words, not with
812 * individual bits. This allows us to specialize _Base_bitset for the
813 * important special case where the %bitset is only a single word.
815 * Extra confusion can result due to the fact that the storage for
816 * _Base_bitset @e is a regular array, and is indexed as such. This is
817 * carefully encapsulated.
821 : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
824 typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
825 typedef unsigned long _WordT;
828 _GLIBCXX23_CONSTEXPR void
829 _M_check_initial_position(
830 const _Str& __s, typename _Str::size_type __position) const
832 if (__position > __s.size())
833 __throw_out_of_range_fmt(
834 __N("bitset::bitset:"
835 " __position (which is %zu) > __s.size() (which is %zu)"),
836 size_t(__position), size_t(__s.size()));
840 void _M_check(size_t __position, const char *__s) const
842 if (__position >= _Nb)
843 __throw_out_of_range_fmt(
844 __N("%s: __position (which is %zu) >= _Nb (which is %zu)"),
845 __s, size_t(__position), size_t(_Nb));
850 _M_do_sanitize() _GLIBCXX_NOEXCEPT
852 typedef _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD> __sanitize_type;
853 __sanitize_type::_S_do_sanitize(this->_M_hiword());
856#if __cplusplus >= 201103L
857 friend struct std::hash<bitset>;
862 * This encapsulates the concept of a single bit. An instance of this
863 * class is a proxy for an actual bit; this way the individual bit
864 * operations are done as faster word-size bitwise instructions.
866 * Most users will never need to use this class directly; conversions
867 * to and from bool are automatic and should be transparent. Overloaded
868 * operators help to preserve the illusion.
870 * (On a typical system, this <em>bit %reference</em> is 64
871 * times the size of an actual bit. Ha.)
881 reference(bitset& __b, size_t __pos) _GLIBCXX_NOEXCEPT
883 _M_wp = &__b._M_getword(__pos);
884 _M_bpos = _Base::_S_whichbit(__pos);
888#if __cplusplus >= 201103L
889 reference(const reference&) = default;
892#if __cplusplus > 202002L && __cpp_constexpr_dynamic_alloc
895 ~reference() _GLIBCXX_NOEXCEPT
901 operator=(bool __x) _GLIBCXX_NOEXCEPT
904 *_M_wp |= _Base::_S_maskbit(_M_bpos);
906 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
910 // For b[i] = b[__j];
913 operator=(const reference& __j) _GLIBCXX_NOEXCEPT
915 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
916 *_M_wp |= _Base::_S_maskbit(_M_bpos);
918 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
925 operator~() const _GLIBCXX_NOEXCEPT
926 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
930 operator bool() const _GLIBCXX_NOEXCEPT
931 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
936 flip() _GLIBCXX_NOEXCEPT
938 *_M_wp ^= _Base::_S_maskbit(_M_bpos);
942 friend class reference;
944 // 23.3.5.1 constructors:
945 /// All bits set to zero.
946 _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT
949 /// Initial bits bitwise-copied from a single word (others set to zero).
950#if __cplusplus >= 201103L
951 constexpr bitset(unsigned long long __val) noexcept
952 : _Base(_Sanitize_val<_Nb>::_S_do_sanitize_val(__val)) { }
954 bitset(unsigned long __val)
956 { _M_do_sanitize(); }
961 * Use a subset of a string.
962 * @param __s A string of `0` and `1` characters.
963 * @param __position Index of the first character in `__s` to use;
965 * @throw std::out_of_range If `__position > __s.size()`.
966 * @throw std::invalid_argument If a character appears in the string
967 * which is neither `0` nor `1`.
969 template<class _CharT, class _Traits, class _Alloc>
972 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
973 size_t __position = 0)
976 _M_check_initial_position(__s, __position);
977 _M_copy_from_string(__s, __position,
978 std::basic_string<_CharT, _Traits, _Alloc>::npos,
979 _CharT('0'), _CharT('1'));
983 * Use a subset of a string.
984 * @param __s A string of `0` and `1` characters.
985 * @param __position Index of the first character in `__s` to use.
986 * @param __n The number of characters to copy.
987 * @throw std::out_of_range If `__position > __s.size()`.
988 * @throw std::invalid_argument If a character appears in the string
989 * which is neither `0` nor `1`.
991 template<class _CharT, class _Traits, class _Alloc>
993 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
994 size_t __position, size_t __n)
997 _M_check_initial_position(__s, __position);
998 _M_copy_from_string(__s, __position, __n, _CharT('0'), _CharT('1'));
1001 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1002 // 396. what are characters zero and one.
1003 template<class _CharT, class _Traits, class _Alloc>
1004 _GLIBCXX23_CONSTEXPR
1005 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
1006 size_t __position, size_t __n,
1007 _CharT __zero, _CharT __one = _CharT('1'))
1010 _M_check_initial_position(__s, __position);
1011 _M_copy_from_string(__s, __position, __n, __zero, __one);
1015#ifdef __cpp_lib_bitset
1017 * Use a subset of a string view.
1018 * @param __s A `string_view` of a sequence of `0` and `1` characters.
1019 * @param __position Index of the first character in `__s` to use.
1020 * @param __n The maximum number of characters from `__s` to use.
1021 * @param __zero The character corresponding to the value 0.
1022 * @param __one The character corresponding to the value 1.
1023 * @throw std::out_of_range If `__position > __s.size()`.
1024 * @throw std::invalid_argument If a character appears in `__s`
1025 * which is neither `0` nor `1`.
1027 template<class _CharT, class _Traits>
1029 bitset(basic_string_view<_CharT, _Traits> __s,
1030 basic_string_view<_CharT, _Traits>::size_type __position = 0,
1031 basic_string_view<_CharT, _Traits>::size_type __n =
1032 basic_string_view<_CharT, _Traits>::npos,
1033 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
1036 _M_check_initial_position(__s, __position);
1037 _M_copy_from_ptr<_CharT, _Traits>(
1038 __s.data(), __s.size(), __position, __n, __zero, __one);
1042#if __cplusplus >= 201103L
1043 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1044 // 4294. bitset(const CharT*) constructor needs to be constrained
1046 * Construct from a character %array.
1047 * @param __str An %array of characters `__zero` and `__one`.
1048 * @param __n The number of characters to use.
1049 * @param __zero The character corresponding to the value 0.
1050 * @param __one The character corresponding to the value 1.
1051 * @throw std::invalid_argument If a character appears in the string
1052 * which is neither `__zero` nor `__one`.
1054 template<typename _CharT,
1055 typename = _Require<is_trivially_copyable<_CharT>,
1056 is_standard_layout<_CharT>,
1057 is_trivially_default_constructible<_CharT>,
1058 __not_<is_array<_CharT>>>>
1059 [[__gnu__::__nonnull__]]
1060 _GLIBCXX23_CONSTEXPR
1062 bitset(const _CharT* __str,
1063 typename __bitset::__string<_CharT>::size_type __n
1064 = __bitset::__string<_CharT>::npos,
1065 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
1069 __throw_logic_error(__N("bitset::bitset(const _CharT*, ...)"));
1070 using _Traits = typename __bitset::__string<_CharT>::traits_type;
1072 if (__n == __bitset::__string<_CharT>::npos)
1073 __n = _Traits::length(__str);
1074 _M_copy_from_ptr<_CharT, _Traits>(__str, __n, 0, __n, __zero, __one);
1078 // 23.3.5.2 bitset operations:
1081 * Operations on bitsets.
1082 * @param __rhs A same-sized bitset.
1084 * These should be self-explanatory.
1086 _GLIBCXX23_CONSTEXPR
1088 operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
1090 this->_M_do_and(__rhs);
1094 _GLIBCXX23_CONSTEXPR
1096 operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
1098 this->_M_do_or(__rhs);
1102 _GLIBCXX23_CONSTEXPR
1104 operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
1106 this->_M_do_xor(__rhs);
1113 * Operations on bitsets.
1114 * @param __position The number of places to shift.
1116 * These should be self-explanatory.
1118 _GLIBCXX23_CONSTEXPR
1120 operator<<=(size_t __position) _GLIBCXX_NOEXCEPT
1122 if (__builtin_expect(__position < _Nb, 1))
1124 this->_M_do_left_shift(__position);
1125 this->_M_do_sanitize();
1128 this->_M_do_reset();
1132 _GLIBCXX23_CONSTEXPR
1134 operator>>=(size_t __position) _GLIBCXX_NOEXCEPT
1136 if (__builtin_expect(__position < _Nb, 1))
1137 this->_M_do_right_shift(__position);
1139 this->_M_do_reset();
1146 * These versions of single-bit set, reset, flip, and test are
1147 * extensions from the SGI version. They do no range checking.
1148 * @ingroup SGIextensions
1150 _GLIBCXX23_CONSTEXPR
1152 _Unchecked_set(size_t __pos) _GLIBCXX_NOEXCEPT
1154 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
1158 _GLIBCXX23_CONSTEXPR
1160 _Unchecked_set(size_t __pos, int __val) _GLIBCXX_NOEXCEPT
1163 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
1165 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1169 _GLIBCXX23_CONSTEXPR
1171 _Unchecked_reset(size_t __pos) _GLIBCXX_NOEXCEPT
1173 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
1177 _GLIBCXX23_CONSTEXPR
1179 _Unchecked_flip(size_t __pos) _GLIBCXX_NOEXCEPT
1181 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
1185 _GLIBCXX_CONSTEXPR bool
1186 _Unchecked_test(size_t __pos) const _GLIBCXX_NOEXCEPT
1187 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
1188 != static_cast<_WordT>(0)); }
1191 // Set, reset, and flip.
1193 * @brief Sets every bit to true.
1195 _GLIBCXX23_CONSTEXPR
1197 set() _GLIBCXX_NOEXCEPT
1200 this->_M_do_sanitize();
1205 * @brief Sets a given bit to a particular value.
1206 * @param __position The index of the bit.
1207 * @param __val Either true or false, defaults to true.
1208 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1210 _GLIBCXX23_CONSTEXPR
1212 set(size_t __position, bool __val = true)
1214 this->_M_check(__position, __N("bitset::set"));
1215 return _Unchecked_set(__position, __val);
1219 * @brief Sets every bit to false.
1221 _GLIBCXX23_CONSTEXPR
1223 reset() _GLIBCXX_NOEXCEPT
1225 this->_M_do_reset();
1230 * @brief Sets a given bit to false.
1231 * @param __position The index of the bit.
1232 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1234 * Same as writing @c set(pos,false).
1236 _GLIBCXX23_CONSTEXPR
1238 reset(size_t __position)
1240 this->_M_check(__position, __N("bitset::reset"));
1241 return _Unchecked_reset(__position);
1245 * @brief Toggles every bit to its opposite value.
1247 _GLIBCXX23_CONSTEXPR
1249 flip() _GLIBCXX_NOEXCEPT
1252 this->_M_do_sanitize();
1257 * @brief Toggles a given bit to its opposite value.
1258 * @param __position The index of the bit.
1259 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1261 _GLIBCXX23_CONSTEXPR
1263 flip(size_t __position)
1265 this->_M_check(__position, __N("bitset::flip"));
1266 return _Unchecked_flip(__position);
1269 /// See the no-argument flip().
1270 _GLIBCXX23_CONSTEXPR
1272 operator~() const _GLIBCXX_NOEXCEPT
1273 { return bitset<_Nb>(*this).flip(); }
1277 * @brief Array-indexing support.
1278 * @param __position Index into the %bitset.
1279 * @return A bool for a <em>const %bitset</em>. For non-const
1280 * bitsets, an instance of the reference proxy class.
1281 * @note These operators do no range checking and throw no exceptions,
1282 * as required by DR 11 to the standard.
1284 * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
1285 * resolves DR 11 (items 1 and 2), but does not do the range-checking
1286 * required by that DR's resolution. -pme
1287 * The DR has since been changed: range-checking is a precondition
1288 * (users' responsibility), and these functions must not throw. -pme
1290 _GLIBCXX23_CONSTEXPR
1292 operator[](size_t __position)
1293 { return reference(*this, __position); }
1295 _GLIBCXX_CONSTEXPR bool
1296 operator[](size_t __position) const
1297 { return _Unchecked_test(__position); }
1301 * @brief Returns a numerical interpretation of the %bitset.
1302 * @return The integral equivalent of the bits.
1303 * @throw std::overflow_error If there are too many bits to be
1304 * represented in an @c unsigned @c long.
1306 _GLIBCXX23_CONSTEXPR
1309 { return this->_M_do_to_ulong(); }
1311#if __cplusplus >= 201103L
1312 _GLIBCXX23_CONSTEXPR
1315 { return this->_M_do_to_ullong(); }
1320 * @brief Returns a character interpretation of the %bitset.
1321 * @return The string equivalent of the bits.
1323 * Note the ordering of the bits: decreasing character positions
1324 * correspond to increasing bit positions (see the main class notes for
1327 template<class _CharT, class _Traits, class _Alloc>
1328 _GLIBCXX23_CONSTEXPR
1329 std::basic_string<_CharT, _Traits, _Alloc>
1332 std::basic_string<_CharT, _Traits, _Alloc> __result;
1333 _M_copy_to_string(__result, _CharT('0'), _CharT('1'));
1337 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1338 // 396. what are characters zero and one.
1339 template<class _CharT, class _Traits, class _Alloc>
1340 _GLIBCXX23_CONSTEXPR
1341 std::basic_string<_CharT, _Traits, _Alloc>
1342 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1344 std::basic_string<_CharT, _Traits, _Alloc> __result;
1345 _M_copy_to_string(__result, __zero, __one);
1349 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1350 // 434. bitset::to_string() hard to use.
1351 template<class _CharT, class _Traits>
1352 _GLIBCXX23_CONSTEXPR
1353 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1355 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
1357 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1358 // 853. to_string needs updating with zero and one.
1359 template<class _CharT, class _Traits>
1360 _GLIBCXX23_CONSTEXPR
1361 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1362 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1363 { return to_string<_CharT, _Traits,
1364 std::allocator<_CharT> >(__zero, __one); }
1366 template<class _CharT>
1367 _GLIBCXX23_CONSTEXPR
1368 std::basic_string<_CharT, std::char_traits<_CharT>,
1369 std::allocator<_CharT> >
1372 return to_string<_CharT, std::char_traits<_CharT>,
1373 std::allocator<_CharT> >();
1376 template<class _CharT>
1377 _GLIBCXX23_CONSTEXPR
1378 std::basic_string<_CharT, std::char_traits<_CharT>,
1379 std::allocator<_CharT> >
1380 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
1382 return to_string<_CharT, std::char_traits<_CharT>,
1383 std::allocator<_CharT> >(__zero, __one);
1386 _GLIBCXX23_CONSTEXPR
1387 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1390 return to_string<char, std::char_traits<char>,
1391 std::allocator<char> >();
1394 _GLIBCXX23_CONSTEXPR
1395 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1396 to_string(char __zero, char __one = '1') const
1398 return to_string<char, std::char_traits<char>,
1399 std::allocator<char> >(__zero, __one);
1403 /// Returns the number of bits which are set.
1404 _GLIBCXX23_CONSTEXPR
1406 count() const _GLIBCXX_NOEXCEPT
1407 { return this->_M_do_count(); }
1409 /// Returns the total number of bits.
1410 _GLIBCXX_CONSTEXPR size_t
1411 size() const _GLIBCXX_NOEXCEPT
1415 /// These comparisons for equality/inequality are, well, @e bitwise.
1416 _GLIBCXX23_CONSTEXPR
1418 operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
1419 { return this->_M_is_equal(__rhs); }
1421#if __cpp_impl_three_way_comparison < 201907L
1422 _GLIBCXX23_CONSTEXPR
1424 operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
1425 { return !this->_M_is_equal(__rhs); }
1430 * @brief Tests the value of a bit.
1431 * @param __position The index of a bit.
1432 * @return The value at @a pos.
1433 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1435 _GLIBCXX23_CONSTEXPR
1437 test(size_t __position) const
1439 this->_M_check(__position, __N("bitset::test"));
1440 return _Unchecked_test(__position);
1443 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1444 // DR 693. std::bitset::all() missing.
1446 * @brief Tests whether all the bits are on.
1447 * @return True if all the bits are set.
1449 _GLIBCXX23_CONSTEXPR
1451 all() const _GLIBCXX_NOEXCEPT
1452 { return this->template _M_are_all<_Nb>(); }
1455 * @brief Tests whether any of the bits are on.
1456 * @return True if at least one bit is set.
1458 _GLIBCXX23_CONSTEXPR
1460 any() const _GLIBCXX_NOEXCEPT
1461 { return this->_M_is_any(); }
1464 * @brief Tests whether any of the bits are on.
1465 * @return True if none of the bits are set.
1467 _GLIBCXX23_CONSTEXPR
1469 none() const _GLIBCXX_NOEXCEPT
1470 { return !this->_M_is_any(); }
1473 /// Self-explanatory.
1474 _GLIBCXX23_CONSTEXPR
1476 operator<<(size_t __position) const _GLIBCXX_NOEXCEPT
1477 { return bitset<_Nb>(*this) <<= __position; }
1479 _GLIBCXX23_CONSTEXPR
1481 operator>>(size_t __position) const _GLIBCXX_NOEXCEPT
1482 { return bitset<_Nb>(*this) >>= __position; }
1486 * @brief Finds the index of the first "on" bit.
1487 * @return The index of the first bit set, or size() if not found.
1488 * @ingroup SGIextensions
1491 _GLIBCXX23_CONSTEXPR
1493 _Find_first() const _GLIBCXX_NOEXCEPT
1494 { return this->_M_do_find_first(_Nb); }
1497 * @brief Finds the index of the next "on" bit after prev.
1498 * @return The index of the next bit set, or size() if not found.
1499 * @param __prev Where to start searching.
1500 * @ingroup SGIextensions
1503 _GLIBCXX23_CONSTEXPR
1505 _Find_next(size_t __prev) const _GLIBCXX_NOEXCEPT
1506 { return this->_M_do_find_next(__prev, _Nb); }
1509 // Helper functions for string operations.
1510 template<class _CharT, class _Traits>
1511 _GLIBCXX23_CONSTEXPR
1513 _M_copy_from_ptr(const _CharT*, size_t, size_t, size_t,
1517 template<class _CharT, class _Traits, class _Alloc>
1518 _GLIBCXX23_CONSTEXPR
1520 _M_copy_from_string(const std::basic_string<_CharT,
1521 _Traits, _Alloc>& __s, size_t __pos, size_t __n,
1522 _CharT __zero, _CharT __one)
1523 { _M_copy_from_ptr<_CharT, _Traits>(__s.data(), __s.size(), __pos, __n,
1526 template<class _CharT, class _Traits, class _Alloc>
1527 _GLIBCXX23_CONSTEXPR
1529 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&,
1530 _CharT, _CharT) const;
1532 template<class _CharT, class _Traits, size_t _Nb2>
1533 friend std::basic_istream<_CharT, _Traits>&
1534 operator>>(std::basic_istream<_CharT, _Traits>&, bitset<_Nb2>&);
1536 template <class _CharT, class _Traits, size_t _Nb2>
1537 friend std::basic_ostream<_CharT, _Traits>&
1538 operator<<(std::basic_ostream<_CharT, _Traits>&, const bitset<_Nb2>&);
1542 // Definitions of non-inline member functions.
1543 template<size_t _Nb>
1544 template<class _CharT, class _Traits>
1545 _GLIBCXX23_CONSTEXPR
1548 _M_copy_from_ptr(const _CharT* __s, size_t __len,
1549 size_t __pos, size_t __n, _CharT __zero, _CharT __one)
1552 const size_t __nbits = std::min(_Nb, std::min(__n, size_t(__len - __pos)));
1553 for (size_t __i = __nbits; __i > 0; --__i)
1555 const _CharT __c = __s[__pos + __nbits - __i];
1556 if (_Traits::eq(__c, __zero))
1558 else if (_Traits::eq(__c, __one))
1559 _Unchecked_set(__i - 1);
1561 __throw_invalid_argument(__N("bitset::_M_copy_from_ptr"));
1566 template<size_t _Nb>
1567 template<class _CharT, class _Traits, class _Alloc>
1568 _GLIBCXX23_CONSTEXPR
1571 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s,
1572 _CharT __zero, _CharT __one) const
1574 __s.assign(_Nb, __zero);
1575 size_t __n = this->_Find_first();
1578 __s[_Nb - __n - 1] = __one;
1579 __n = _Find_next(__n);
1584 // 23.3.5.3 bitset operations:
1587 * @brief Global bitwise operations on bitsets.
1588 * @param __x A bitset.
1589 * @param __y A bitset of the same size as @a __x.
1590 * @return A new bitset.
1592 * These should be self-explanatory.
1594 template<size_t _Nb>
1595 _GLIBCXX23_CONSTEXPR
1597 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1599 bitset<_Nb> __result(__x);
1604 template<size_t _Nb>
1605 _GLIBCXX23_CONSTEXPR
1607 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1609 bitset<_Nb> __result(__x);
1614 template <size_t _Nb>
1615 _GLIBCXX23_CONSTEXPR
1617 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
1619 bitset<_Nb> __result(__x);
1628 * @brief Global I/O operators for bitsets.
1630 * Direct I/O between streams and bitsets is supported. Output is
1631 * straightforward. Input will skip whitespace, only accept @a 0 and @a 1
1632 * characters, and will only extract as many digits as the %bitset will
1635 template<class _CharT, class _Traits, size_t _Nb>
1636 std::basic_istream<_CharT, _Traits>&
1637 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
1639 typedef typename _Traits::char_type char_type;
1640 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1641 typedef typename __istream_type::ios_base __ios_base;
1643#pragma GCC diagnostic push
1644#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
1647 static _GLIBCXX_CONSTEXPR bool _S_use_alloca() { return _Nb <= 256; }
1649 explicit _Buffer(_CharT* __p) : _M_ptr(__p) { }
1653 if _GLIBCXX_CONSTEXPR (!_S_use_alloca())
1657 _CharT* const _M_ptr;
1660 if _GLIBCXX_CONSTEXPR (_Buffer::_S_use_alloca())
1661 __ptr = (_CharT*)__builtin_alloca(_Nb);
1663 __ptr = new _CharT[_Nb];
1664 const _Buffer __buf(__ptr);
1665#pragma GCC diagnostic pop
1667 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1668 // 303. Bitset input operator underspecified
1669 const char_type __zero = __is.widen('0');
1670 const char_type __one = __is.widen('1');
1672 typename __ios_base::iostate __state = __ios_base::goodbit;
1673 typename __istream_type::sentry __sentry(__is);
1678 for (size_t __i = _Nb; __i > 0; --__i)
1680 static typename _Traits::int_type __eof = _Traits::eof();
1682 typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
1683 if (_Traits::eq_int_type(__c1, __eof))
1685 __state |= __ios_base::eofbit;
1690 const char_type __c2 = _Traits::to_char_type(__c1);
1691 if (_Traits::eq(__c2, __zero))
1693 else if (_Traits::eq(__c2, __one))
1696 eq_int_type(__is.rdbuf()->sputbackc(__c2),
1699 __state |= __ios_base::failbit;
1705 __catch(__cxxabiv1::__forced_unwind&)
1707 __is._M_setstate(__ios_base::badbit);
1708 __throw_exception_again;
1711 { __is._M_setstate(__ios_base::badbit); }
1714#pragma GCC diagnostic push
1715#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
1716 if _GLIBCXX_CONSTEXPR (_Nb)
1718 if (size_t __len = __ptr - __buf._M_ptr)
1719 __x.template _M_copy_from_ptr<_CharT, _Traits>(__buf._M_ptr, __len,
1723 __state |= __ios_base::failbit;
1725#pragma GCC diagnostic pop
1727 __is.setstate(__state);
1731 template <class _CharT, class _Traits, size_t _Nb>
1732 std::basic_ostream<_CharT, _Traits>&
1733 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1734 const bitset<_Nb>& __x)
1736 std::basic_string<_CharT, _Traits> __tmp;
1738 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1739 // 396. what are characters zero and one.
1740 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__os.getloc());
1741 __x._M_copy_to_string(__tmp, __ct.widen('0'), __ct.widen('1'));
1742 return __os << __tmp;
1747_GLIBCXX_END_NAMESPACE_CONTAINER
1750#undef _GLIBCXX_BITSET_WORDS
1751#undef _GLIBCXX_BITSET_BITS_PER_WORD
1752#undef _GLIBCXX_BITSET_BITS_PER_ULL
1754#if __cplusplus >= 201103L
1756namespace std _GLIBCXX_VISIBILITY(default)
1758_GLIBCXX_BEGIN_NAMESPACE_VERSION
1761 /// std::hash specialization for bitset.
1762 template<size_t _Nb>
1763 struct hash<_GLIBCXX_STD_C::bitset<_Nb>>
1764 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<_Nb>>
1767 operator()(const _GLIBCXX_STD_C::bitset<_Nb>& __b) const noexcept
1769 const size_t __clength = (_Nb + __CHAR_BIT__ - 1) / __CHAR_BIT__;
1770 return std::_Hash_impl::hash(__b._M_getdata(), __clength);
1775 struct hash<_GLIBCXX_STD_C::bitset<0>>
1776 : public __hash_base<size_t, _GLIBCXX_STD_C::bitset<0>>
1779 operator()(const _GLIBCXX_STD_C::bitset<0>&) const noexcept
1783_GLIBCXX_END_NAMESPACE_VERSION
1788#if defined _GLIBCXX_DEBUG && _GLIBCXX_HOSTED
1789# include <debug/bitset>
1792#endif /* _GLIBCXX_BITSET */