tags:

views:

164

answers:

2

In C++, are bitsets actually a set of bools? Doesn't this defeat the purpose of using a BitSet because bools are 32 bits (I think...)?

+8  A: 

They represent a collection of bool's, but those values are really stored as bits in an unsigned long.

The size of a bool is not necessary any number of bits, neither is an unsigned long. (Though the minimum number of bits for any data type is 8, and for an unsigned long it must be at least 32.)

GMan
I think you mean "at least 32" not "greater than 32", although in fact it only need be as long and an int or longer, and an int could be less than 32bits (in a 16 bit processor for example). In practice however, 32 bits is the likely size, even in a 16 or 8 bit platform
Clifford
@Clifford: I do mean at least, greater than is too strict. However, I am correct in stating a long must be 32 bits or higher. http://stackoverflow.com/questions/271076/what-is-the-difference-between-an-int-and-a-long-in-c/271132#271132
GMan
"greater than" was just *wrong* rather than *too strict*. I stand corrected on the minimum size of `long` however - apologies.
Clifford
@Clifford: Right, "too strict" is another way is saying incorrect. "1 is not greater than 1, but it is at least 1." The former comparison is too strict.
GMan
+6  A: 

No, std::bitsets are not actually bools, they are actually bitsets. Who told you that they were bools?

Are you perhaps getting confused with the controversy over std::vector<bool>? Which is, incidientally, the opposite issue, since it looks like a set of bools but is actually a bitset.

Tyler McHenry
The real controversy over std::vector<bool> is that it isn't a real container and breaks all kind of behavior that std::vector is supposed to have.
Noah Roberts
Even worse, as I hear it the fact that std::vector<bool> breaks the usual rules for std::vector is *intentional* - it was written that way as an example of how to write a specialised implementation of std::vector.
Mac
This answer is far more interesting that the question!
Clifford
@Noah "It isn't a real container" is what I mean by it *looks* like a set (er, vector) of bools, but it isn't really.
Tyler McHenry