Pages: [1]   Go Down
Author Topic: Convert int to boolean  (Read 1334 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 48
Bald-Headed, Middle-Aged Nerd
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
boolean x = 2;
boolean y = 3;
int z = 0;
  Serial.begin(9600);
  z = x + y;
  Serial.println(z, DEC);
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50039
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: