C ++: bitset 56 --- the standard library special type of facility

A, bitset Overview

  • Bitset class library defines that bit operations easier to use , and capable of handling more than a maximum bit size of the set of integer type
  • bitset is defined in the header file in the bitset

Second, the definition and initialization bitset

  • The figure shows the construction method of the bitset
  • Low and High:
    • Binary set start position (index 0) is referred to low, referred to as subsequent high
    • Note, bitset index 0 starting from the far right
  • bitset class is a template, similar to the array type, has a fixed size , and the size must be a constant expression. When we define a bitset, we need to declare how many bits it contains:
std::bitset<32> bitvec(1U);  //U占32位。因此bitvec为32位;低位为1,其他位均为0

Unsigned values ​​with initialization bitset

  • When we use a time integral value to initialize the bitset , this value is converted to unsigned long long type and is treated as a bit pattern
  • Rules are as follows:
    • If the size of the bitset is greater than the number of bits in an unsigned long long, the remaining high-order bit is set to 0
    • If the size of the bitset smaller than the number of bits in an unsigned long long, using only a given status values, exceeds the size of the upper discarded bitset
  • E.g:
//0xbeef为:1011 1110 1110 1111

//bitvec1比初始值小。高位被丢弃
std::bitset<13> bitvec1(0xbeef);  //二进制位序列为1 1110 1110 1111

//bitvec1比初始值大。高位被置位0
std::bitset<20> bitvec2(0xbeef);  //进制位序列为0000 1011 1110 1110 1111

//在64位机器上,long long 0ULL是64位的,因此~0ULL是64个1
std::bitset<128> bitvec3(~0ULL);  //0~63位为1,63~127位为0

std::cout << "bitvec1: " << bitvec1 << std::endl;
std::cout << "bitvec2: " << bitvec2 << std::endl;
std::cout << "bitvec3: " << bitvec3 << std::endl;

 

Bitset from a string initialization

  • We can from a string or a character array pointer to initialize the bitset
  • Note (Key): When using the number represented by a string, the string corresponding to minimal high standard character, subscript character corresponding to the maximum low
  • E.g:
std::bitset<32> bitvec4("1100"); //1100
std::cout << "bitvec4 : " << bitvec4 << std::endl;

 

  • We can also use sub-string string to initialize the bitset. E.g:
std::string str("1111111000000011001101");
std::bitset<32> bitvec5(str, 5, 4);           //1100
std::bitset<32> bitvec6(str, str.size() - 4); //1101

std::cout << "bitvec5 : " << bitvec5 << std::endl;
std::cout << "bitvec6 : " << bitvec6 << std::endl;

 

Three, bitset operations

  • Operation roughly divided into two categories:
    • A kind of return information bitset. For example count, size, all, any, none, etc.
    • Another class to set the bitset. For example, set, reset, flip. These functions are overloaded
  • E.g:
std::bitset<32> bitVec(1U);      //32位,低位为1,剩余位为0

bool is_set = bitVec.any();      //true,因为有1被置位
bool is_not_set = bitVec.none(); //false,因为有1被置位
bool all_set = bitVec.all();     //false,因为有1被置位

size_t onBits = bitVec.count();  //返回1
size_t sz = bitVec.size();       //返回31

bitVec.flip();                   //反转bitvec中的所有位
bitVec.reset();                  //将所有位复位
bitVec.set();                    //将所有位复位
  • Members of the flip, set, reset and test allows us to read and write bits specified location , for example:
std::bitset<32> bitVec(1U);     //32位,低位为1,剩余位为0

int i = 5;
bitVec.flip(0);                 //翻转第1位          
bitVec.set(bitVec.size() - 1);  //置位第1位
bitVec.set(0, 0);               //复位第1位
bitVec.reset(i);                //复位第i位
bitVec.test(0);                 //返回false,因为第1位是复位的
  • Const subscript operator to reload the attributes:
    • const version of the value of the subscript operator returns to the position where BOOL, returns true if the bit is a 1, this bit returns to 0 falase
    • Non-const version returns a special type defined bitset, allows us to manipulate the value of the specified bit
std::bitset<32> bitVec(1U);   //32位,低位为1,剩余位为0

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

Bitset extracted values ​​(to_ulong, to_ullong)

  • to_ulong to_ullong operation and return a value , stored objects with the same bit pattern bitset
  • Only when the size of the bitset <= corresponding size (to_ulong as unsigned long, to_ullong as unsigned long long), in order to use these two operations
  • E.g:
//在64位机器上,long long 0ULL是64位的,因此~0ULL是64个1
std::bitset<128> bitvec3(~0ULL);  //0~63位为1,63~127位为0
	
unsigned long ulong = bitvec3.to_ulong();
std::cout << "ulong = " << ulong << std::endl;
  • Operating results above procedures are as follows:

 

The operator IO bitset

  • Operator input (>>):
    • A stream read from the input character string is saved to a temporary object
    • Until the number of characters read reaches bitset corresponding size, or the character is not encountered when 1 or 0, or when the end of the file or input error, the reading process is stopped
    • After the reading process immediately using a temporary string object to initialize the bitset
    • If the number of characters read is smaller than the size of the bitset, as usual, high bit is set 0
  • Output operator (<<):
    • Print a bitset object bit pattern
  • E.g:
std::bitset<16> bits;
std::cin >> bits;
std::cout << "bits: " << bits << std::endl;

Case presentation

  • Now we have a program: Suppose there are 30 students in the class, the teacher to student conduct a weekly inspection, testing results and not divided by two. We used to represent a unsigned long, unsigned long on any machine at least 32, so you can use every instead of a student
  • Here we start with the most common bit operation to complete this function
bool status;
unsigned long quizA = 0;    //此值被当作位集合使用
quizA |= 1UL << 27;         //指出第27个学生通过了检测
status = quizA&(1UL << 27); //检查第27个学生示范通过了检测
quizA &= ~(1UL << 27);      //第27个学生为通过检测
  • We used to do the same work bitset above 
bool status;
	
std::bitset<30> quizB; //每个学生分配一位,所有位初始化为0
quizB.set(27);         ///指出第27个学生通过了检测
status = quizB[27];    //检查第27个学生示范通过了检测
quizB.reset(04);       //第27个学生为通过检测

 

Released 1504 original articles · won praise 1063 · Views 430,000 +

Guess you like

Origin blog.csdn.net/qq_41453285/article/details/104668968