Game Development Reference
In-Depth Information
Listing 18-2 shows the different constructors you can use to initialize bitsets .
Listing 18-2. bitset Constructors
MyBitset defaultConstructor;
MyBitset unsignedLongConstructor{ 0x17 };
MyBitset stringConstructor{ string{ "11011" } };
There are three constructors you can use. The first is the default constructor, which initializes all bits
to zero. The second constructor uses an unsigned long to initialize the bits. In this example, I have
used a literal value to pass into the constructor. The last constructor takes a string containing 1s and
0s representing the value you wish each bit to contain.
Working with bitsets
A bitset provides methods to carry out several different operations. These can be split into methods
that query the current state of the bitset and methods that are used to alter the bits. Listing 18-3
shows the methods you can use to gather information about your bitset .
Listing 18-3. Querying bitsets
cout << boolalpha;
cout << "Size of the bitset: " << stringConstructor.size() << endl;
cout << "Number of set bits: " << stringConstructor.count() << endl;
cout << "Is any bit set? " << stringConstructor.any() << endl;
cout << "Are all bits set? " << stringConstructor.all () << endl;
cout << "Are no bits set? " << stringConstructor.none() << endl;
for (unsigned int i = 0; i < stringConstructor.size(); ++i)
{
cout << "Bit " << i << " value: " << stringConstructor[i] << endl;
cout << "Bit " << i << " test: " << stringConstructor.test(i) << endl;
}
The bitset template provides methods to query the size and count of a bitset . The size method
should return the size you passed into the template and count will return the number of bits that you
have set to true. Figure 18-1 shows the output you can expect to see when you execute the code
from Listing 18-3.
 
Search WWH ::




Custom Search