In the c++primer above, Bitset can be initialized with unsigned long, but the above example only takes constants such as 0xFFFF, and in practice, when in vs2010, I use the unsigned long type of variable to initialize, Found that there was an error,
Error C2668: "Std::bitset<_bits>::bitset": ambiguous call to overloaded function
Under test, int is 4 bytes, unsigned long 4 bytes, long is also 4 bytes, but int and long can be used to initialize
The following is a c++primer about Bitse using unsigned long and string for initialization, in particular, in the Bitset is the number of low in the former high
The following is a reprint of the relevant Bitset content:
Function: Handle the ordered set of bits
#include <bitset>
Using Std::bitset;
Methods to initialize the Bitset variable:
Bitset<n> b; |
B has n bits, each of which is 0 |
Bitset<n> b (U); |
B is a copy of the unsigned long type U |
Bitset<n> b (s); |
B is a copy of the bit string contained in the string object s |
Bitset<n> B (S, POS, n); |
B is a copy of n bits starting from position POS in s |
Note: n must be a constant expression.
For example:
Bitset<16> BITVEC1 (0XFFFF); Bits 0 ... Set to 1
Bitset<32> bitvec2 (0XFFFF); Bits 0...15 is set to 1; 16...31 is 0
The order in which the bit sets are read from the string object is right-to-left
String Strval ("1100");
Bitset<32> BITVEC3 (Strval);
The string object and the Bitset object are reversed: the rightmost character of the string object (the one with the largest subscript) is used to initialize the low-order bit of the Bitset object (that is, the bit with the subscript 0). It is important to remember this difference when initializing a Bitset object with a string object.
Operation of the Bitset object
B.any () |
Is there a bits in B that is set to 1? |
B.none () |
There is no bits in B that is set to 1? |
B.count () |
Number of bits with 1 in b |
B.size () |
Number of bits in B |
B[pos] |
Access the bits at POS in B |
B.test (POS) |
B is the bits at POS at 1? |
B.set () |
Place all bits in B to 1 |
B.set (POS) |
Put the binary position in B at POS at 1 |
B.reset () |
Place all bits in B to 0 |
B.reset (POS) |
Put the binary position in B at POS at 0 |
B.flip () |
Take all the bits in B counter- |
B.flip (POS) |
Reverse the bits in the POS at the point B |
B.to_ulong () |
Returns a unsigned long value with the same bits in B |
OS << b |
Output the bit set in B to the OS stream |
Bitset<32> Bitvec; 32bits,all Zero
BOOL Is_set=bitvec.any (); False,all bits is zero
BOOL Is_not_set=bitvec.none (); True,all bits is zero
size_t Bits_set=bitvec.count (); Returns the Bits_set value of a size_t type, counting the number of 1
The size_t type is a machine-related unsigned type, sufficient in size.
You can use the following table operators to read or test the bits of an index location:
for (int index=0;index!=32;index++) {
bitvec[index]=0;
}//Use the following table operator to set all bits to zero
for (int index=0;index!=32;index++) {
Bitvec.set (index);
}//Use Set operation to set all bits to 1
if (Bitvec.test (i))
Test if the I bit is 1
if (Bitvec[i])
Same function as previous function
Bitvec.set (); All set One
Bitvec.reset ();//All zeros
unsigned long ulong=bitvec.to_ulong ();
cout<< "ulong=" <<ulong<<endl; When the length of Bitvec is less than unsigned long can
Bitset Initialization issues