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]
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:
BitArray.cpp handles the bitwise operations, and actual operator overloads that make the BitArray and BitRef data types behave correctly.
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!