There is a special, non-standard Arduino data type called "boolean".
How does it compare with the standard C++ data type "bool", and what are the reasons to use one or the other?
If they are identical in usage, why do both exist, and which one is preferred in Arduino code?
3 Answers 3
While the other answers give generally useful information, the authors seem to have not read the question very carefully because they are talking about the bool type rather than the boolean type. In the Arduino API, boolean
is a type alias for bool
, as defined in Arduino.h:
https://github.com/arduino/Arduino/blob/1.8.3/hardware/arduino/avr/cores/arduino/Arduino.h#L125
typedef bool boolean;
So you can use the boolean
type just as you would bool
and any standard C++ programming reference will instruct you in the use of bool
. For this reason the other answers which mistakenly refer to bool
are still somewhat useful.
Apparently at some point in the past the Arduino folks decided the name boolean
would be more beginner friendly than bool
, perhaps to be consistent with the Processing language. However, as discussed here:
https://github.com/arduino/Arduino/issues/4673
the unnecessary use of a non-standard type is now considered by some to be a poor decision as its use makes code less portable and really doesn't provide any clear advantage over bool
. A proposal is in the works to remove the use of boolean
from all official Arduino code and document bool
in the Arduino reference and some even wish to formally deprecate boolean
. Therefore my advice is to use bool
instead of boolean
in your code.
A Boolean variable type is one that has only 2 possible values, true
or false
.
Internally, it is created as an int
(16-bit integer) or a uint8_t
type (8-bit integer).
You can assign the "values" of true
or false
using the assignment operator:
bool foo;
foo = true;
if (foo) {
do_something();
}
Internally, a true
is a 1
and a false
is a 0
, and the number values could be equally substituted.
Boolean types in Arduino and C++ mostly exist for readability, and not for any particular efficiency over int
type.
The official reference can be found here
It is best to always use true and false, and not use numbers.
Below some examples are given
Assign false resp true to a Boolean:
bool a = true;
bool b = false;
Check the Boolean value:
if (b)
{
// Executed when b is true
}
else
{
// Executed when b is false
}
if (a && b)
{
// Executed when a is true and b is true
}
if (a || b)
{
// Executed when a is true or b is true
}
If b is false, a will be false, otherwise a will stay as it was
a &= b;
If b is true, a will be true, otherwise a will stay as it was
a |= b;
// If a is true it will be false and vice versa.
a = !a;
-
1That last one should be a = !a shouldn't it? ~ is a bitwise not. Depending on how true is defined ~true may still be true.Delta_G– Delta_G2017年08月15日 17:25:02 +00:00Commented Aug 15, 2017 at 17:25
boolean
is a non-standard type alias forbool
set in the Arduino core library and therefore is very much Arduino specific. See my answer for details.