STL bitset

The author MiserWeyte

Because before Los Valley Race 20,191,109 month I bitset unfamiliar, led directly to the T3 \ (70 \ the SIM 100 pts \ rightarrow 50pts \) . Review the bitset.


First, the concept

A bitset stores bits.

Thus, as it can be appreciated the bitset a bool array, but faster and can be as direct as a bit integer arithmetic, count the number 1 or the like.

Second, the declaration and initialization

bitset declaration, passed in variable n bitset length:

#include <bitset>
bitset <n> b; // 全为0
bitset <n> b(u); // 从unsigned long型变量u复制而来
bitset <n> b(s); // 从string对象s复制而来
bitset <n> b(s, pos, m); // 从string对象s的[pos, pos+m]复制而来

Use a few examples unsigned long initialization:

bitset<16> bitvec1(0xffff); // bits 0 ... 15 are set to 1
// bitvec2 same size as initializer
bitset<32> bitvec2(0xffff); // bits 0 ... 15 are set to 1; 16 ... 31 are 0
// on a 32-bit machine, bits 0 to 31 initialized from 0xffff
bitset<128> bitvec3(0xffff); // bits 32 through 127 initialized to zero

When using unsigned long string or initialized, the original type is converted into a bit pattern in binary form corresponding to the initial position bitset, the excess discarded bitset specified length.

Third, the operation

This section translated from c ++ Reference .

1. Visit

  • b [pos]: b, a first access position pos

  • b.count (): count the number 1
  • b.size (): Returns the number of bits bitset
  • b.test (pos): Returns b [pos] value
  • b.any (): Are there any test 1
  • b.none (): verify that no 1
  • b.all (): whether the tests were 1 (c ++ 11)

2. Modify

  • b.set (pos): The b [pos] modified as 1
    • If the incoming pos, all bits changed to 1
  • b.reset (pos): The b [pos] modified as 0
    • If the incoming pos, all bits changed to 0
  • b.flip (pos): The b [pos] negated
    • If the incoming pos, all the bit-by-bit negated

3. Transformation

  • b.to_string (): conversion of string
  • b.to_ulong (): is converted to unsigned long
  • b.to_ullong (): is converted to unsigned long long (c ++ 11)

4. Output

  • os << b: b is output to the stream os
    • Sample output
bitset <16> b(0xff);
cout << b;
///////////////////////////////////////////////
//output:
//0000000011111111
///////////////////////////////////////////////

5. bit operation

bitset operator support level, basically treated as a cosmetic.

For some c ++ reference sample :

// bitset operators
#include <iostream>       // std::cout
#include <string>         // std::string
#include <bitset>         // std::bitset

int main ()
{
  std::bitset<4> foo (std::string("1001"));
  std::bitset<4> bar (std::string("0011"));

  std::cout << (foo^=bar) << '\n'; // 1010 (XOR,assign)
  std::cout << (foo&=bar) << '\n'; // 0010 (AND,assign)
  std::cout << (foo|=bar) << '\n'; // 0011 (OR,assign)

  std::cout << (foo<<=2) << '\n';  // 1100 (SHL,assign)
  std::cout << (foo>>=1) << '\n';  // 0110 (SHR,assign)

  std::cout << (~bar) << '\n';     // 1100 (NOT)
  std::cout << (bar<<1) << '\n';   // 0110 (SHL)
  std::cout << (bar>>1) << '\n';   // 0001 (SHR)

  std::cout << (foo==bar) << '\n'; // false (0110==0011)
  std::cout << (foo!=bar) << '\n'; // true  (0110!=0011)

  std::cout << (foo&bar) << '\n';  // 0010
  std::cout << (foo|bar) << '\n';  // 0111
  std::cout << (foo^bar) << '\n';  // 0101

  return 0;
}
///////////////////////////////////////////////////////
//output:
//1010
//0010
//0011
//1100
//0110
//1100
//0110
//0001
//0
//1
//0010
//0111
//0101
///////////////////////////////////////////////////////

Guess you like

Origin www.cnblogs.com/miserweyte/p/11831963.html