This article is the reading notes of "C++ Primer"
table of Contents
#include <bitset>
The standard library defines bitset
classes 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
bitset
The binary bits in the can be accessed by position . For example, itbitvec
contains 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
bitset
Similarly 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 long
initialization
When we use an integer value to initialize bitset
, this value will be converted to a unsigned long long
type and treated as a bit pattern. bitset
The binary bits in will be a copy of this pattern. If bitset
the size of is greater than one unsigned long long
of the binary digits, the remaining high bits are set to 0. If bitset
the size is less than the unsigned long long
number of binary digits in one, bitset
the 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
string
Initialize from a / character arraybitset
We can string
initialize 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 string
the number of characters contained is bitset
less, bitset
the high bit is set to 0.
Note:
string
The subscript numbering convention isbitset
just the opposite: thestring
digit with the largest middle subscript (the rightmost character) is used to initializebittset
the 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); //使用最后四个字符
bitset
operating
bitset
Operation defines a variety of methods for detecting or setting one or more binary bits. bitset
The class also supports bitwise operators
The subscript operator const
has overloaded attributes. const
The subscript operator of the version returns when the bit is specified true
, otherwise it returns false
. Non- const
version returns bitset
a 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! 类型
bitset
The input operator reads characters from an input stream and saves them in a temporary string
object. The bitset
reading 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 string
object to initialize bitset
. If the number of characters read is less than bitset
the size, as usual, the high bit will be set to 0