1 // <bitset> -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this library; see the file COPYING. If not, write to
19 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 // Boston, MA 02110-1301, USA.
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
33 * Silicon Graphics Computer Systems, Inc.
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Silicon Graphics makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
44 /** @file include/bitset
45 * This is a Standard C++ Library header.
48 #ifndef _GLIBCXX_BITSET
49 #define _GLIBCXX_BITSET 1
51 #pragma GCC system_header
53 #include <cstddef> // For size_t
55 #include <bits/functexcept.h> // For invalid_argument, out_of_range,
58 #include <cxxabi-forced.h>
60 #define _GLIBCXX_BITSET_BITS_PER_WORD (__CHAR_BIT__ * sizeof(unsigned long))
61 #define _GLIBCXX_BITSET_WORDS(__n) \
62 ((__n) < 1 ? 0 : ((__n) + _GLIBCXX_BITSET_BITS_PER_WORD - 1) \
63 / _GLIBCXX_BITSET_BITS_PER_WORD)
65 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
68 * Base class, general case. It is a class invariant that _Nw will be
71 * See documentation for bitset.
76 typedef unsigned long _WordT;
78 /// 0 is the least significant word.
84 _Base_bitset(unsigned long __val)
91 _S_whichword(size_t __pos )
92 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
95 _S_whichbyte(size_t __pos )
96 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
99 _S_whichbit(size_t __pos )
100 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
103 _S_maskbit(size_t __pos )
104 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
107 _M_getword(size_t __pos)
108 { return _M_w[_S_whichword(__pos)]; }
111 _M_getword(size_t __pos) const
112 { return _M_w[_S_whichword(__pos)]; }
116 { return _M_w[_Nw - 1]; }
120 { return _M_w[_Nw - 1]; }
123 _M_do_and(const _Base_bitset<_Nw>& __x)
125 for (size_t __i = 0; __i < _Nw; __i++)
126 _M_w[__i] &= __x._M_w[__i];
130 _M_do_or(const _Base_bitset<_Nw>& __x)
132 for (size_t __i = 0; __i < _Nw; __i++)
133 _M_w[__i] |= __x._M_w[__i];
137 _M_do_xor(const _Base_bitset<_Nw>& __x)
139 for (size_t __i = 0; __i < _Nw; __i++)
140 _M_w[__i] ^= __x._M_w[__i];
144 _M_do_left_shift(size_t __shift);
147 _M_do_right_shift(size_t __shift);
152 for (size_t __i = 0; __i < _Nw; __i++)
153 _M_w[__i] = ~_M_w[__i];
159 for (size_t __i = 0; __i < _Nw; __i++)
160 _M_w[__i] = ~static_cast<_WordT>(0);
165 { __builtin_memset(_M_w, 0, _Nw * sizeof(_WordT)); }
168 _M_is_equal(const _Base_bitset<_Nw>& __x) const
170 for (size_t __i = 0; __i < _Nw; ++__i)
171 if (_M_w[__i] != __x._M_w[__i])
177 _M_are_all_aux() const
179 for (size_t __i = 0; __i < _Nw - 1; __i++)
180 if (_M_w[__i] != ~static_cast<_WordT>(0))
182 return ((_Nw - 1) * _GLIBCXX_BITSET_BITS_PER_WORD
183 + __builtin_popcountl(_M_hiword()));
189 for (size_t __i = 0; __i < _Nw; __i++)
190 if (_M_w[__i] != static_cast<_WordT>(0))
199 for (size_t __i = 0; __i < _Nw; __i++)
200 __result += __builtin_popcountl(_M_w[__i]);
205 _M_do_to_ulong() const;
207 // find first "on" bit
209 _M_do_find_first(size_t __not_found) const;
211 // find the next "on" bit that follows "prev"
213 _M_do_find_next(size_t __prev, size_t __not_found) const;
216 // Definitions of non-inline functions from _Base_bitset.
219 _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift)
221 if (__builtin_expect(__shift != 0, 1))
223 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
224 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
227 for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
228 _M_w[__n] = _M_w[__n - __wshift];
231 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
233 for (size_t __n = _Nw - 1; __n > __wshift; --__n)
234 _M_w[__n] = ((_M_w[__n - __wshift] << __offset)
235 | (_M_w[__n - __wshift - 1] >> __sub_offset));
236 _M_w[__wshift] = _M_w[0] << __offset;
239 std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
245 _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift)
247 if (__builtin_expect(__shift != 0, 1))
249 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
250 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
251 const size_t __limit = _Nw - __wshift - 1;
254 for (size_t __n = 0; __n <= __limit; ++__n)
255 _M_w[__n] = _M_w[__n + __wshift];
258 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
260 for (size_t __n = 0; __n < __limit; ++__n)
261 _M_w[__n] = ((_M_w[__n + __wshift] >> __offset)
262 | (_M_w[__n + __wshift + 1] << __sub_offset));
263 _M_w[__limit] = _M_w[_Nw-1] >> __offset;
266 std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
272 _Base_bitset<_Nw>::_M_do_to_ulong() const
274 for (size_t __i = 1; __i < _Nw; ++__i)
276 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
282 _Base_bitset<_Nw>::_M_do_find_first(size_t __not_found) const
284 for (size_t __i = 0; __i < _Nw; __i++)
286 _WordT __thisword = _M_w[__i];
287 if (__thisword != static_cast<_WordT>(0))
288 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
289 + __builtin_ctzl(__thisword));
291 // not found, so return an indication of failure.
297 _Base_bitset<_Nw>::_M_do_find_next(size_t __prev, size_t __not_found) const
299 // make bound inclusive
302 // check out of bounds
303 if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
307 size_t __i = _S_whichword(__prev);
308 _WordT __thisword = _M_w[__i];
310 // mask off bits below bound
311 __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
313 if (__thisword != static_cast<_WordT>(0))
314 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
315 + __builtin_ctzl(__thisword));
317 // check subsequent words
319 for (; __i < _Nw; __i++)
321 __thisword = _M_w[__i];
322 if (__thisword != static_cast<_WordT>(0))
323 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
324 + __builtin_ctzl(__thisword));
326 // not found, so return an indication of failure.
328 } // end _M_do_find_next
331 * Base class, specialization for a single word.
333 * See documentation for bitset.
336 struct _Base_bitset<1>
338 typedef unsigned long _WordT;
345 _Base_bitset(unsigned long __val)
350 _S_whichword(size_t __pos )
351 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
354 _S_whichbyte(size_t __pos )
355 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
358 _S_whichbit(size_t __pos )
359 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
362 _S_maskbit(size_t __pos )
363 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
370 _M_getword(size_t) const
382 _M_do_and(const _Base_bitset<1>& __x)
383 { _M_w &= __x._M_w; }
386 _M_do_or(const _Base_bitset<1>& __x)
387 { _M_w |= __x._M_w; }
390 _M_do_xor(const _Base_bitset<1>& __x)
391 { _M_w ^= __x._M_w; }
394 _M_do_left_shift(size_t __shift)
395 { _M_w <<= __shift; }
398 _M_do_right_shift(size_t __shift)
399 { _M_w >>= __shift; }
407 { _M_w = ~static_cast<_WordT>(0); }
414 _M_is_equal(const _Base_bitset<1>& __x) const
415 { return _M_w == __x._M_w; }
418 _M_are_all_aux() const
419 { return __builtin_popcountl(_M_w); }
423 { return _M_w != 0; }
427 { return __builtin_popcountl(_M_w); }
430 _M_do_to_ulong() const
434 _M_do_find_first(size_t __not_found) const
437 return __builtin_ctzl(_M_w);
442 // find the next "on" bit that follows "prev"
444 _M_do_find_next(size_t __prev, size_t __not_found) const
447 if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
450 _WordT __x = _M_w >> __prev;
452 return __builtin_ctzl(__x) + __prev;
459 * Base class, specialization for no storage (zero-length %bitset).
461 * See documentation for bitset.
464 struct _Base_bitset<0>
466 typedef unsigned long _WordT;
471 _Base_bitset(unsigned long)
475 _S_whichword(size_t __pos )
476 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
479 _S_whichbyte(size_t __pos )
480 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
483 _S_whichbit(size_t __pos )
484 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
487 _S_maskbit(size_t __pos )
488 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
490 // This would normally give access to the data. The bounds-checking
491 // in the bitset class will prevent the user from getting this far,
492 // but (1) it must still return an lvalue to compile, and (2) the
493 // user might call _Unchecked_set directly, in which case this /needs/
494 // to fail. Let's not penalize zero-length users unless they actually
495 // make an unchecked call; all the memory ugliness is therefore
496 // localized to this single should-never-get-this-far function.
498 _M_getword(size_t) const
500 __throw_out_of_range(__N("_Base_bitset::_M_getword"));
509 _M_do_and(const _Base_bitset<0>&)
513 _M_do_or(const _Base_bitset<0>&)
517 _M_do_xor(const _Base_bitset<0>&)
521 _M_do_left_shift(size_t)
525 _M_do_right_shift(size_t)
540 // Are all empty bitsets equal to each other? Are they equal to
541 // themselves? How to compare a thing which has no state? What is
542 // the sound of one zero-length bitset clapping?
544 _M_is_equal(const _Base_bitset<0>&) const
548 _M_are_all_aux() const
560 _M_do_to_ulong() const
563 // Normally "not found" is the size, but that could also be
564 // misinterpreted as an index in this corner case. Oh well.
566 _M_do_find_first(size_t) const
570 _M_do_find_next(size_t, size_t) const
575 // Helper class to zero out the unused high-order bits in the highest word.
576 template<size_t _Extrabits>
579 static void _S_do_sanitize(unsigned long& __val)
580 { __val &= ~((~static_cast<unsigned long>(0)) << _Extrabits); }
585 { static void _S_do_sanitize(unsigned long) {} };
588 * @brief The %bitset class represents a @e fixed-size sequence of bits.
590 * @ingroup Containers
592 * (Note that %bitset does @e not meet the formal requirements of a
593 * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
595 * The template argument, @a Nb, may be any non-negative number,
596 * specifying the number of bits (e.g., "0", "12", "1024*1024").
598 * In the general unoptimized case, storage is allocated in word-sized
599 * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
600 * words will be used for storage. B - Nb%B bits are unused. (They are
601 * the high-order bits in the highest word.) It is a class invariant
602 * that those unused bits are always zero.
604 * If you think of %bitset as "a simple array of bits," be aware that
605 * your mental picture is reversed: a %bitset behaves the same way as
606 * bits in integers do, with the bit at index 0 in the "least significant
607 * / right-hand" position, and the bit at index Nb-1 in the "most
608 * significant / left-hand" position. Thus, unlike other containers, a
609 * %bitset's index "counts from right to left," to put it very loosely.
611 * This behavior is preserved when translating to and from strings. For
612 * example, the first line of the following program probably prints
613 * "b('a') is 0001100001" on a modern ASCII system.
617 * #include <iostream>
620 * using namespace std;
627 * cout << "b('a') is " << b << endl;
631 * string str = s.str();
632 * cout << "index 3 in the string is " << str[3] << " but\n"
633 * << "index 3 in the bitset is " << b[3] << endl;
638 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt12ch33s02.html
639 * for a description of extensions.
641 * Most of the actual code isn't contained in %bitset<> itself, but in the
642 * base class _Base_bitset. The base class works with whole words, not with
643 * individual bits. This allows us to specialize _Base_bitset for the
644 * important special case where the %bitset is only a single word.
646 * Extra confusion can result due to the fact that the storage for
647 * _Base_bitset @e is a regular array, and is indexed as such. This is
648 * carefully encapsulated.
652 : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
655 typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
656 typedef unsigned long _WordT;
661 _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD>::
662 _S_do_sanitize(this->_M_hiword());
667 * This encapsulates the concept of a single bit. An instance of this
668 * class is a proxy for an actual bit; this way the individual bit
669 * operations are done as faster word-size bitwise instructions.
671 * Most users will never need to use this class directly; conversions
672 * to and from bool are automatic and should be transparent. Overloaded
673 * operators help to preserve the illusion.
675 * (On a typical system, this "bit %reference" is 64 times the size of
676 * an actual bit. Ha.)
689 reference(bitset& __b, size_t __pos)
691 _M_wp = &__b._M_getword(__pos);
692 _M_bpos = _Base::_S_whichbit(__pos);
703 *_M_wp |= _Base::_S_maskbit(_M_bpos);
705 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
709 // For b[i] = b[__j];
711 operator=(const reference& __j)
713 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
714 *_M_wp |= _Base::_S_maskbit(_M_bpos);
716 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
723 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
726 operator bool() const
727 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
733 *_M_wp ^= _Base::_S_maskbit(_M_bpos);
737 friend class reference;
739 // 23.3.5.1 constructors:
740 /// All bits set to zero.
744 /// Initial bits bitwise-copied from a single word (others set to zero).
745 bitset(unsigned long __val)
747 { _M_do_sanitize(); }
750 * @brief Use a subset of a string.
751 * @param s A string of '0' and '1' characters.
752 * @param position Index of the first character in @a s to use;
754 * @throw std::out_of_range If @a pos is bigger the size of @a s.
755 * @throw std::invalid_argument If a character appears in the string
756 * which is neither '0' nor '1'.
758 template<class _CharT, class _Traits, class _Alloc>
760 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
761 size_t __position = 0)
764 if (__position > __s.size())
765 __throw_out_of_range(__N("bitset::bitset initial position "
767 _M_copy_from_string(__s, __position,
768 std::basic_string<_CharT, _Traits, _Alloc>::npos);
772 * @brief Use a subset of a string.
773 * @param s A string of '0' and '1' characters.
774 * @param position Index of the first character in @a s to use.
775 * @param n The number of characters to copy.
776 * @throw std::out_of_range If @a pos is bigger the size of @a s.
777 * @throw std::invalid_argument If a character appears in the string
778 * which is neither '0' nor '1'.
780 template<class _CharT, class _Traits, class _Alloc>
781 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
782 size_t __position, size_t __n)
785 if (__position > __s.size())
786 __throw_out_of_range(__N("bitset::bitset initial position "
788 _M_copy_from_string(__s, __position, __n);
791 // 23.3.5.2 bitset operations:
794 * @brief Operations on bitsets.
795 * @param rhs A same-sized bitset.
797 * These should be self-explanatory.
800 operator&=(const bitset<_Nb>& __rhs)
802 this->_M_do_and(__rhs);
807 operator|=(const bitset<_Nb>& __rhs)
809 this->_M_do_or(__rhs);
814 operator^=(const bitset<_Nb>& __rhs)
816 this->_M_do_xor(__rhs);
823 * @brief Operations on bitsets.
824 * @param position The number of places to shift.
826 * These should be self-explanatory.
829 operator<<=(size_t __position)
831 if (__builtin_expect(__position < _Nb, 1))
833 this->_M_do_left_shift(__position);
834 this->_M_do_sanitize();
842 operator>>=(size_t __position)
844 if (__builtin_expect(__position < _Nb, 1))
846 this->_M_do_right_shift(__position);
847 this->_M_do_sanitize();
857 * These versions of single-bit set, reset, flip, and test are
858 * extensions from the SGI version. They do no range checking.
859 * @ingroup SGIextensions
862 _Unchecked_set(size_t __pos)
864 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
869 _Unchecked_set(size_t __pos, int __val)
872 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
874 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
879 _Unchecked_reset(size_t __pos)
881 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
886 _Unchecked_flip(size_t __pos)
888 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
893 _Unchecked_test(size_t __pos) const
894 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
895 != static_cast<_WordT>(0)); }
898 // Set, reset, and flip.
900 * @brief Sets every bit to true.
906 this->_M_do_sanitize();
911 * @brief Sets a given bit to a particular value.
912 * @param position The index of the bit.
913 * @param val Either true or false, defaults to true.
914 * @throw std::out_of_range If @a pos is bigger the size of the %set.
917 set(size_t __position, bool __val = true)
919 if (__position >= _Nb)
920 __throw_out_of_range(__N("bitset::set"));
921 return _Unchecked_set(__position, __val);
925 * @brief Sets every bit to false.
935 * @brief Sets a given bit to false.
936 * @param position The index of the bit.
937 * @throw std::out_of_range If @a pos is bigger the size of the %set.
939 * Same as writing @c set(pos,false).
942 reset(size_t __position)
944 if (__position >= _Nb)
945 __throw_out_of_range(__N("bitset::reset"));
946 return _Unchecked_reset(__position);
950 * @brief Toggles every bit to its opposite value.
956 this->_M_do_sanitize();
961 * @brief Toggles a given bit to its opposite value.
962 * @param position The index of the bit.
963 * @throw std::out_of_range If @a pos is bigger the size of the %set.
966 flip(size_t __position)
968 if (__position >= _Nb)
969 __throw_out_of_range(__N("bitset::flip"));
970 return _Unchecked_flip(__position);
973 /// See the no-argument flip().
976 { return bitset<_Nb>(*this).flip(); }
980 * @brief Array-indexing support.
981 * @param position Index into the %bitset.
982 * @return A bool for a 'const %bitset'. For non-const bitsets, an
983 * instance of the reference proxy class.
984 * @note These operators do no range checking and throw no exceptions,
985 * as required by DR 11 to the standard.
987 * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
988 * resolves DR 11 (items 1 and 2), but does not do the range-checking
989 * required by that DR's resolution. -pme
990 * The DR has since been changed: range-checking is a precondition
991 * (users' responsibility), and these functions must not throw. -pme
994 operator[](size_t __position)
995 { return reference(*this,__position); }
998 operator[](size_t __position) const
999 { return _Unchecked_test(__position); }
1003 * @brief Returns a numerical interpretation of the %bitset.
1004 * @return The integral equivalent of the bits.
1005 * @throw std::overflow_error If there are too many bits to be
1006 * represented in an @c unsigned @c long.
1010 { return this->_M_do_to_ulong(); }
1013 * @brief Returns a character interpretation of the %bitset.
1014 * @return The string equivalent of the bits.
1016 * Note the ordering of the bits: decreasing character positions
1017 * correspond to increasing bit positions (see the main class notes for
1020 template<class _CharT, class _Traits, class _Alloc>
1021 std::basic_string<_CharT, _Traits, _Alloc>
1024 std::basic_string<_CharT, _Traits, _Alloc> __result;
1025 _M_copy_to_string(__result);
1029 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1030 // 434. bitset::to_string() hard to use.
1031 template<class _CharT, class _Traits>
1032 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1034 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
1036 template<class _CharT>
1037 std::basic_string<_CharT, std::char_traits<_CharT>,
1038 std::allocator<_CharT> >
1041 return to_string<_CharT, std::char_traits<_CharT>,
1042 std::allocator<_CharT> >();
1045 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1048 return to_string<char, std::char_traits<char>,
1049 std::allocator<char> >();
1052 // Helper functions for string operations.
1053 template<class _CharT, class _Traits, class _Alloc>
1055 _M_copy_from_string(const std::basic_string<_CharT,
1056 _Traits, _Alloc>& __s,
1059 template<class _CharT, class _Traits, class _Alloc>
1061 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&) const;
1063 /// Returns the number of bits which are set.
1066 { return this->_M_do_count(); }
1068 /// Returns the total number of bits.
1074 /// These comparisons for equality/inequality are, well, @e bitwise.
1076 operator==(const bitset<_Nb>& __rhs) const
1077 { return this->_M_is_equal(__rhs); }
1080 operator!=(const bitset<_Nb>& __rhs) const
1081 { return !this->_M_is_equal(__rhs); }
1085 * @brief Tests the value of a bit.
1086 * @param position The index of a bit.
1087 * @return The value at @a pos.
1088 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1091 test(size_t __position) const
1093 if (__position >= _Nb)
1094 __throw_out_of_range(__N("bitset::test"));
1095 return _Unchecked_test(__position);
1098 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1099 // DR 693. std::bitset::all() missing.
1101 * @brief Tests whether all the bits are on.
1102 * @return True if all the bits are set.
1106 { return this->_M_are_all_aux() == _Nb; }
1109 * @brief Tests whether any of the bits are on.
1110 * @return True if at least one bit is set.
1114 { return this->_M_is_any(); }
1117 * @brief Tests whether any of the bits are on.
1118 * @return True if none of the bits are set.
1122 { return !this->_M_is_any(); }
1125 /// Self-explanatory.
1127 operator<<(size_t __position) const
1128 { return bitset<_Nb>(*this) <<= __position; }
1131 operator>>(size_t __position) const
1132 { return bitset<_Nb>(*this) >>= __position; }
1136 * @brief Finds the index of the first "on" bit.
1137 * @return The index of the first bit set, or size() if not found.
1138 * @ingroup SGIextensions
1143 { return this->_M_do_find_first(_Nb); }
1146 * @brief Finds the index of the next "on" bit after prev.
1147 * @return The index of the next bit set, or size() if not found.
1148 * @param prev Where to start searching.
1149 * @ingroup SGIextensions
1153 _Find_next(size_t __prev ) const
1154 { return this->_M_do_find_next(__prev, _Nb); }
1157 // Definitions of non-inline member functions.
1158 template<size_t _Nb>
1159 template<class _CharT, class _Traits, class _Alloc>
1162 _M_copy_from_string(const std::basic_string<_CharT, _Traits,
1163 _Alloc>& __s, size_t __pos, size_t __n)
1166 const size_t __nbits = std::min(_Nb, std::min(__n, __s.size() - __pos));
1167 for (size_t __i = __nbits; __i > 0; --__i)
1169 switch(__s[__pos + __nbits - __i])
1174 _Unchecked_set(__i - 1);
1177 __throw_invalid_argument(__N("bitset::_M_copy_from_string"));
1182 template<size_t _Nb>
1183 template<class _CharT, class _Traits, class _Alloc>
1186 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s) const
1188 __s.assign(_Nb, '0');
1189 for (size_t __i = _Nb; __i > 0; --__i)
1190 if (_Unchecked_test(__i - 1))
1191 __s[_Nb - __i] = '1';
1194 // 23.3.5.3 bitset operations:
1197 * @brief Global bitwise operations on bitsets.
1198 * @param x A bitset.
1199 * @param y A bitset of the same size as @a x.
1200 * @return A new bitset.
1202 * These should be self-explanatory.
1204 template<size_t _Nb>
1206 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1208 bitset<_Nb> __result(__x);
1213 template<size_t _Nb>
1215 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1217 bitset<_Nb> __result(__x);
1222 template <size_t _Nb>
1224 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1226 bitset<_Nb> __result(__x);
1234 * @brief Global I/O operators for bitsets.
1236 * Direct I/O between streams and bitsets is supported. Output is
1237 * straightforward. Input will skip whitespace, only accept '0' and '1'
1238 * characters, and will only extract as many digits as the %bitset will
1241 template<class _CharT, class _Traits, size_t _Nb>
1242 std::basic_istream<_CharT, _Traits>&
1243 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
1245 typedef typename _Traits::char_type char_type;
1246 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1247 typedef typename __istream_type::ios_base __ios_base;
1249 std::basic_string<_CharT, _Traits> __tmp;
1252 typename __ios_base::iostate __state = __ios_base::goodbit;
1253 typename __istream_type::sentry __sentry(__is);
1258 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1259 // 303. Bitset input operator underspecified
1260 const char_type __zero = __is.widen('0');
1261 const char_type __one = __is.widen('1');
1262 for (size_t __i = _Nb; __i > 0; --__i)
1264 static typename _Traits::int_type __eof = _Traits::eof();
1266 typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
1267 if (_Traits::eq_int_type(__c1, __eof))
1269 __state |= __ios_base::eofbit;
1274 const char_type __c2 = _Traits::to_char_type(__c1);
1276 __tmp.push_back('0');
1277 else if (__c2 == __one)
1278 __tmp.push_back('1');
1280 eq_int_type(__is.rdbuf()->sputbackc(__c2),
1283 __state |= __ios_base::failbit;
1289 catch(__cxxabiv1::__forced_unwind&)
1291 __is._M_setstate(__ios_base::badbit);
1292 __throw_exception_again;
1295 { __is._M_setstate(__ios_base::badbit); }
1298 if (__tmp.empty() && _Nb)
1299 __state |= __ios_base::failbit;
1301 __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb);
1303 __is.setstate(__state);
1307 template <class _CharT, class _Traits, size_t _Nb>
1308 std::basic_ostream<_CharT, _Traits>&
1309 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1310 const bitset<_Nb>& __x)
1312 std::basic_string<_CharT, _Traits> __tmp;
1313 __x._M_copy_to_string(__tmp);
1314 return __os << __tmp;
1318 _GLIBCXX_END_NESTED_NAMESPACE
1320 #undef _GLIBCXX_BITSET_WORDS
1321 #undef _GLIBCXX_BITSET_BITS_PER_WORD
1323 #ifdef _GLIBCXX_DEBUG
1324 # include <debug/bitset>
1327 #endif /* _GLIBCXX_BITSET */