 # What is a boolean ?

Happy New Year Everybody!

I have a stupid question, i know what's all that stuff but i can't understand the reason...

Why the boolean type is a int 8 bit ?

True, False, Null (may be)

That's make one or (may be two) bit(s).

Why that's in arduino core ?

There's no data type that can store one bit, so bool is based on an 8-bit integer, being the smallest allocatable unit in most architectures.

Happy New Year as well! :)

From the file \Arduino\hardware\arduino\cores\arduino\Arduino.h

``````typedef uint8_t boolean;
``````

It's just an 8-bit unsigned integer value.

Jiggy-Ninja:
From the file \Arduino\hardware\arduino\cores\arduino\Arduino.h

``````typedef uint8_t boolean;
``````

It’s just an 8-bit unsigned integer value.

Here’s a little sketch to show you the difference between a bool and a boolean.

``````bool b;
boolean bn;

void setup() {
Serial.begin(115200);

for (int i = -2; i < 3; i += 2) {
b = i;
bn = i;
Serial.print(b);
if (b) {
Serial.println("  b is true");
}
else {
Serial.println("  b is false");
}
Serial.print(bn);
if (bn) {
Serial.println("  bn is true");
}
else {
Serial.println("  bn is false");
}
}
}

void loop() {
}
``````

A boolean is just a number with a specific human concept attached to it:

0 = false 1+ = true

So 4 is true, 83 is true, 254 is true, but 0 is false.

As already stated the Arduino cannot store a single value of less than 8 bits, so all 8 bits are used and effectively 7 are wasted.

You can compress up to 8 booleans into a single 8 bit struct if you need to save space:

``````struct myFlags {
unsigned flag1:1;
unsigned flag2:1;
unsigned flag3:1;
unsigned flag4:1;
unsigned flag5:1;
unsigned flag6:1;
unsigned flag7:1;
unsigned flag8:1;
} __attribute__((packed)) flags;

flags.flag3 = 1;
flags.flag2 = 0;
``````