Reputation: 763
I was studying about arithmetic types in C++ Prime by Stanley B. Lippman. When the author talks about integral types and the size of each one of them, like for example char is 8 bits, I noticed that it is not so clear the minimum size of a boolean. Considering type conversions, and the following code:
bool b = 42; // b is true
int i = b; // i had value 1
Can I say that booleans have the same minimum size of an integer (usually 16 bits), since if it is false it would be 0, that is an int, and 1 if it is true, another int? And if what I said is wrong, what is the minimum size of a boolean?
Upvotes: 2
Views: 4296
Reputation: 50046
What is the minimum size of a boolean?
in standard you can read 5.3.3:
The result of sizeof applied to any other fundamental type (3.9.1) is implementation-defined. [ Note: in particular, sizeof(bool), sizeof(char16_t), sizeof(char32_t), and sizeof(wchar_t) are implementation-defined.75 —end note ]
and note:
sizeof(bool) is not required to be 1
so it might be 1 byte but it might be also 4 bytes. Standard also allows for ints to be of size 1 byte 16bits:
1 == sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
so minimum size for bool is 1 byte, the same as for int. The other question is whether you will ever find platform with 1 byte int
type.
[edit]
minimum size (guaranteed minimum) for int
is 16bits, this size guarantee for integral/arithmetic types in C and C++ explains why.
Upvotes: 4
Reputation: 145457
There is a difference between the general notion of “boolean” that you ask about, and the C++ type bool
.
A bool
that is not a bitfield is minimum 1 byte, i.e. sizeof(bool)
≥ 1. This limit is because a byte is the minimum addressable unit; any C++ object is at least 1 byte. The standard does not place any upper limit on the size of bool
, but in practice it will not be larger than can be handled with single memory read and write operation.
A boolean variable is any variable used to implement the notion of boolean. There are a lot of boolean types around, not just C++'s own bool
. E.g., in Windows programming you have a BOOL
type that's more than one byte, and that in some cases can represent logical true via any non-zero value.
And in some cases, with a collection of boolean values they can be represented with just 1 bit each, for example in a std::bitset
or in a std::vector<bool>
(which is special-cased for the item type bool
in order to allow this). Or, I believe, but I haven't checked if that's supported, with a bitfield of size 1 of type bool
. And these considerations means that the question is a bit too vague to have a simple and crisp answer. If you'd asked about sizeof(bool)
it would have been much more clear-cut: just 1 or more bytes, depending on the implementation.
Upvotes: 3
Reputation: 240
bool
takes up a minimum 1 byte. Even though there are only two options of true
or false
, it can't be 1 bit because a bool
needs to be addressable.
Upvotes: 9
Reputation: 181
In the c99 standard (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf) the integer is defined as spanning at least the range [-32767, 32767] (see section 5.2.4.2.1). However, the spec also says (in 6.2.5 2)
An object declared as type _Bool is large enough to store the values 0 and 1.
The implications of this, is that the spec in no way requires them to be the same size. Satisfying the spec doesn't require having them match. It is distinctly possible (although unlikely) for a particular implementation to choose to have them be the same size. But it's nothing you can/should rely on.
Upvotes: 1
Reputation: 5209
If you have bool b = 42;
in your code, compiler finds out, that 42
is not of type bool
and will treat it is if it was bool b = true;
.
When you write later int i = true
compiler will again find out, that true
is not integer, and will treat it as it was int i = 1
, because, by definition, 0
is false
and every other int is true
.
However, talking about size of the type is something different. bool
's size will always be at least 8bit
s, because, that's how addresses work.
Upvotes: 0
Reputation: 67584
Can I say that booleans have the same minimum size of an integer
No, if you could it would be in the standard.
Most standard implementations on modern systems (x86/x64) use bool
to be the same size as a register (32/64 bits respectively) for speed reasons. However nothing stops you from having bit-sized bool
variables, they're a simple bitfield away! And on microcontroller implementations bool
is usually as small as possible (a single byte) since your memory is extremely limited.
Upvotes: 1
Reputation: 2440
No, you can't.
A Boolean has a distinct size. Sometimes Ints can be treated as Booleans because of 0,1 but that does not mean a Boolean has the same size as a Int.
Upvotes: 1