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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ## Bitsets <a id="bitset">[[bitset]]</a>
2
+
3
+ ### Header `<bitset>` synopsis <a id="bitset.syn">[[bitset.syn]]</a>
4
+
5
+ ``` cpp
6
+ #include <string>
7
+ #include <iosfwd> // for istream ([istream.syn]), ostream ([ostream.syn]), see [iosfwd.syn]
8
+
9
+ namespace std {
10
+ template <size_t N> class bitset;
11
+
12
+ // [bitset.operators], bitset operators
13
+ template <size_t N>
14
+ bitset<N> operator&(const bitset<N>&, const bitset<N>&) noexcept;
15
+ template <size_t N>
16
+ bitset<N> operator|(const bitset<N>&, const bitset<N>&) noexcept;
17
+ template <size_t N>
18
+ bitset<N> operator^(const bitset<N>&, const bitset<N>&) noexcept;
19
+ template <class charT, class traits, size_t N>
20
+ basic_istream<charT, traits>&
21
+ operator>>(basic_istream<charT, traits>& is, bitset<N>& x);
22
+ template <class charT, class traits, size_t N>
23
+ basic_ostream<charT, traits>&
24
+ operator<<(basic_ostream<charT, traits>& os, const bitset<N>& x);
25
+ }
26
+ ```
27
+
28
+ The header `<bitset>` defines a class template and several related
29
+ functions for representing and manipulating fixed-size sequences of
30
+ bits.
31
+
32
+ ### Class template `bitset` <a id="template.bitset">[[template.bitset]]</a>
33
+
34
+ ``` cpp
35
+ namespace std {
36
+ template<size_t N> class bitset {
37
+ public:
38
+ // bit reference:
39
+ class reference {
40
+ friend class bitset;
41
+ reference() noexcept;
42
+ public:
43
+ ~reference() noexcept;
44
+ reference& operator=(bool x) noexcept; // for b[i] = x;
45
+ reference& operator=(const reference&) noexcept; // for b[i] = b[j];
46
+ bool operator~() const noexcept; // flips the bit
47
+ operator bool() const noexcept; // for x = b[i];
48
+ reference& flip() noexcept; // for b[i].flip();
49
+ };
50
+
51
+ // [bitset.cons], constructors
52
+ constexpr bitset() noexcept;
53
+ constexpr bitset(unsigned long long val) noexcept;
54
+ template<class charT, class traits, class Allocator>
55
+ explicit bitset(
56
+ const basic_string<charT, traits, Allocator>& str,
57
+ typename basic_string<charT, traits, Allocator>::size_type pos = 0,
58
+ typename basic_string<charT, traits, Allocator>::size_type n =
59
+ basic_string<charT, traits, Allocator>::npos,
60
+ charT zero = charT('0'),
61
+ charT one = charT('1'));
62
+ template <class charT>
63
+ explicit bitset(
64
+ const charT* str,
65
+ typename basic_string<charT>::size_type n = basic_string<charT>::npos,
66
+ charT zero = charT('0'),
67
+ charT one = charT('1'));
68
+
69
+ // [bitset.members], bitset operations
70
+ bitset<N>& operator&=(const bitset<N>& rhs) noexcept;
71
+ bitset<N>& operator|=(const bitset<N>& rhs) noexcept;
72
+ bitset<N>& operator^=(const bitset<N>& rhs) noexcept;
73
+ bitset<N>& operator<<=(size_t pos) noexcept;
74
+ bitset<N>& operator>>=(size_t pos) noexcept;
75
+ bitset<N>& set() noexcept;
76
+ bitset<N>& set(size_t pos, bool val = true);
77
+ bitset<N>& reset() noexcept;
78
+ bitset<N>& reset(size_t pos);
79
+ bitset<N> operator~() const noexcept;
80
+ bitset<N>& flip() noexcept;
81
+ bitset<N>& flip(size_t pos);
82
+
83
+ // element access:
84
+ constexpr bool operator[](size_t pos) const; // for b[i];
85
+ reference operator[](size_t pos); // for b[i];
86
+
87
+ unsigned long to_ulong() const;
88
+ unsigned long long to_ullong() const;
89
+ template <class charT = char,
90
+ class traits = char_traits<charT>,
91
+ class Allocator = allocator<charT>>
92
+ basic_string<charT, traits, Allocator>
93
+ to_string(charT zero = charT('0'), charT one = charT('1')) const;
94
+
95
+ size_t count() const noexcept;
96
+ constexpr size_t size() const noexcept;
97
+ bool operator==(const bitset<N>& rhs) const noexcept;
98
+ bool operator!=(const bitset<N>& rhs) const noexcept;
99
+ bool test(size_t pos) const;
100
+ bool all() const noexcept;
101
+ bool any() const noexcept;
102
+ bool none() const noexcept;
103
+ bitset<N> operator<<(size_t pos) const noexcept;
104
+ bitset<N> operator>>(size_t pos) const noexcept;
105
+ };
106
+
107
+ // [bitset.hash], hash support
108
+ template <class T> struct hash;
109
+ template <size_t N> struct hash<bitset<N>>;
110
+ }
111
+ ```
112
+
113
+ The class template `bitset<N>` describes an object that can store a
114
+ sequence consisting of a fixed number of bits, `N`.
115
+
116
+ Each bit represents either the value zero (reset) or one (set). To
117
+ *toggle* a bit is to change the value zero to one, or the value one to
118
+ zero. Each bit has a non-negative position `pos`. When converting
119
+ between an object of class `bitset<N>` and a value of some integral
120
+ type, bit position `pos` corresponds to the *bit value* `1 << pos`. The
121
+ integral value corresponding to two or more bits is the sum of their bit
122
+ values.
123
+
124
+ The functions described in this subclause can report three kinds of
125
+ errors, each associated with a distinct exception:
126
+
127
+ - an *invalid-argument* error is associated with exceptions of type
128
+ `invalid_argument` ([[invalid.argument]]);
129
+ - an *out-of-range* error is associated with exceptions of type
130
+ `out_of_range` ([[out.of.range]]);
131
+ - an *overflow* error is associated with exceptions of type
132
+ `overflow_error` ([[overflow.error]]).
133
+
134
+ #### `bitset` constructors <a id="bitset.cons">[[bitset.cons]]</a>
135
+
136
+ ``` cpp
137
+ constexpr bitset() noexcept;
138
+ ```
139
+
140
+ *Effects:* Constructs an object of class `bitset<N>`, initializing all
141
+ bits to zero.
142
+
143
+ ``` cpp
144
+ constexpr bitset(unsigned long long val) noexcept;
145
+ ```
146
+
147
+ *Effects:* Constructs an object of class `bitset<N>`, initializing the
148
+ first `M` bit positions to the corresponding bit values in `val`. `M` is
149
+ the smaller of `N` and the number of bits in the value
150
+ representation ([[basic.types]]) of `unsigned long long`. If `M < N`,
151
+ the remaining bit positions are initialized to zero.
152
+
153
+ ``` cpp
154
+ template <class charT, class traits, class Allocator>
155
+ explicit
156
+ bitset(const basic_string<charT, traits, Allocator>& str,
157
+ typename basic_string<charT, traits, Allocator>::size_type pos = 0,
158
+ typename basic_string<charT, traits, Allocator>::size_type n =
159
+ basic_string<charT, traits, Allocator>::npos,
160
+ charT zero = charT('0'), charT one = charT('1'));
161
+ ```
162
+
163
+ *Throws:* `out_of_range` if `pos > str.size()` or `invalid_argument` if
164
+ an invalid character is found (see below).
165
+
166
+ *Effects:* Determines the effective length `rlen` of the initializing
167
+ string as the smaller of `n` and `str.size() - pos`.
168
+
169
+ The function then throws
170
+
171
+ `invalid_argument` if any of the `rlen` characters in `str` beginning at
172
+ position `pos` is other than `zero` or `one`. The function uses
173
+ `traits::eq()` to compare the character values.
174
+
175
+ Otherwise, the function constructs an object of class `bitset<N>`,
176
+ initializing the first `M` bit positions to values determined from the
177
+ corresponding characters in the string `str`. `M` is the smaller of `N`
178
+ and `rlen`.
179
+
180
+ An element of the constructed object has value zero if the corresponding
181
+ character in `str`, beginning at position `pos`, is `zero`. Otherwise,
182
+ the element has the value one. Character position `pos + M - 1`
183
+ corresponds to bit position zero. Subsequent decreasing character
184
+ positions correspond to increasing bit positions.
185
+
186
+ If `M < N`, remaining bit positions are initialized to zero.
187
+
188
+ ``` cpp
189
+ template <class charT>
190
+ explicit bitset(
191
+ const charT* str,
192
+ typename basic_string<charT>::size_type n = basic_string<charT>::npos,
193
+ charT zero = charT('0'), charT one = charT('1'));
194
+ ```
195
+
196
+ *Effects:* Constructs an object of class `bitset<N>` as if by:
197
+
198
+ ``` cpp
199
+ bitset(
200
+ n == basic_string<charT>::npos
201
+ ? basic_string<charT>(str)
202
+ : basic_string<charT>(str, n),
203
+ 0, n, zero, one)
204
+ ```
205
+
206
+ #### `bitset` members <a id="bitset.members">[[bitset.members]]</a>
207
+
208
+ ``` cpp
209
+ bitset<N>& operator&=(const bitset<N>& rhs) noexcept;
210
+ ```
211
+
212
+ *Effects:* Clears each bit in `*this` for which the corresponding bit in
213
+ `rhs` is clear, and leaves all other bits unchanged.
214
+
215
+ *Returns:* `*this`.
216
+
217
+ ``` cpp
218
+ bitset<N>& operator|=(const bitset<N>& rhs) noexcept;
219
+ ```
220
+
221
+ *Effects:* Sets each bit in `*this` for which the corresponding bit in
222
+ `rhs` is set, and leaves all other bits unchanged.
223
+
224
+ *Returns:* `*this`.
225
+
226
+ ``` cpp
227
+ bitset<N>& operator^=(const bitset<N>& rhs) noexcept;
228
+ ```
229
+
230
+ *Effects:* Toggles each bit in `*this` for which the corresponding bit
231
+ in `rhs` is set, and leaves all other bits unchanged.
232
+
233
+ *Returns:* `*this`.
234
+
235
+ ``` cpp
236
+ bitset<N>& operator<<=(size_t pos) noexcept;
237
+ ```
238
+
239
+ *Effects:* Replaces each bit at position `I` in `*this` with a value
240
+ determined as follows:
241
+
242
+ - If `I < pos`, the new value is zero;
243
+ - If `I >= pos`, the new value is the previous value of the bit at
244
+ position `I - pos`.
245
+
246
+ *Returns:* `*this`.
247
+
248
+ ``` cpp
249
+ bitset<N>& operator>>=(size_t pos) noexcept;
250
+ ```
251
+
252
+ *Effects:* Replaces each bit at position `I` in `*this` with a value
253
+ determined as follows:
254
+
255
+ - If `pos >= N - I`, the new value is zero;
256
+ - If `pos < N - I`, the new value is the previous value of the bit at
257
+ position `I + pos`.
258
+
259
+ *Returns:* `*this`.
260
+
261
+ ``` cpp
262
+ bitset<N>& set() noexcept;
263
+ ```
264
+
265
+ *Effects:* Sets all bits in `*this`.
266
+
267
+ *Returns:* `*this`.
268
+
269
+ ``` cpp
270
+ bitset<N>& set(size_t pos, bool val = true);
271
+ ```
272
+
273
+ *Throws:* `out_of_range` if `pos` does not correspond to a valid bit
274
+ position.
275
+
276
+ *Effects:* Stores a new value in the bit at position `pos` in `*this`.
277
+ If `val` is nonzero, the stored value is one, otherwise it is zero.
278
+
279
+ *Returns:* `*this`.
280
+
281
+ ``` cpp
282
+ bitset<N>& reset() noexcept;
283
+ ```
284
+
285
+ *Effects:* Resets all bits in `*this`.
286
+
287
+ *Returns:* `*this`.
288
+
289
+ ``` cpp
290
+ bitset<N>& reset(size_t pos);
291
+ ```
292
+
293
+ *Throws:* `out_of_range` if `pos` does not correspond to a valid bit
294
+ position.
295
+
296
+ *Effects:* Resets the bit at position `pos` in `*this`.
297
+
298
+ *Returns:* `*this`.
299
+
300
+ ``` cpp
301
+ bitset<N> operator~() const noexcept;
302
+ ```
303
+
304
+ *Effects:* Constructs an object `x` of class `bitset<N>` and initializes
305
+ it with `*this`.
306
+
307
+ *Returns:* `x.flip()`.
308
+
309
+ ``` cpp
310
+ bitset<N>& flip() noexcept;
311
+ ```
312
+
313
+ *Effects:* Toggles all bits in `*this`.
314
+
315
+ *Returns:* `*this`.
316
+
317
+ ``` cpp
318
+ bitset<N>& flip(size_t pos);
319
+ ```
320
+
321
+ *Throws:* `out_of_range` if `pos` does not correspond to a valid bit
322
+ position.
323
+
324
+ *Effects:* Toggles the bit at position `pos` in `*this`.
325
+
326
+ *Returns:* `*this`.
327
+
328
+ ``` cpp
329
+ unsigned long to_ulong() const;
330
+ ```
331
+
332
+ *Throws:* `overflow_error`
333
+
334
+ if the integral value `x` corresponding to the bits in `*this` cannot be
335
+ represented as type `unsigned long`.
336
+
337
+ *Returns:* `x`.
338
+
339
+ ``` cpp
340
+ unsigned long long to_ullong() const;
341
+ ```
342
+
343
+ *Throws:* `overflow_error` if the integral value `x` corresponding to
344
+ the bits in `*this` cannot be represented as type `unsigned long long`.
345
+
346
+ *Returns:* `x`.
347
+
348
+ ``` cpp
349
+ template <class charT = char,
350
+ class traits = char_traits<charT>,
351
+ class Allocator = allocator<charT>>
352
+ basic_string<charT, traits, Allocator>
353
+ to_string(charT zero = charT('0'), charT one = charT('1')) const;
354
+ ```
355
+
356
+ *Effects:* Constructs a string object of the appropriate type and
357
+ initializes it to a string of length `N` characters. Each character is
358
+ determined by the value of its corresponding bit position in `*this`.
359
+ Character position `N - 1` corresponds to bit position zero. Subsequent
360
+ decreasing character positions correspond to increasing bit positions.
361
+ Bit value zero becomes the character `zero`, bit value one becomes the
362
+ character `one`.
363
+
364
+ *Returns:* The created object.
365
+
366
+ ``` cpp
367
+ size_t count() const noexcept;
368
+ ```
369
+
370
+ *Returns:* A count of the number of bits set in `*this`.
371
+
372
+ ``` cpp
373
+ constexpr size_t size() const noexcept;
374
+ ```
375
+
376
+ *Returns:* `N`.
377
+
378
+ ``` cpp
379
+ bool operator==(const bitset<N>& rhs) const noexcept;
380
+ ```
381
+
382
+ *Returns:* `true` if the value of each bit in `*this` equals the value
383
+ of the corresponding bit in `rhs`.
384
+
385
+ ``` cpp
386
+ bool operator!=(const bitset<N>& rhs) const noexcept;
387
+ ```
388
+
389
+ *Returns:* `true` if `!(*this == rhs)`.
390
+
391
+ ``` cpp
392
+ bool test(size_t pos) const;
393
+ ```
394
+
395
+ *Throws:* `out_of_range` if `pos` does not correspond to a valid bit
396
+ position.
397
+
398
+ *Returns:* `true` if the bit at position `pos` in `*this` has the value
399
+ one.
400
+
401
+ ``` cpp
402
+ bool all() const noexcept;
403
+ ```
404
+
405
+ *Returns:* `count() == size()`.
406
+
407
+ ``` cpp
408
+ bool any() const noexcept;
409
+ ```
410
+
411
+ *Returns:* `count() != 0`.
412
+
413
+ ``` cpp
414
+ bool none() const noexcept;
415
+ ```
416
+
417
+ *Returns:* `count() == 0`.
418
+
419
+ ``` cpp
420
+ bitset<N> operator<<(size_t pos) const noexcept;
421
+ ```
422
+
423
+ *Returns:* `bitset<N>(*this) <<= pos`.
424
+
425
+ ``` cpp
426
+ bitset<N> operator>>(size_t pos) const noexcept;
427
+ ```
428
+
429
+ *Returns:* `bitset<N>(*this) >>= pos`.
430
+
431
+ ``` cpp
432
+ constexpr bool operator[](size_t pos) const;
433
+ ```
434
+
435
+ *Requires:* `pos` shall be valid.
436
+
437
+ *Returns:* `true` if the bit at position `pos` in `*this` has the value
438
+ one, otherwise `false`.
439
+
440
+ *Throws:* Nothing.
441
+
442
+ ``` cpp
443
+ bitset<N>::reference operator[](size_t pos);
444
+ ```
445
+
446
+ *Requires:* `pos` shall be valid.
447
+
448
+ *Returns:* An object of type `bitset<N>::reference` such that
449
+ `(*this)[pos] == this->test(pos)`, and such that `(*this)[pos] = val` is
450
+ equivalent to `this->set(pos, val)`.
451
+
452
+ *Throws:* Nothing.
453
+
454
+ *Remarks:* For the purpose of determining the presence of a data
455
+ race ([[intro.multithread]]), any access or update through the
456
+ resulting reference potentially accesses or modifies, respectively, the
457
+ entire underlying bitset.
458
+
459
+ ### `bitset` hash support <a id="bitset.hash">[[bitset.hash]]</a>
460
+
461
+ ``` cpp
462
+ template <size_t N> struct hash<bitset<N>>;
463
+ ```
464
+
465
+ The specialization is enabled ([[unord.hash]]).
466
+
467
+ ### `bitset` operators <a id="bitset.operators">[[bitset.operators]]</a>
468
+
469
+ ``` cpp
470
+ bitset<N> operator&(const bitset<N>& lhs, const bitset<N>& rhs) noexcept;
471
+ ```
472
+
473
+ *Returns:* `bitset<N>(lhs) &= rhs`.
474
+
475
+ ``` cpp
476
+ bitset<N> operator|(const bitset<N>& lhs, const bitset<N>& rhs) noexcept;
477
+ ```
478
+
479
+ *Returns:* `bitset<N>(lhs) |= rhs`.
480
+
481
+ ``` cpp
482
+ bitset<N> operator^(const bitset<N>& lhs, const bitset<N>& rhs) noexcept;
483
+ ```
484
+
485
+ *Returns:* `bitset<N>(lhs) ^= rhs`.
486
+
487
+ ``` cpp
488
+ template <class charT, class traits, size_t N>
489
+ basic_istream<charT, traits>&
490
+ operator>>(basic_istream<charT, traits>& is, bitset<N>& x);
491
+ ```
492
+
493
+ A formatted input function ([[istream.formatted]]).
494
+
495
+ *Effects:* Extracts up to `N` characters from `is`. Stores these
496
+ characters in a temporary object `str` of type
497
+ `basic_string<charT, traits>`, then evaluates the expression
498
+ `x = bitset<N>(str)`. Characters are extracted and stored until any of
499
+ the following occurs:
500
+
501
+ - `N` characters have been extracted and stored;
502
+ - end-of-file occurs on the input sequence;
503
+ - the next input character is neither `is.widen(’0’)` nor
504
+ `is.widen(’1’)` (in which case the input character is not extracted).
505
+
506
+ If no characters are stored in `str`, calls
507
+ `is.setstate(ios_base::failbit)` (which may throw
508
+ `ios_base::failure` ([[iostate.flags]])).
509
+
510
+ *Returns:* `is`.
511
+
512
+ ``` cpp
513
+ template <class charT, class traits, size_t N>
514
+ basic_ostream<charT, traits>&
515
+ operator<<(basic_ostream<charT, traits>& os, const bitset<N>& x);
516
+ ```
517
+
518
+ *Returns:*
519
+
520
+ ``` cpp
521
+ os << x.template to_string<charT, traits, allocator<charT>>(
522
+ use_facet<ctype<charT>>(os.getloc()).widen('0'),
523
+ use_facet<ctype<charT>>(os.getloc()).widen('1'))
524
+ ```
525
+
526
+ (see  [[ostream.formatted]]).
527
+