Go Down

Topic: Convert int to boolean (Read 4685 times) previous topic - next topic

BobW

Why does the code below print 5 instead of 2 ?
If I store 2 or 3 to a BOOLEAN variable, wouldn't it make the variable TRUE (1) ?

If I "bang" (negate) x and y TWICE before printing (!!x + !!y), I get 2 as I would expect.

Does this mean that boolean variables are really stored as whole bytes?

Code: [Select]
boolean x = 2;
boolean y = 3;
int z = 0;
 Serial.begin(9600);
 z = x + y;
 Serial.println(z, DEC);

PaulS

On the Arduino, boolean is a typedef, not a native type (like int). It's a uint8_t in disguise.

Quote
Does this mean that boolean variables are really stored as whole bytes?


Yes, it does.
The art of getting good answers lies in asking good questions.

Go Up