Do true and false compile as the same value as HIGH and LOW?

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);

Expression2:

digitalWrite(PIN10, myBoolean);

Thanks,

Mike

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.

You can simplify your if/else code as follows:

digitalWrite(pin, myboolean ? HIGH : LOW);

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

An option would be to make a #define function or an inline bit of C:

void inline boolWrite(byte pin, boolean b) {
  digitalWrite(pin, b ? HIGH : LOW);
}

PaulMurrayCbr:
An option would be to make a #define function or an inline bit of C:

void inline boolWrite(byte pin, boolean b) {

digitalWrite(pin, b ? HIGH : LOW);
}

I've done things like this in the past:

#define TEST_B (testB == true) ? HIGH : LOW

digitalWrite(PIN10, TEST_B);

But I have never seen inline before and I don't see where you returned anything from that method so what's that all about?

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?

...R

A reasonable summary of how the compiler sees the symbols...

const int HIGH = 1;
const int LOW = 0;

const uint8_t true = 1;
const uint8_t false = 0;

From which the answer to the OP's question is unequivocally YES

...R

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!

from Arduino.h:

#define HIGH 0x1
#define LOW  0x0

And from C++ specification you'll see true and false are 1 and 0:
https://en.cppreference.com/w/cpp/language/bool_literal

MarkT:
Just look at the source code - that's the great thing about using open source tools!

from Arduino.h:

#define HIGH 0x1

#define LOW  0x0

Check some of the newer Arduino cores, such as the one for the Nano Every and UNO WiFi Rev 2, HIGH and LOW are defined as an enum.

See https://github.com/arduino/ArduinoCore-API/issues/25 for some possible problems

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.

I happen to think that true/false makes more sense for this function

Do you mean digitalWrite() ?

Yes, digitalWrite(). I should have been explicit - it should have been designed with a boolean parameter..

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.

marco_c:
Yes, digitalWrite(). I should have been explicit.

As you say, it is a matter of opinion, but how does

digitalWrite(pin, true);

make any sense ?

HIGH and LOW at least imply the voltage level of the pin

There is an integral conversion rule for bool

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

from Implicit conversions - cppreference.com

marco_c:
There is an integral conversion rule for bool

I stand corrected, thanks.

UKHeliBob:
As you say, it is a matter of opinion, but how does

digitalWrite(pin, true);

make any sense ?

HIGH and LOW at least imply the voltage level of the pin

Using true does get confusing when the pin is driving an active-low device. Outputting false to turn a relay ON is hardly intuitive.

Reasonable? Absolutely! Guaranteed across all compilers and IDEs? Not a snowballs chance in hell!