Arduino misconceptions 7: boolean variables are a native C/AVR type

A lot of Arduino sketches make fairly heavy use of the “boolean” type – after all, digitalRead() and digitalWrite() use TRUE/FALSE or HIGH/LOW don’t they? Why not use boolean? It seems intuitive, after all.

One of the silliest lines in Arduino

One of the silliest lines in Arduino

Well, there are many, many reasons not to use boolean. I think it is one of the biggest mistakes in the Arduino environment.

Everything on the AVR is at least 8 bits wide. If I write to a memory location, register, or port, I need to write all 8 bits. If I read, I need to read all 8 bits. A boolean is just a single bit. We can infer from this that we are going to need to do some work to represent a boolean.

The next thing we need to realise is how C evaluates true and false on various  types. This is really simple. If every bit in an integer is set to 0, evaluate this as false. Otherwise, true. This stems from the way that processors work internally, using the Z (zero) flag to determine if a branch should be taken or not.

This means that 0xA6 is as true as 0×01 is as true as 0xFF. The only thing that evaluates to false is 0×00.

These two things – the smallest native memory location being 8 bits wide, and the evaluation of 0×00 as false – make it really easy to implement a boolean. We just use an 8-bit unsigned integer and call it boolean. We then assigned 0×00 to FALSE and something non-zero to TRUE.

This is almost exactly how Arduino implements booleans. It works, but it hides some of the fundamental concepts you need to understand to efficiently write code for AVR microcontrollers.

What are the problems with this?

  • You are wasting 7 bits of a 8 bit memory location or register. This is fine on a modern PC, but on an AVR microcontroller, you only have 32 registers and limited RAM.
  • This is hugely exacerbated when you have build arrays of boolean. Having an array of bits is very common (for example, in serial transmission). “boolean arr_of_bool[32]” uses 32 bytes to store 32 bits. Use “uint8_t arr_of_uint8[4]” instead, and learn how to set bits.
  • It hides the concept that the only thing that evaluates to false is 0. You often see “if (variable == true)” in Arduino code. It just isn’t required. Even worse is “if (variable ? TRUE : FALSE)”. Ouch – I’ve even seen this tenary operator inside a comparison in a library before.
  • It hides the concept of setting individual bits in a byte. You really cannot go very far in embedded systems without knowing about this.
  • It is not a native C type. Move to another platform or compiler and you will need to re-implement it. Should it be bool or boolean? true or TRUE? HIGH or LOW?

I’d be really keen to see Arduino move towards native data types, but I can’t see it happening.

One thought on “Arduino misconceptions 7: boolean variables are a native C/AVR type

  1. Permalink  ⋅ Reply

    Michael LeBlanc

    August 17, 2013 at 8:44pm

    Yes, you’re right, booleans are a hack to make Arduino seem simple. And I also agree with you that the Arduino will probably never move toward native data types, but that’s ok in my book, because the Arduino is a ‘gateway’ system. People like me–who have never taken a programming course, or an electrical engineering course–can use Arduino with very little initial setup because it’s ‘dumbed-down’ quite a bit. But after a couple of years with Arduino, I started getting curious about the AVR and have done some tinkering with flashing ATMEGA chips using AVR Studio. I grew out of the Arduino to a certain extent, but I needed Arduino to get me going as a novice. I personally hope they keep a lot of the complicated stuff hidden.

Leave a Reply

Your email will not be published. Name and Email fields are required.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">