Talk briefly about C++ header series of bitset

  • 2020-05-12 02:56:12
  • OfStack

Introduction to the

This header file is about bit sets and is actually vector

position

The bit essentially corresponds to the concept of bool, with only the opposite values of 0 or 1, true or false. Unfortunately, the byte is the smallest memory unit on the machine, so bool is basically 1 byte in size.

bitset was created for efficient use of space.

Bit operation

operator [] : access bit by subscript.
count: the number of bits with a counting bit value of 1.
size: returns the size of the bits, that is, the number of bits.
test: tests whether the bit value of the subscript is 1.
any: determines if any of the 1 bit values are 1.
none: determines if there is no 1 bit value of 1.
all: determines if all bit values are 1.
set: sets a bit value to 1.
reset: reset 1 bit value to 0.
flip: flipping 1 bit value, i.e. 0 to 1, 1 to 0.

The transition function

to_string: converts to a string.
to_ulong: convert to unsigned long.
to_ullong: convert to unsigned long long.

A set of operations

Here, bitset can be viewed as an integer in bits 01. For integers, we can perform many operations: or, and, or, move left, and so on. The header file overloads these operators so that we can operate on the bit set:

&, & =
| | =
^ ^ =
< < , < < =
> > , > > =
~
==
!=

reference type

Since the smallest built-in type size of C++ is 1 byte, bit conceptually only needs 1 bit, so, with vector


Related articles: