8

Consider the C++ code below:

bool a = 5;
bool b = 6;
int c = (int)a + (int)b;

When I compile&run this code, c has the value 2. Does the standard guarantee that, in any compiler/platform, bool values initialized with false (0) or true (not necessarily 1) will be 1 in operations and the code above will always result in c being 2?

And in C99, including stdbool.h, is that still valid?

fbafelipe
  • 4,862
  • 2
  • 25
  • 40
  • 1
    i guess your answer is here: http://stackoverflow.com/questions/2725044/can-i-assume-booltrue-int1-for-any-c-compiler – Alessandro Pezzato Oct 05 '11 at 13:04
  • 2
    IMO, if you assign something other than true/false to a bool variable it should be code-reviewed back to you. – Max Oct 05 '11 at 13:05
  • Thanks @AlessandroPezzato for finding the duplicate – David Rodríguez - dribeas Oct 05 '11 at 13:07
  • 1
    @Alessandro Pezzato In that question he asks about the keywork true, not a variable with a non 0 or 1 value assigned. – fbafelipe Oct 05 '11 at 13:08
  • @Max It is common to see code that assing a pointer directly to a bool, to check if it is not NULL. I know it is a good pratice to compare with NULL first, but sometimes people get lazy when coding and just assing it. – fbafelipe Oct 05 '11 at 13:15
  • @bafelipe: In that question it asks about a value of `true` in a variable of type `bool`, which can only have two values `true` and `false`. The question is exactly the same, unless the problem you are facing is that you believe `bool` to be able to hold values other than `true` and `false`: `bool b = f(); assert( b == true || b == false );` will always pass assuming that `f` returns anything convertible to `bool`, whatever the definition of `f` is. – David Rodríguez - dribeas Oct 05 '11 at 13:18
  • @David Rodríguez In that question he is asking about the keyword true. I am asking about a value stored in a variable because, since the compiler won't use a single bit for a bool, the compiler could do some optimizations and not ensure that it's either 0 or 1. – fbafelipe Oct 05 '11 at 13:23
  • @fbafelipe: do you understand that a boolean can only contain `true` and `false` semantically? You can use an `int` with many values and in a test it will be checked for not-zero, but a `bool` cannot have other values than `true` and `false`. The assert in the previous comment was meant to make that explicit, whatever the value from which you set a `bool` is, after being set it will only be either `true` or `false`. – David Rodríguez - dribeas Oct 05 '11 at 13:32
  • @David Rodríguez Just because semantically it should contain only 0 or 1, doesn't mean the compiler will ensure that. For example, in this code (http://ideone.com/y1IUg) with a enum, semantically f should be 0 or 1, but the compiler does not ensure that. – fbafelipe Oct 05 '11 at 13:40

4 Answers4

12

Section 4.7 (integer versions) of the C++ standard says:

If the source type is bool, the value false is converted to zero and the value true is converted to one.

Section 4.9 makes the same guarantee for floating point conversions.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
1

For compilers, the rule is often that false is 0 and anything else will be true. However, treating bool like it is an integer type is usually considered bad form. The standard however include a rule to convert to int and you assumption is correct false = 0 and true = 1 as long as the compiler adhere to the standard!

In any case, why arithmetic with bool types?

Hope this help

Martin
  • 282
  • 2
  • 6
  • Arichmetic with bool types can be useful to write less code, so you can write x += a;, instead of x += (a ? 1 : 0);. – fbafelipe Oct 05 '11 at 13:51
  • Why not use x |= a; or even x = x || a; all nice and respectful of the underlying types – Martin Oct 05 '11 at 13:54
  • And if x is an int, counting how many (of a set of bools) are true? I know there are ways around, but since this seems simpler, I am asking the question to know if it will always work. – fbafelipe Oct 05 '11 at 13:57
  • 1
    @fbafelipe: Writing less code is *never* my goal. I want to write code that is easy to maintain, easy to understand, and performs well. Sometimes short code meets these goals. Doing arithmetic on bools never does. – aaaa bbbb Oct 05 '11 at 22:42
  • Arrays may have a specialization for booleans, that optimize memory, in which case it may be preferred instead of an array of another type (most probably at the expanse of computation time, but you should not assume people needs). – nojhan Nov 14 '18 at 15:25
  • if you want to check if only one boolean is true, you can do: `if ((bool1 + bool2 + bool3 + bool4) == 1)` – Erez Oct 21 '21 at 05:40
1

According to the standard:

  • true converts to 1
  • false converts to 0

And he cast to int is not necessary as the conversion to int is implicit.

curiousguy
  • 8,038
  • 2
  • 40
  • 58
1

David Schwartz already answered for C++. For the C99 standard we have 6.3.1.4:

When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1.

Since 6.3.1.1 of the standard also makes it clear that _Bool is subject to integer promotions it's clear that a _Bool will always be either 0 or 1.

Quantumboredom
  • 1,426
  • 11
  • 19