Enlico
Enlico

Reputation: 28510

Does the standard mandate that the 1-byte storage of a boolean contain only 1 of 2 possible 8-bit values? If so which ones are they?

Related to this and this.

If I define

bool y = true;
bool n = false;

is the bit-wise content of the 1-byte storage of y and n mandated by the standard? If so, which are those two contents? If not, then how is the comparison between bools themselves or between bools and other integer types handled?

Upvotes: 1

Views: 108

Answers (1)

Yksisarvinen
Yksisarvinen

Reputation: 22394

Standard does not require any bit representation for bool, it only requires that there are two values true and false - and in particular, it doesn't seem to require that there is only one representation for each value ([basic.fundamental]#10):

Type bool is a distinct type that has the same object representation, value representation, and alignment requirements as an implementation-defined unsigned integer type.
The values of type bool are true and false.

It is required that true is mapped to 1 and false is mapped to 0 when converting bool to int ([conv.prom]#6):

A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true becoming one.

Upvotes: 3

Related Questions