2

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?

Gabriel
  • 763
  • 1
  • 10
  • 28
  • 1
    Booleans can be converted to `int`. That doesn't mean they have to have the same size as `int`; `bool` is a distinct type. – chris Aug 15 '15 at 20:51
  • What platform are you using where ints are "usually 16 bits"? – melpomene Aug 15 '15 at 20:54
  • @melpomene: I've worked with many such platforms. They're mostly embedded ones. – barak manos Aug 15 '15 at 20:55
  • 5
    @melpomene, I think the OP meant the minimum size is usually 16 bits, which is correct always, not just usually, according to the standard. – chris Aug 15 '15 at 20:55
  • @chris correct, that was what I meant, thank you for the help! – Gabriel Aug 15 '15 at 20:57
  • 2
    Side note: Not sure who this Lippman guy is, but the language standard does not dictate that `char` is 8 bits. Although it is the case on most platforms, the correct way to determine this is with preprocessor definition `CHAR_BIT` (defined in file `limits.h`). – barak manos Aug 15 '15 at 20:57
  • what do you mean by "minimum" ? – gen-y-s Aug 15 '15 at 21:00
  • @chris ok, I got that. And that really make sense. So bool have a 1 bit size always? – Gabriel Aug 15 '15 at 21:06
  • @GabrielMello, I have to agree that I haven't seen much of anything other than one byte. However, my experience leaves out embedded systems, particularly ones that do special things like 32-bit bytes. – chris Aug 15 '15 at 21:09
  • @chris I understand. But thank you for your explanation, it was very helpful. – Gabriel Aug 15 '15 at 21:14
  • Not an exact duplicate, but close: https://stackoverflow.com/questions/4897844/is-sizeofbool-defined – 5gon12eder Aug 15 '15 at 21:26
  • @5gon12eder that's true, close but not the same! Thank you for the information. – Gabriel Aug 15 '15 at 21:35

7 Answers7

8

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.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
Jon Snow
  • 230
  • 1
  • 9
  • 1
    I don't understand why this answer is so aggressively down-voted. Because while it is terse (maybe too terse), I think it is correct. More correct than some of the higher-ranked answers. – 5gon12eder Aug 15 '15 at 21:19
  • 1
    Upvoting tactically, because some non-technical folks (3 of them so far) have downvtoted, giving an entirely incorrect impression to readers. – Cheers and hth. - Alf Aug 15 '15 at 21:21
  • It can't be 1 bit also because `CHAR_BIT >= 8`. – alecov Jul 28 '16 at 20:59
4

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.

Community
  • 1
  • 1
marcinj
  • 48,511
  • 9
  • 79
  • 100
  • Interesting, so it is more of a theoretical question, because we cannot find this kind of thing in real life? – Gabriel Aug 15 '15 at 21:12
  • The old (and perhaps current) Texas Instruments digital signal processors had CHAR_BIT = 16. Perhaps they had sizeof(int) = 1 also. But I don't know that. – Cheers and hth. - Alf Aug 15 '15 at 21:17
  • @GabrielMello I think so, this gives you an idea how different platforms might break your code if you are not carefull enough. Ie. if you need to have ints of size 4 bytes, then use int32_t instead of int. – marcinj Aug 15 '15 at 21:24
  • @Cheersandhth.-Alf thank you for the information, I will definitely search more about this. I know just a little about Texas Instruments, even knowing that it is not selling anything of this brand in my country, it is a famous (and good?) brand. – Gabriel Aug 15 '15 at 21:30
  • @marcinj so, now it's crystal clear for me. Thank you so much for your explanation about boolean and minimum size. – Gabriel Aug 15 '15 at 21:32
  • 2
    A "byte" in the standardese sense is "whatever the size of `char` is", not "8 bits". The standard requires `int` to have at least 16 bits, `long` at least 32 bits, and `long long` at least 64 bits. – T.C. Aug 16 '15 at 04:28
3

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.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • Thank you for the good and complete explanation about the topic, it was really helpful and also thank you for the advice about the way I should have had asked the question. – Gabriel Aug 15 '15 at 21:40
1

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.

Luke Xu
  • 2,302
  • 3
  • 19
  • 43
1

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.

Blindy
  • 65,249
  • 10
  • 91
  • 131
  • 3
    One thing stopping you from having a 1-bit `bool` is that you can't take its address. – chris Aug 15 '15 at 20:57
  • Nothing's stopping an implementation from reverting to byte-sized `bool` variables if you ever take its address, otherwise it can be stored as bits (similarly how `inline` works in fact). Nobody would do this, of course, since it's very expensive to access sub-register data, but it can be done if you want. And in regards to bit fields, you can't take their address anyway. – Blindy Aug 15 '15 at 20:59
  • 3
    *"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. "* Huh? On all x86 C++ implementations I know, `sizeof(bool) == 1`. – dyp Aug 15 '15 at 21:02
  • My mistake on missing the bit about bit-fields. Assuming it's legal (I don't see why not, but bit-fields are not my strong suit), then my previous comment was pointless. – chris Aug 15 '15 at 21:44
1

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.

Curds
  • 181
  • 7
0

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 8bits, because, that's how addresses work.

Zereges
  • 5,139
  • 1
  • 25
  • 49
  • "will treat it is if it was bool b = true" that's most definitely false, the standard requires no such conversions. – Blindy Aug 15 '15 at 20:57
  • @Blindy, It sounds like that to me (N4527 8.5/17): *Otherwise, the initial value of the object being initialized is the (possibly converted) value of the initializer expression. Standard conversions (Clause 4) will be used, if necessary, to convert the initializer expression to the cv-unqualified version of the destination type*. It's not exactly the same thing, but it's pretty close as a mental picture of what's happening. – chris Aug 15 '15 at 21:05
  • 2
    ""will treat it is if it was bool b = true" is true with regard to the produced program's effect. That's required by the standard. But there is a difference during compilation, namely that a sufficiently verbose compiler may produce a sillywarning. I'm thinking Visual C++ here, but haven't checked. Anyway if someone downvoted for that, as seems to be the case, well that's a very lousy reason to downvote. One might propose some even more clear wording, if one can imagine such. But downvoting, that's most definitely an inappropriate response. – Cheers and hth. - Alf Aug 15 '15 at 21:14