Hmmm...
A bitset is a special container class that is designed to store bits (elements with only two possible values: 0 or 1, true or false, ...).
The class is very similar to a regular array, but optimizing for space allocation: each element occupies only one bit (which is eight times less than the smallest elemental type in C++: char).
Each element (each bit) can be accessed individually: for example, for a given bitset named mybitset, the expression mybitset[3] accesses its fourth bit, just like a regular array accesses its elements.
Because no such small elemental type exists in most C++ environments, the individual elements are accessed as special references which mimic bool elements:
Sooooo, anyone tested this? I find it hard to believe that GNU's C++ compiler wouldn't implement this, and also hard to believe the site would be flat-out wrong.
EDIT: Ok, quick test:
quixadhal@andropov:~$ cat bittest.cpp
#include <iostream>
#include <bitset>
#include <string>
using namespace std;
class foo {
public:
bitset<13> chunk;
string stuff;
};
int main(int argc, char **argv) {
class foo f;
cout << "sizeof(f): " << sizeof(f) << endl;
f.chunk[3] = true;
cout << "sizeof(f): " << sizeof(f) << endl;
f.chunk[5] = true;
cout << "sizeof(f): " << sizeof(f) << endl;
f.stuff = f.chunk.to_string<char,char_traits<char>,allocator<char> >();
cout << "sizeof(f): " << sizeof(f) << endl;
cout << "f.stuff: " << f.stuff << endl;
}
quixadhal@andropov:~$ !g++
g++ -o bittest bittest.cpp
quixadhal@andropov:~$ ./bittest
sizeof(f): 8
sizeof(f): 8
sizeof(f): 8
sizeof(f): 8
f.stuff: 0000000101000
Change the number of bits from 13 to 113 and....
quixadhal@andropov:~$ g++ -o bittest bittest.cpp
quixadhal@andropov:~$ ./bittest
sizeof(f): 20
sizeof(f): 20
sizeof(f): 20
sizeof(f): 20
f.stuff: 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000101000
So, not 100% compact to the individual bit, but close to the machine word boundary? 113/8 would be 15 bytes... so we used 16 bytes + 4 for the string pointer. Testing with a value of 1024 yields 132 bytes used.
.........................