1 // <bitset> -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
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
54 #include <cstring> // For memset
56 #include <bits/functexcept.h> // For invalid_argument, out_of_range,
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)
69 * Base class, general case. It is a class inveriant that _Nw will be
72 * See documentation for bitset.
78 typedef unsigned long _WordT;
80 /// 0 is the least significant word.
86 _Base_bitset(unsigned long __val)
93 _S_whichword(size_t __pos )
94 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
97 _S_whichbyte(size_t __pos )
98 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
101 _S_whichbit(size_t __pos )
102 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
105 _S_maskbit(size_t __pos )
106 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
109 _M_getword(size_t __pos)
110 { return _M_w[_S_whichword(__pos)]; }
113 _M_getword(size_t __pos) const
114 { return _M_w[_S_whichword(__pos)]; }
118 { return _M_w[_Nw - 1]; }
122 { return _M_w[_Nw - 1]; }
125 _M_do_and(const _Base_bitset<_Nw>& __x)
127 for (size_t __i = 0; __i < _Nw; __i++)
128 _M_w[__i] &= __x._M_w[__i];
132 _M_do_or(const _Base_bitset<_Nw>& __x)
134 for (size_t __i = 0; __i < _Nw; __i++)
135 _M_w[__i] |= __x._M_w[__i];
139 _M_do_xor(const _Base_bitset<_Nw>& __x)
141 for (size_t __i = 0; __i < _Nw; __i++)
142 _M_w[__i] ^= __x._M_w[__i];
146 _M_do_left_shift(size_t __shift);
149 _M_do_right_shift(size_t __shift);
154 for (size_t __i = 0; __i < _Nw; __i++)
155 _M_w[__i] = ~_M_w[__i];
161 for (size_t __i = 0; __i < _Nw; __i++)
162 _M_w[__i] = ~static_cast<_WordT>(0);
167 { std::memset(_M_w, 0, _Nw * sizeof(_WordT)); }
170 _M_is_equal(const _Base_bitset<_Nw>& __x) const
172 for (size_t __i = 0; __i < _Nw; ++__i)
174 if (_M_w[__i] != __x._M_w[__i])
183 for (size_t __i = 0; __i < _Nw; __i++)
185 if (_M_w[__i] != static_cast<_WordT>(0))
195 for (size_t __i = 0; __i < _Nw; __i++)
196 __result += __builtin_popcountl(_M_w[__i]);
201 _M_do_to_ulong() const;
203 // find first "on" bit
205 _M_do_find_first(size_t __not_found) const;
207 // find the next "on" bit that follows "prev"
209 _M_do_find_next(size_t __prev, size_t __not_found) const;
212 // Definitions of non-inline functions from _Base_bitset.
215 _Base_bitset<_Nw>::_M_do_left_shift(size_t __shift)
217 if (__builtin_expect(__shift != 0, 1))
219 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
220 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
223 for (size_t __n = _Nw - 1; __n >= __wshift; --__n)
224 _M_w[__n] = _M_w[__n - __wshift];
227 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
229 for (size_t __n = _Nw - 1; __n > __wshift; --__n)
230 _M_w[__n] = ((_M_w[__n - __wshift] << __offset)
231 | (_M_w[__n - __wshift - 1] >> __sub_offset));
232 _M_w[__wshift] = _M_w[0] << __offset;
235 std::fill(_M_w + 0, _M_w + __wshift, static_cast<_WordT>(0));
241 _Base_bitset<_Nw>::_M_do_right_shift(size_t __shift)
243 if (__builtin_expect(__shift != 0, 1))
245 const size_t __wshift = __shift / _GLIBCXX_BITSET_BITS_PER_WORD;
246 const size_t __offset = __shift % _GLIBCXX_BITSET_BITS_PER_WORD;
247 const size_t __limit = _Nw - __wshift - 1;
250 for (size_t __n = 0; __n <= __limit; ++__n)
251 _M_w[__n] = _M_w[__n + __wshift];
254 const size_t __sub_offset = (_GLIBCXX_BITSET_BITS_PER_WORD
256 for (size_t __n = 0; __n < __limit; ++__n)
257 _M_w[__n] = ((_M_w[__n + __wshift] >> __offset)
258 | (_M_w[__n + __wshift + 1] << __sub_offset));
259 _M_w[__limit] = _M_w[_Nw-1] >> __offset;
262 std::fill(_M_w + __limit + 1, _M_w + _Nw, static_cast<_WordT>(0));
268 _Base_bitset<_Nw>::_M_do_to_ulong() const
270 for (size_t __i = 1; __i < _Nw; ++__i)
272 __throw_overflow_error(__N("_Base_bitset::_M_do_to_ulong"));
278 _Base_bitset<_Nw>::_M_do_find_first(size_t __not_found) const
280 for (size_t __i = 0; __i < _Nw; __i++)
282 _WordT __thisword = _M_w[__i];
283 if (__thisword != static_cast<_WordT>(0))
284 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
285 + __builtin_ctzl(__thisword));
287 // not found, so return an indication of failure.
293 _Base_bitset<_Nw>::_M_do_find_next(size_t __prev, size_t __not_found) const
295 // make bound inclusive
298 // check out of bounds
299 if (__prev >= _Nw * _GLIBCXX_BITSET_BITS_PER_WORD)
303 size_t __i = _S_whichword(__prev);
304 _WordT __thisword = _M_w[__i];
306 // mask off bits below bound
307 __thisword &= (~static_cast<_WordT>(0)) << _S_whichbit(__prev);
309 if (__thisword != static_cast<_WordT>(0))
310 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
311 + __builtin_ctzl(__thisword));
313 // check subsequent words
315 for (; __i < _Nw; __i++)
317 __thisword = _M_w[__i];
318 if (__thisword != static_cast<_WordT>(0))
319 return (__i * _GLIBCXX_BITSET_BITS_PER_WORD
320 + __builtin_ctzl(__thisword));
322 // not found, so return an indication of failure.
324 } // end _M_do_find_next
328 * Base class, specialization for a single word.
330 * See documentation for bitset.
334 struct _Base_bitset<1>
336 typedef unsigned long _WordT;
343 _Base_bitset(unsigned long __val)
348 _S_whichword(size_t __pos )
349 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
352 _S_whichbyte(size_t __pos )
353 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
356 _S_whichbit(size_t __pos )
357 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
360 _S_maskbit(size_t __pos )
361 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
368 _M_getword(size_t) const
380 _M_do_and(const _Base_bitset<1>& __x)
381 { _M_w &= __x._M_w; }
384 _M_do_or(const _Base_bitset<1>& __x)
385 { _M_w |= __x._M_w; }
388 _M_do_xor(const _Base_bitset<1>& __x)
389 { _M_w ^= __x._M_w; }
392 _M_do_left_shift(size_t __shift)
393 { _M_w <<= __shift; }
396 _M_do_right_shift(size_t __shift)
397 { _M_w >>= __shift; }
405 { _M_w = ~static_cast<_WordT>(0); }
412 _M_is_equal(const _Base_bitset<1>& __x) const
413 { return _M_w == __x._M_w; }
417 { return _M_w != 0; }
421 { return __builtin_popcountl(_M_w); }
424 _M_do_to_ulong() const
428 _M_do_find_first(size_t __not_found) const
431 return __builtin_ctzl(_M_w);
436 // find the next "on" bit that follows "prev"
438 _M_do_find_next(size_t __prev, size_t __not_found) const
441 if (__prev >= ((size_t) _GLIBCXX_BITSET_BITS_PER_WORD))
444 _WordT __x = _M_w >> __prev;
446 return __builtin_ctzl(__x) + __prev;
454 * Base class, specialization for no storage (zero-length %bitset).
456 * See documentation for bitset.
460 struct _Base_bitset<0>
462 typedef unsigned long _WordT;
467 _Base_bitset(unsigned long)
471 _S_whichword(size_t __pos )
472 { return __pos / _GLIBCXX_BITSET_BITS_PER_WORD; }
475 _S_whichbyte(size_t __pos )
476 { return (__pos % _GLIBCXX_BITSET_BITS_PER_WORD) / __CHAR_BIT__; }
479 _S_whichbit(size_t __pos )
480 { return __pos % _GLIBCXX_BITSET_BITS_PER_WORD; }
483 _S_maskbit(size_t __pos )
484 { return (static_cast<_WordT>(1)) << _S_whichbit(__pos); }
486 // This would normally give access to the data. The bounds-checking
487 // in the bitset class will prevent the user from getting this far,
488 // but (1) it must still return an lvalue to compile, and (2) the
489 // user might call _Unchecked_set directly, in which case this /needs/
490 // to fail. Let's not penalize zero-length users unless they actually
491 // make an unchecked call; all the memory ugliness is therefore
492 // localized to this single should-never-get-this-far function.
494 _M_getword(size_t) const
496 __throw_out_of_range(__N("_Base_bitset::_M_getword"));
505 _M_do_and(const _Base_bitset<0>&)
509 _M_do_or(const _Base_bitset<0>&)
513 _M_do_xor(const _Base_bitset<0>&)
517 _M_do_left_shift(size_t)
521 _M_do_right_shift(size_t)
536 // Are all empty bitsets equal to each other? Are they equal to
537 // themselves? How to compare a thing which has no state? What is
538 // the sound of one zero-length bitset clapping?
540 _M_is_equal(const _Base_bitset<0>&) const
552 _M_do_to_ulong() const
555 // Normally "not found" is the size, but that could also be
556 // misinterpreted as an index in this corner case. Oh well.
558 _M_do_find_first(size_t) const
562 _M_do_find_next(size_t, size_t) const
567 // Helper class to zero out the unused high-order bits in the highest word.
568 template<size_t _Extrabits>
571 static void _S_do_sanitize(unsigned long& __val)
572 { __val &= ~((~static_cast<unsigned long>(0)) << _Extrabits); }
577 { static void _S_do_sanitize(unsigned long) {} };
580 * @brief The %bitset class represents a @e fixed-size sequence of bits.
582 * @ingroup Containers
584 * (Note that %bitset does @e not meet the formal requirements of a
585 * <a href="tables.html#65">container</a>. Mainly, it lacks iterators.)
587 * The template argument, @a Nb, may be any non-negative number,
588 * specifying the number of bits (e.g., "0", "12", "1024*1024").
590 * In the general unoptimized case, storage is allocated in word-sized
591 * blocks. Let B be the number of bits in a word, then (Nb+(B-1))/B
592 * words will be used for storage. B - Nb%B bits are unused. (They are
593 * the high-order bits in the highest word.) It is a class invariant
594 * that those unused bits are always zero.
596 * If you think of %bitset as "a simple array of bits," be aware that
597 * your mental picture is reversed: a %bitset behaves the same way as
598 * bits in integers do, with the bit at index 0 in the "least significant
599 * / right-hand" position, and the bit at index Nb-1 in the "most
600 * significant / left-hand" position. Thus, unlike other containers, a
601 * %bitset's index "counts from right to left," to put it very loosely.
603 * This behavior is preserved when translating to and from strings. For
604 * example, the first line of the following program probably prints
605 * "b('a') is 0001100001" on a modern ASCII system.
609 * #include <iostream>
612 * using namespace std;
619 * cout << "b('a') is " << b << endl;
623 * string str = s.str();
624 * cout << "index 3 in the string is " << str[3] << " but\n"
625 * << "index 3 in the bitset is " << b[3] << endl;
629 * Also see http://gcc.gnu.org/onlinedocs/libstdc++/ext/sgiexts.html#ch23
630 * for a description of extensions.
633 * Most of the actual code isn't contained in %bitset<> itself, but in the
634 * base class _Base_bitset. The base class works with whole words, not with
635 * individual bits. This allows us to specialize _Base_bitset for the
636 * important special case where the %bitset is only a single word.
638 * Extra confusion can result due to the fact that the storage for
639 * _Base_bitset @e is a regular array, and is indexed as such. This is
640 * carefully encapsulated.
645 : private _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)>
648 typedef _Base_bitset<_GLIBCXX_BITSET_WORDS(_Nb)> _Base;
649 typedef unsigned long _WordT;
654 _Sanitize<_Nb % _GLIBCXX_BITSET_BITS_PER_WORD>::
655 _S_do_sanitize(this->_M_hiword());
660 * This encapsulates the concept of a single bit. An instance of this
661 * class is a proxy for an actual bit; this way the individual bit
662 * operations are done as faster word-size bitwise instructions.
664 * Most users will never need to use this class directly; conversions
665 * to and from bool are automatic and should be transparent. Overloaded
666 * operators help to preserve the illusion.
668 * (On a typical system, this "bit %reference" is 64 times the size of
669 * an actual bit. Ha.)
682 reference(bitset& __b, size_t __pos)
684 _M_wp = &__b._M_getword(__pos);
685 _M_bpos = _Base::_S_whichbit(__pos);
696 *_M_wp |= _Base::_S_maskbit(_M_bpos);
698 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
702 // For b[i] = b[__j];
704 operator=(const reference& __j)
706 if ((*(__j._M_wp) & _Base::_S_maskbit(__j._M_bpos)))
707 *_M_wp |= _Base::_S_maskbit(_M_bpos);
709 *_M_wp &= ~_Base::_S_maskbit(_M_bpos);
716 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) == 0; }
719 operator bool() const
720 { return (*(_M_wp) & _Base::_S_maskbit(_M_bpos)) != 0; }
726 *_M_wp ^= _Base::_S_maskbit(_M_bpos);
730 friend class reference;
732 // 23.3.5.1 constructors:
733 /// All bits set to zero.
737 /// Initial bits bitwise-copied from a single word (others set to zero).
738 bitset(unsigned long __val)
740 { _M_do_sanitize(); }
743 * @brief Use a subset of a string.
744 * @param s A string of '0' and '1' characters.
745 * @param position Index of the first character in @a s to use;
747 * @throw std::out_of_range If @a pos is bigger the size of @a s.
748 * @throw std::invalid_argument If a character appears in the string
749 * which is neither '0' nor '1'.
751 template<class _CharT, class _Traits, class _Alloc>
753 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
754 size_t __position = 0)
757 if (__position > __s.size())
758 __throw_out_of_range(__N("bitset::bitset initial position "
760 _M_copy_from_string(__s, __position,
761 std::basic_string<_CharT, _Traits, _Alloc>::npos);
765 * @brief Use a subset of a string.
766 * @param s A string of '0' and '1' characters.
767 * @param position Index of the first character in @a s to use.
768 * @param n The number of characters to copy.
769 * @throw std::out_of_range If @a pos is bigger the size of @a s.
770 * @throw std::invalid_argument If a character appears in the string
771 * which is neither '0' nor '1'.
773 template<class _CharT, class _Traits, class _Alloc>
774 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __s,
775 size_t __position, size_t __n)
778 if (__position > __s.size())
779 __throw_out_of_range(__N("bitset::bitset initial position "
781 _M_copy_from_string(__s, __position, __n);
784 // 23.3.5.2 bitset operations:
787 * @brief Operations on bitsets.
788 * @param rhs A same-sized bitset.
790 * These should be self-explanatory.
793 operator&=(const bitset<_Nb>& __rhs)
795 this->_M_do_and(__rhs);
800 operator|=(const bitset<_Nb>& __rhs)
802 this->_M_do_or(__rhs);
807 operator^=(const bitset<_Nb>& __rhs)
809 this->_M_do_xor(__rhs);
816 * @brief Operations on bitsets.
817 * @param position The number of places to shift.
819 * These should be self-explanatory.
822 operator<<=(size_t __position)
824 if (__builtin_expect(__position < _Nb, 1))
826 this->_M_do_left_shift(__position);
827 this->_M_do_sanitize();
835 operator>>=(size_t __position)
837 if (__builtin_expect(__position < _Nb, 1))
839 this->_M_do_right_shift(__position);
840 this->_M_do_sanitize();
850 * These versions of single-bit set, reset, flip, and test are
851 * extensions from the SGI version. They do no range checking.
852 * @ingroup SGIextensions
855 _Unchecked_set(size_t __pos)
857 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
862 _Unchecked_set(size_t __pos, int __val)
865 this->_M_getword(__pos) |= _Base::_S_maskbit(__pos);
867 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
872 _Unchecked_reset(size_t __pos)
874 this->_M_getword(__pos) &= ~_Base::_S_maskbit(__pos);
879 _Unchecked_flip(size_t __pos)
881 this->_M_getword(__pos) ^= _Base::_S_maskbit(__pos);
886 _Unchecked_test(size_t __pos) const
887 { return ((this->_M_getword(__pos) & _Base::_S_maskbit(__pos))
888 != static_cast<_WordT>(0)); }
891 // Set, reset, and flip.
893 * @brief Sets every bit to true.
899 this->_M_do_sanitize();
904 * @brief Sets a given bit to a particular value.
905 * @param position The index of the bit.
906 * @param val Either true or false, defaults to true.
907 * @throw std::out_of_range If @a pos is bigger the size of the %set.
910 set(size_t __position, bool __val = true)
912 if (__position >= _Nb)
913 __throw_out_of_range(__N("bitset::set"));
914 return _Unchecked_set(__position, __val);
918 * @brief Sets every bit to false.
928 * @brief Sets a given bit to false.
929 * @param position The index of the bit.
930 * @throw std::out_of_range If @a pos is bigger the size of the %set.
932 * Same as writing @c set(pos,false).
935 reset(size_t __position)
937 if (__position >= _Nb)
938 __throw_out_of_range(__N("bitset::reset"));
939 return _Unchecked_reset(__position);
943 * @brief Toggles every bit to its opposite value.
949 this->_M_do_sanitize();
954 * @brief Toggles a given bit to its opposite value.
955 * @param position The index of the bit.
956 * @throw std::out_of_range If @a pos is bigger the size of the %set.
959 flip(size_t __position)
961 if (__position >= _Nb)
962 __throw_out_of_range(__N("bitset::flip"));
963 return _Unchecked_flip(__position);
966 /// See the no-argument flip().
969 { return bitset<_Nb>(*this).flip(); }
973 * @brief Array-indexing support.
974 * @param position Index into the %bitset.
975 * @return A bool for a 'const %bitset'. For non-const bitsets, an
976 * instance of the reference proxy class.
977 * @note These operators do no range checking and throw no exceptions,
978 * as required by DR 11 to the standard.
981 * _GLIBCXX_RESOLVE_LIB_DEFECTS Note that this implementation already
982 * resolves DR 11 (items 1 and 2), but does not do the range-checking
983 * required by that DR's resolution. -pme
984 * The DR has since been changed: range-checking is a precondition
985 * (users' responsibility), and these functions must not throw. -pme
989 operator[](size_t __position)
990 { return reference(*this,__position); }
993 operator[](size_t __position) const
994 { return _Unchecked_test(__position); }
998 * @brief Retuns a numerical interpretation of the %bitset.
999 * @return The integral equivalent of the bits.
1000 * @throw std::overflow_error If there are too many bits to be
1001 * represented in an @c unsigned @c long.
1005 { return this->_M_do_to_ulong(); }
1008 * @brief Retuns a character interpretation of the %bitset.
1009 * @return The string equivalent of the bits.
1011 * Note the ordering of the bits: decreasing character positions
1012 * correspond to increasing bit positions (see the main class notes for
1015 template<class _CharT, class _Traits, class _Alloc>
1016 std::basic_string<_CharT, _Traits, _Alloc>
1019 std::basic_string<_CharT, _Traits, _Alloc> __result;
1020 _M_copy_to_string(__result);
1024 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1025 // 434. bitset::to_string() hard to use.
1026 template<class _CharT, class _Traits>
1027 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
1029 { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
1031 template<class _CharT>
1032 std::basic_string<_CharT, std::char_traits<_CharT>,
1033 std::allocator<_CharT> >
1036 return to_string<_CharT, std::char_traits<_CharT>,
1037 std::allocator<_CharT> >();
1040 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
1043 return to_string<char, std::char_traits<char>,
1044 std::allocator<char> >();
1047 // Helper functions for string operations.
1048 template<class _CharT, class _Traits, class _Alloc>
1050 _M_copy_from_string(const std::basic_string<_CharT,
1051 _Traits, _Alloc>& __s,
1054 template<class _CharT, class _Traits, class _Alloc>
1056 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>&) const;
1058 /// Returns the number of bits which are set.
1061 { return this->_M_do_count(); }
1063 /// Returns the total number of bits.
1069 /// These comparisons for equality/inequality are, well, @e bitwise.
1071 operator==(const bitset<_Nb>& __rhs) const
1072 { return this->_M_is_equal(__rhs); }
1075 operator!=(const bitset<_Nb>& __rhs) const
1076 { return !this->_M_is_equal(__rhs); }
1080 * @brief Tests the value of a bit.
1081 * @param position The index of a bit.
1082 * @return The value at @a pos.
1083 * @throw std::out_of_range If @a pos is bigger the size of the %set.
1086 test(size_t __position) const
1088 if (__position >= _Nb)
1089 __throw_out_of_range(__N("bitset::test"));
1090 return _Unchecked_test(__position);
1094 * @brief Tests whether any of the bits are on.
1095 * @return True if at least one bit is set.
1099 { return this->_M_is_any(); }
1102 * @brief Tests whether any of the bits are on.
1103 * @return True if none of the bits are set.
1107 { return !this->_M_is_any(); }
1110 /// Self-explanatory.
1112 operator<<(size_t __position) const
1113 { return bitset<_Nb>(*this) <<= __position; }
1116 operator>>(size_t __position) const
1117 { return bitset<_Nb>(*this) >>= __position; }
1121 * @brief Finds the index of the first "on" bit.
1122 * @return The index of the first bit set, or size() if not found.
1123 * @ingroup SGIextensions
1128 { return this->_M_do_find_first(_Nb); }
1131 * @brief Finds the index of the next "on" bit after prev.
1132 * @return The index of the next bit set, or size() if not found.
1133 * @param prev Where to start searching.
1134 * @ingroup SGIextensions
1138 _Find_next(size_t __prev ) const
1139 { return this->_M_do_find_next(__prev, _Nb); }
1142 // Definitions of non-inline member functions.
1143 template<size_t _Nb>
1144 template<class _CharT, class _Traits, class _Alloc>
1147 _M_copy_from_string(const std::basic_string<_CharT, _Traits,
1148 _Alloc>& __s, size_t __pos, size_t __n)
1151 const size_t __nbits = std::min(_Nb, std::min(__n, __s.size() - __pos));
1152 for (size_t __i = __nbits; __i > 0; --__i)
1154 switch(__s[__pos + __nbits - __i])
1159 _Unchecked_set(__i - 1);
1162 __throw_invalid_argument(__N("bitset::_M_copy_from_string"));
1167 template<size_t _Nb>
1168 template<class _CharT, class _Traits, class _Alloc>
1171 _M_copy_to_string(std::basic_string<_CharT, _Traits, _Alloc>& __s) const
1173 __s.assign(_Nb, '0');
1174 for (size_t __i = _Nb; __i > 0; --__i)
1175 if (_Unchecked_test(__i - 1))
1176 __s[_Nb - __i] = '1';
1179 // 23.3.5.3 bitset operations:
1182 * @brief Global bitwise operations on bitsets.
1183 * @param x A bitset.
1184 * @param y A bitset of the same size as @a x.
1185 * @return A new bitset.
1187 * These should be self-explanatory.
1189 template<size_t _Nb>
1191 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1193 bitset<_Nb> __result(__x);
1198 template<size_t _Nb>
1200 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1202 bitset<_Nb> __result(__x);
1207 template <size_t _Nb>
1209 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
1211 bitset<_Nb> __result(__x);
1219 * @brief Global I/O operators for bitsets.
1221 * Direct I/O between streams and bitsets is supported. Output is
1222 * straightforward. Input will skip whitespace, only accept '0' and '1'
1223 * characters, and will only extract as many digits as the %bitset will
1226 template<class _CharT, class _Traits, size_t _Nb>
1227 std::basic_istream<_CharT, _Traits>&
1228 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
1230 typedef typename _Traits::char_type char_type;
1231 typedef std::basic_istream<_CharT, _Traits> __istream_type;
1232 typedef typename __istream_type::ios_base __ios_base;
1234 std::basic_string<_CharT, _Traits> __tmp;
1237 typename __ios_base::iostate __state = __ios_base::goodbit;
1238 typename __istream_type::sentry __sentry(__is);
1243 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1244 // 303. Bitset input operator underspecified
1245 const char_type __zero = __is.widen('0');
1246 const char_type __one = __is.widen('1');
1247 for (size_t __i = _Nb; __i > 0; --__i)
1249 static typename _Traits::int_type __eof = _Traits::eof();
1251 typename _Traits::int_type __c1 = __is.rdbuf()->sbumpc();
1252 if (_Traits::eq_int_type(__c1, __eof))
1254 __state |= __ios_base::eofbit;
1259 const char_type __c2 = _Traits::to_char_type(__c1);
1261 __tmp.push_back('0');
1262 else if (__c2 == __one)
1263 __tmp.push_back('1');
1265 eq_int_type(__is.rdbuf()->sputbackc(__c2),
1268 __state |= __ios_base::failbit;
1275 { __is._M_setstate(__ios_base::badbit); }
1278 if (__tmp.empty() && _Nb)
1279 __state |= __ios_base::failbit;
1281 __x._M_copy_from_string(__tmp, static_cast<size_t>(0), _Nb);
1283 __is.setstate(__state);
1287 template <class _CharT, class _Traits, size_t _Nb>
1288 std::basic_ostream<_CharT, _Traits>&
1289 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1290 const bitset<_Nb>& __x)
1292 std::basic_string<_CharT, _Traits> __tmp;
1293 __x._M_copy_to_string(__tmp);
1294 return __os << __tmp;
1297 // Specializations for zero-sized bitsets, to avoid "unsigned comparison
1298 // with zero" warnings.
1304 __throw_out_of_range(__N("bitset::set"));
1313 __throw_out_of_range(__N("bitset::reset"));
1322 __throw_out_of_range(__N("bitset::flip"));
1331 __throw_out_of_range(__N("bitset::test"));
1336 _GLIBCXX_END_NESTED_NAMESPACE
1338 #undef _GLIBCXX_BITSET_WORDS
1339 #undef _GLIBCXX_BITSET_BITS_PER_WORD
1341 #ifdef _GLIBCXX_DEBUG
1342 # include <debug/bitset>
1345 #endif /* _GLIBCXX_BITSET */