Computers, Development Boards & Shields
Educational & Robotics Kits
Electronic Sensors
Motors & Controllers
Communication & Display
Electronic Components
Soldering & Lab Tools

Make your Arduino/C++ code 8 times more efficient with a new BitArray Class!

So, I remember when I first started coding for micro-controllers it was not long before I managed to run out of on chip memory with how inefficiently I had learned to program on the computer. In one for statement it is easy to cause a buffer overrun and make your chip do some seriously weird stuff.

Once I realized that was why? my projects were crashing, I thought well I will solve my problems by storing registry values in a Boolean value. A Boolean is only a 1 or 0 so it should only take one bit of memory right? Wrong! I was quite disappointed to discover that a Boolean value still takes an entire byte of memory on the computer, and even worse, that the same policy carries over to the micro-controller world.

I know these days it seems efficiency is the last thing anyone cares about, but when you want to store large amounts of data in memory it may not be a big deal with 8 Gigabytes on a computer, but when you only have 1.5 K of ram the difference between being able to store less than 1000 or around 8000 points of usable data becomes quite significant.? Sadly until recently I simply did not have the technical know-how to tackle the problem, so I simply avoided doing things that would require larger sums of memory on a micro-controller.

Recently I started helping out some students from Colorado State University Pueblo on a rover project, and what would you know we wanted to store Ultrasonic Mapping data in memory for navigation, but could only store 30 by 30 points of obstacle data. Sure for simple stuff that’s okay, but if you declare any variables after that your toast. That took me back to the drawing board, and brought me back to the idea I had had a couple years back of making a new data type that actually stored and handled data at the bit level.

What I managed to put together I am sure still has room for improvement (so please don’t hesitate to make any additions and let me know so we can keep the library as fast and efficient as possible), but what I was able to produce was a library that actually stores 8 bits of data, and allows individual bit manipulation and boolean like behavior without the the user having to perform bitwise manipulations. Whats better is that there is no additional overhead, an Array of 256 BitArrays takes just 256 Bytes in memory (which is 2048?Boolean values)!

That is to say 2048 Boolean values finally fits in 2048 Bits! The code works in C++ and in the Arduino environment, so if you are writing C code of any kind you should be able to use this class to make bit level manipulation easier, and make your memory usage more efficient.

[stextbox id=”download”]CURRENT VERSION 1S0A
Library for the Arduino IDE available for download.[/stextbox]

Download the most recent version.

We put a lot of work into our hardware and software. Please share it if you know anyone that would benefit from our free libraries.

The code is available to download, but I present it here for those who are trying to understand it better, or do something similar in there code.

BitArray.h handles declaration of all class members, and implementation of a couple simple initialization routines:

[codebox 1]

BitArray.cpp handles the bitwise operations, and actual operator overloads that make the BitArray and BitRef data types behave correctly.

[codebox 2]

I would love to hear if you have any suggested improvements, or if this code has helped you do something you couldn’t before. Happy coding!




2 Responses to “Make your Arduino/C++ code 8 times more efficient with a new BitArray Class!”

  1. June 13, 2013 at 3:30 am #

    “Whats better is that there is no additional overhead, an Array of 256 BitArrays takes just 256 Bytes in memory!”

    Is that correct? I thought a 256 Bit array would only use 32 Bytes of memory, or am I missing something?

    • Joseph Dattilo June 19, 2013 at 4:26 am #

      You even had me going there for a second. 256 BitArrays take 256 Bytes because each bit array contains 8 individual Boolean values. This is required because the minimum working resolution of memory is a single byte. It is not possible to manipulate a single bit without rewriting the whole byte.

      That is the main reason I wrote the library, because out of lazyness pretty much all programming languages force you to spend 8 bits for a single boolean value. Course if you only need one boolean value then you end up wasting a byte anyways, but for storing large numbers of boolean values this library should be the ticket.

Leave a Reply