Just wondering if anyone knows if boolean true and false compile to the same values as HIGH and LOW universally amongst all Arduino C++ compilers? Just wondering if both of these expressions would do the same thing universally amongst all compilers:
Expression1:
if (myBoolean == true) digitalWrite(PIN10, HIGH);
else digitalWrite(PIN10, LOW);
If the designer of digitalWrite() wanted to make sure they were compatible with HIGH and LOW they would have used true and false in the first place. However, they did not and even if all current implementations were compatible there is no guarantee that they will remain so, so you should always use the value specified by the library designer.
marco_c:
If the designer of digitalWrite() wanted to make sure they were compatible with HIGH and LOW they would have used true and false in the first place. However, they did not and even if all current implementation were compatible there is no guarantee that they will remain so, so you should always use the value specified by the library designer.
You can simplify your if/else code as follows:
digitalWrite(pin, myboolean ? HIGH : LOW);
Yes, that's how I normally write those expressions, but the question came up so I just wanted to check.
I suspected the answer to be NO, there are no guarantees, but just wanted to make sure.
Thank you!
Mike
marco_c:
If the designer of digitalWrite() wanted to make sure they were compatible with HIGH and LOW they would have used true and false in the first place.
So what does the compiler ACTUALLY produce for {A} HIGH and LOW and {B} true and false?
EasyGoing1:
Just wondering if anyone knows if boolean true and false compile to the same values as HIGH and LOW universally amongst all Arduino C++ compilers?
Just look at the source code - that's the great thing about using open source tools!
So what does the compiler ACTUALLY produce for {A} HIGH and LOW and {B} true and false?
That is not a relevant question.
The designer of the API has made a commitment to support HIGH and LOW. This happens to be defined, in some version of the library, as 1 and 0. In another implementation or future version he/she is at liberty to change this as long as they keep supporting the same functionality for the new values of the symbolic constants. So the actual value of the constants is irrelevant to the application coder as long as they honour the API 'contract'.
Doing anything other than using the defined symbols is really bad coding practice, can lead to hard-to-find bugs and has the high potential of creating non-portable source code.
I happen to think that true/false makes more sense for this function, but what I think does not matter at this stage.
The API definitions of HIGH / LOW via #define, enum, etc are one thing.
But 'bool' is an actual C++ type. A bool variable can take on the values of 'true' and 'false'. I don't think the language specification defines any rules for how the compiler designer maps 'true' and 'false' into internal, machine-level representations. So I think, technically, allowing your code to assume a specific internal representation or trying to equate / compare a bool variable with a variable of any other type will invoke undefined behavior.
If the source type is bool, the value false is converted to zero and the value true is converted to the value one of the destination type (note that if the destination type is int, this is an integer promotion, not an integer conversion).