# Interchanging HIGH/LOW with true/false

**URL:** <https://forum.arduino.cc/t/interchanging-high-low-with-true-false/146611>\
**Category:** Programming\
**Created:** [February 21, 2013, 7:48am UTC](https://forum.arduino.cc/t/interchanging-high-low-with-true-false/146611 "2013-02-21T07:48:17Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![stoopkid](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@stoopkid](https://forum.arduino.cc/u/stoopkid)\
**Post date:** [February 21, 2013, 7:48am UTC](https://forum.arduino.cc/t/interchanging-high-low-with-true-false/146611/1 "2013-02-21T07:48:17Z")

</div>

I notice that both of these lines work:

```cpp
boolean pressed = (digitalRead(3)==HIGH);

boolean pressed = digitalRead(3);

```

I would assume that the first is more correct unless HIGH and LOW were interchangeable with true and false in which case the second line would be a better way to write it.

Which is correct? or _more_ correct? Can I always trust that second one to work?

Thanks

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [February 21, 2013, 7:58am UTC](https://forum.arduino.cc/t/interchanging-high-low-with-true-false/146611/2 "2013-02-21T07:58:20Z")

</div>

Boolean FALSE is zero, anything else is TRUE, so you can use any value that resolves to one or the other in a boolean test. The answers from your examples depend more on how the switch is wired than the program statements used. For instance, a floating switch input is not guaranteed to be in one state or another for any length of time if the switch is open.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 21, 2013, 7:59am UTC](https://forum.arduino.cc/t/interchanging-high-low-with-true-false/146611/3 "2013-02-21T07:59:38Z")

</div>

A boolean is simply a byte sized variable. True is non-zero. False is zero.

HIGH and LOW are defined as 1 and 0 which match the definitions of true and false.

So, either f your statements will work, under some circumstances, although I prefer the first one. It explicitly says that you want to compare the reading of the pin to HIGH.

Think about what the second statement would mean if the internal (or an external) pullup resistor is used. When pullups are used, LOW (false) mean pressed.

---

<div class="post-metadata">

**Author:** ![robtillaart](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robtillaart/32/1098811_2.png) [@robtillaart](https://forum.arduino.cc/u/robtillaart)\
**Post date:** [February 21, 2013, 8:23am UTC](https://forum.arduino.cc/t/interchanging-high-low-with-true-false/146611/4 "2013-02-21T08:23:14Z")

</div>

Definitely the first one is better as it is the most explicit.

Bob Martin (from the book Clean code) would say "use descriptive names where possible" and add spaces around the ==

boolean isButtonXPressed = (digitalRead(ButtonX) == HIGH);

This makes the code self documenting

---

<div class="post-metadata">

**Author:** ![nickgammon](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@nickgammon](https://forum.arduino.cc/u/nickgammon)\
**Post date:** [February 21, 2013, 9:18am UTC](https://forum.arduino.cc/t/interchanging-high-low-with-true-false/146611/5 "2013-02-21T09:18:23Z")

</div>

> ```arduino
> boolean pressed = (digitalRead(3)==HIGH);
> 
> ```

You don't need the brackets, so:

```arduino
boolean pressed = digitalRead(3) == HIGH;

```

I prefer that, because for one thing, in a lot of cases, where you use the pull-up resistor, pressed would be:

```arduino
boolean pressed = digitalRead(3) == LOW;

```

Second, semantically, it is "true" you did a digital read, but did it return HIGH or LOW?

---

<div class="post-metadata">

**Author:** ![michinyon](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@michinyon](https://forum.arduino.cc/u/michinyon)\
**Post date:** [February 21, 2013, 9:44am UTC](https://forum.arduino.cc/t/interchanging-high-low-with-true-false/146611/6 "2013-02-21T09:44:45Z")

</div>

Some people advocate writing code like

```cpp
boolean state = ( HIGH == digitalRead(3) )

```

The reason to do this, is that you will get a compiler error if you type the wrong number of equals signs.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 21, 2013, 10:50am UTC](https://forum.arduino.cc/t/interchanging-high-low-with-true-false/146611/7 "2013-02-21T10:50:16Z")

</div>

> michinyon:  
> Some people advocate writing code like
> 
> ```arduino
> boolean state = ( HIGH == digitalRead(3) )
> 
> ```
> 
> The reason to do this, is that you will get a compiler error if you type the wrong number of equals signs.

Personally I err on the side of extra brackets. Yes, it may look a little more crowded, but you can then be 100% certain what order things are going to happen in. Some of the order of precedences can be a little grey - if you have two operators that have the same order of precedence, which one will happen first? And can you remember the order of precedence of ALL the operators off the top of your head? Thought not. I have a good memory, and I can't - only that \* and / happen before - and + (as taught in maths lessons when you're 6). So, add brackets to make it explicit. Assembly doesn't use brackets, so it shouldn't make the slightest bit of difference to the quality of the resultant code, but it might make all the difference to your calculations.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 21, 2013, 11:11am UTC](https://forum.arduino.cc/t/interchanging-high-low-with-true-false/146611/8 "2013-02-21T11:11:27Z")

</div>

> Some of the order of precedences can be a little grey

I disagree. The precedence rules are quite well defined.

> And can you remember the order of precedence of ALL the operators off the top of your head?

THIS is the real problem. I can't. I can remember that the order is left to right for operators of the same precedence, that multiplication and division are higher than addition and subtraction, that multiplication and division have the same precedence, and that addition and subtraction have the same precedence.

Where ==, \>=, \>, \<=, \<, comma, dot, -\>, the various shift operators, and the other bit operators fit I have no clue, without looking them up.

But, I do know that parentheses let ME control the order the way I want. So, I use them.

I think OPs use of parentheses around the comparison (as in (digitalRead(n) == HIGH)) are a good idea. It clearly indicates, to me, that it is the result of that comparison that is being assigned to the variable.

---

<div class="post-metadata">

**Author:** ![olf2012](https://avatars.discourse-cdn.com/v4/letter/o/f9ae1b/32.png) [@olf2012](https://forum.arduino.cc/u/olf2012)\
**Post date:** [February 21, 2013, 1:01pm UTC](https://forum.arduino.cc/t/interchanging-high-low-with-true-false/146611/9 "2013-02-21T13:01:33Z")

</div>

> robtillaart:  
> Definitely the first one is better as it is the most explicit.
> 
> Bob Martin (from the book Clean code) would say "use descriptive names where possible" and add spaces around the ==
> 
> boolean isButtonXPressed = (digitalRead(ButtonX) == HIGH);
> 
> This makes the code self documenting

I go one step further with "descriptive names" and define

```arduino
const boolean ButtonDown = false; // button connects to GND
...
if(digitalRead(pinButton) == ButtonDown){...}

```

If I need to change the button to high-side switching, there is only one line of code to change

---

<div class="post-metadata">

**Author:** ![pYro\_65](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/pyro_65/32/214665_2.png) [@pYro\_65](https://forum.arduino.cc/u/pYro_65)\
**Post date:** [February 21, 2013, 1:15pm UTC](https://forum.arduino.cc/t/interchanging-high-low-with-true-false/146611/10 "2013-02-21T13:15:16Z")

</div>

> olf2012:  
> I go one step further with "descriptive names" and define
> 
> ```arduino
> const boolean ButtonDown = false; // button connects to GND
> 
> ```

...  
if(digitalRead(pinButton) == ButtonDown){...}

> ```arduino
> 
> If I need to change the button to high-side switching, there is only one line of code to change
> 
> ```

Thats what enums are great for:

```arduino
enum BUTTON_STATE{
  ButtonDown ,
  ButtonUp
};
...
if(digitalRead(pinButton) == ButtonDown){...}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 21, 2013, 1:25pm UTC](https://forum.arduino.cc/t/interchanging-high-low-with-true-false/146611/11 "2013-02-21T13:25:14Z")

</div>

```cpp
const boolean ButtonDown = false; // button connects to GND

```

I like the approach, but I'd argue that the type and value should match the return type and value of digitalRead():

```cpp
const int ButtonDown = LOW; // button connects to GND

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 22, 2013, 3:20pm UTC](https://forum.arduino.cc/t/interchanging-high-low-with-true-false/146611/12 "2013-02-22T15:20:37Z")

</div>

> PeterH:
> 
> ```arduino
> const boolean ButtonDown = false; // button connects to GND
> 
> ```
> 
> I like the approach, but I'd argue that the type and value should match the return type and value of digitalRead():
> 
> ```arduino
> const int ButtonDown = LOW; // button connects to GND
> 
> ```

... why is digitalRead() even an int?! And on an 8 bit core too. It really should be either (unsigned) char, or boolean.

"Arduino. Helping to encourage bad practices since 2005."

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 8:05pm UTC](https://forum.arduino.cc/t/interchanging-high-low-with-true-false/146611/13 "2021-05-05T20:05:36Z")

</div>


