1

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...)?

cam
  • 8,725
  • 18
  • 57
  • 81
  • 2
    Keep in mind that `sizeof(bool)` is implementation defined - it might be 1 (which would probably be 8 bits), but it need not be. It certainly doesn't have to be 32-bits (in fact for MSVC it's 1, or 8 bits). – Michael Burr Apr 29 '10 at 22:45

2 Answers2

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::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
  • 74,820
  • 18
  • 121
  • 166
  • 1
    The 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
  • 2
    Even 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
    This answer is far more interesting that the question! – Clifford Apr 29 '10 at 22:43
  • @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