C++ Standard Library (8): bitset type

This article is the reading notes of "C++ Primer"

#include <bitset>

The standard library defines bitsetclasses to make the use of bit operations easier, and can handle bit sets that exceed the size of the longest integer type

Definition and initializationbitset

bitsetThe binary bits in the can be accessed by position . For example, it bitveccontains 32 binary digits numbered from 0 to 31. Numbered from 0 start bit is referred to low (low-order), the number of 31 bits is referred to as the end of the upper (high-order)

Default initialization

Insert picture description here

bitsetSimilarly array, with a fixed size , the size must be a constant expression . When we define one bitset, we need to declare how many binary bits it contains:

bitset<32> bitvec; // 32位;全部位均为0

Use unsigned long longinitialization

Insert picture description here

When we use an integer value to initialize bitset, this value will be converted to a unsigned long longtype and treated as a bit pattern. bitsetThe binary bits in will be a copy of this pattern. If bitsetthe size of is greater than one unsigned long longof the binary digits, the remaining high bits are set to 0. If bitsetthe size is less than the unsigned long longnumber of binary digits in one, bitsetthe higher bits that exceed the size are discarded:

// bitvecl比初始值小;初始值中的高位被丢弃
bitset<13> bitvecl(Oxbeef); //二进制位序列为1111011101111
// bitvec2比初始值大;它的高位置0
bitset<20> bitvec2(Oxbeef); //二进制位序列为00001011111011101l ll
// 在64位机器中,long long OULL是64个0比特, 因此~OULL是64个1
bitset<l28> bitvec3(~OULL); // 0~63位为l; 63~127位为0

stringInitialize from a / character arraybitset

Insert picture description here

We can stringinitialize from a pointer or a character array pointer bitset. In both cases, the characters directly represent the bit pattern. The character with the smallest subscript in the string corresponds to the high bit:

bitset<32> bitvec4("1100"); // 2 、3两位为1, 剩余两位为0

If stringthe number of characters contained is bitsetless, bitsetthe high bit is set to 0.

Note: stringThe subscript numbering convention is bitsetjust the opposite: the stringdigit with the largest middle subscript (the rightmost character) is used to initialize bittsetthe lower bit (the binary bit with subscript 0)

We can just use a substring as the initial value:

string str("lllllll000000011001101");
bitset<32> bitvec5(str, 5, 4); //从str[5]开始的四个二进制位,1100
bitset<32> bitvec6(str, str.size()-4); //使用最后四个字符

bitsetoperating

bitsetOperation defines a variety of methods for detecting or setting one or more binary bits. bitsetThe class also supports bitwise operators

Insert picture description here
The subscript operator consthas overloaded attributes. constThe subscript operator of the version returns when the bit is specified true, otherwise it returns false. Non- constversion returns bitseta special type defined, which allows us to manipulate the value of the specified bit:

bitvec[0] = 0; // 将第一位复位
bitvec[31] = bitvec[0]; // 将最后一位设置为与笫一位一样
bitvec[0].flip(); // 翻转笫一位
~bitvec[0]; // 等价操作, 也是翻转笫一位
bool b = bitvec[0]; // 将bitvec[0]的值转换为boo! 类型

bitsetThe input operator reads characters from an input stream and saves them in a temporary stringobject. The bitsetreading process will not stop until the number of characters read reaches the corresponding size, or when characters other than 1 or 0 are encountered, or when the end of the file or input errors are encountered. Then use the temporary stringobject to initialize bitset. If the number of characters read is less than bitsetthe size, as usual, the high bit will be set to 0

Guess you like

Origin blog.csdn.net/weixin_42437114/article/details/109270628