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...)?
Asked
Active
Viewed 295 times
2 Answers
8
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.)

GManNickG
- 494,350
- 52
- 494
- 543
-
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 Apr 29 '10 at 22:42
-
@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 – GManNickG Apr 29 '10 at 22:47
-
"greater than" was just *wrong* rather than *too strict*. I stand corrected on the minimum size of `long` however - apologies. – Clifford Apr 30 '10 at 15:07
-
@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. – GManNickG Apr 30 '10 at 19:07
6
No, std::bitset
s 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
- 74,820
- 18
- 121
- 166
-
1The real controversy over std::vector
is that it isn't a real container and breaks all kind of behavior that std::vector is supposed to have. – Edward Strange Apr 29 '10 at 22:05 -
2Even worse, as I hear it the fact that std::vector
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 Apr 29 '10 at 22:15 -
1
-
@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 Apr 29 '10 at 22:44