integer values for HIGH and LOW

Hello,
I just discovered (by accident) that 1 and 0 work the same as HIGH and LOW for pin output values. I wish I had known this earlier! Please consider mentioning this in the online reference. Using integers allows you to set pin output values with the result of a calculation - very handy in certain situations. You can even write statements like this: digitalWrite(pin, a > b ? 1 : 0);
Thanks for considering my suggestion, and thanks for creating the Arduino!
Tom

JavaMan:
Hello,
I just discovered (by accident) that 1 and 0 work the same as HIGH and LOW for pin output values. I wish I had known this earlier! Please consider mentioning this in the online reference. Using integers allows you to set pin output values with the result of a calculation - very handy in certain situations. You can even write statements like this: digitalWrite(pin, a > b ? 1 : 0);
Thanks for considering my suggestion, and thanks for creating the Arduino!
Tom

Well you can simplify this further, since GCC uses 1 as the default value for relational operators on most platforms, including the avr. Though the compiler should generate the same code, since one is canonicalized to the other if STORE_FLAG_VALUE defined for the machine declaration is 1:

digitalWrite(pin, a > b);

That being said, I personally tend to think using LOW and HIGH conveys the meaning more than just 0 or 1.

I just discovered (by accident) that 1 and 0 work the same as HIGH and LOW for pin output values

if i'm right this wasn't true in the old IDE (0022)

if i'm right this wasn't true in the old IDE (0022)

You're not right; from "Wiring.h"

#define HIGH 0x1
#define LOW  0x0

Further, "digitalWrite", follows the spirit of C, and treats all non-zero values as HIGH.
Whilst this is not guaranteed for the future, it would take a very strong argument to change this behaviour.

Thank you all for the responses, especially the one about all non-zero values . Looks like experienced people already know such things, which is to my point that beginners should be able to learn about it by reading it in the reference documentation.

it is a c ANSI standard, i think, but i've already been wrong

As the pin only has 2 states - HIGH & LOW, 1 & 0. TRUE & FALSE...

Having worked with PLCs in the past I tend to think TRUE/FALSE and translate to HIGH/LOW.

experienced people already know such things

Well, yeah, sort of. It's a computer. That means that eventually, everything becomes a number, stored somewhere in some number of bits. And there are a limited number of choices of number that make sense for LOW and HIGH...

Perhaps this is one of the things that separates the "low level embedded programmers" from the "application programmers." In a modern application programming language, you rarely have to remember that your "window" is actually a collection of numbers. Program an AVR chip, and you need to remember that almost all the time... Arduino is in-between.

1 Like