2

The following questions (and answers) indicate that a bool's value when converted to int will be 0 or 1.

  1. Is C/C++ bool type always guaranteed to be 0 or 1 when typecast'ed to int?

  2. c++ bool question

My question is whether the compiler can represent the value as something else internally (up to the conversion). I vaguely recalling the MSVC debugger show the numeric value of the byte representing the bool if it isn't 0 or 1, and I seem to recall seeing 255 sometimes (0xFF).

Said differently, could the following code return something other than 0 or 1?

int boolval(bool z) {
  return *(unsigned char *)&z;
}

I am currently working with a binary format that uses bool's in the structure and am seeing 255's instead of 0's and 1's.

EDIT: I just found an almost identical questions a moment ago so I am linking it here for more information. How is a bool represented in memory? Thanks for the answers.

Community
  • 1
  • 1
Tim
  • 2,708
  • 1
  • 18
  • 32

3 Answers3

9

The compiler can represent true however it likes as long as 1==(int)true and 0==(int)false

The simplest way for the compiler to meet those requirements is to represent false as all zero bits and true as the least significant bit set, because then the "conversion" to int doesn't involve changing anything.

But the compiler is free to represent true as all bits set, and issue instructions to convert that to 1 when converted to int, or even to represent true as all zero bits and false as all bits set or any other odd representation, as long it meets the requirements.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
4

[basic.fundamental]/6:

Types bool, […] are collectively called integral types. A synonym for integral type is integer type.
The representations of integral types shall define values by use of a pure binary numeration system.

This is vague. In no way does the standard require false and true to correspond to 0 or 1 in memory (it could be the other way around). What we do know is that the representation obviously doesn't change, so you will get consistent results.

However, the following rules apply to the x86_64 processor specific ABI:

Booleans, when stored in a memory object, are stored as single byte objects the value of which is always 0 (false) or 1 (true). When stored in integer registers or passed as arguments on the stack, all 8 bytes of the register are significant; any nonzero value is considered true.

The value will for that ABI then be 0 for false and anything for true. It's not guaranteed which value will be returned from your function in that case.

Columbo
  • 60,038
  • 8
  • 155
  • 203
  • That's true for the x86 psABI (see p13 of http://refspecs.linuxfoundation.org/elf/x86_64-abi-0.95.pdf although it says all bits are significant for parameters, with any non-zero value being true), I don't know if e.g. the MS compiler uses that ABI though. – Jonathan Wakely Dec 06 '14 at 22:33
  • 1
    Well, these rules have nothing to do with x86: You can write implementations on x86 that do not follow these rules, and you can write implementations on all other systems that follow these rules... It's all down to what ABI you are using. – cmaster - reinstate monica Dec 06 '14 at 22:34
  • @JonathanWakely Ahh, there it is. I'll correct my answer. – Columbo Dec 06 '14 at 22:37
  • Oops, sorry, I meant to say that's the **x86_64** psABI. The Intel386 psABI doesn't seem to mention `bool` at all. – Jonathan Wakely Dec 06 '14 at 22:42
0

There are 2 parts of this question:

1) What do compilers actually do?

  • All major compiler currently represents false as 0
  • Some compilers however do not represent true always as 1,
    but rather whatever value the source had, so that in
    bool ok = res; ok will have whatever value res had.

2) What do the standard say?

  • No guarantees!
sp2danny
  • 7,488
  • 3
  • 31
  • 53