Arduino BitReader library

BitReader – An arduino library for reading & writing data as chunks of bits

Introduction

Here is another arduino library that I have written.

As a desktop developer, I am not used to have memory (or even CPU) requirements as most developers will not look at memory consumption as an arduino developer would. Memory & CPU is cheap. If your application is struggling to run on today’s computer, it will run smoothly in 1-2 years.

As most arduino developers have faced, the amount of memory available on embedded devices is pretty limited. Having a way to save bits or our precious memory is interesting.

BitReader is an arduino library that allows one to read or write data as chunks of bits.

Skip to the download section for quick download.

Purpose

This BitReader library allows one to read data which is not necessarily aligned on 8, 16 or 32 bits structures.

Note that since you are encoding data in binary format (bits), the trade-off is that more code is required to process/decode the data and there is a small performance delay required for decoding the data

The library is useful for storing or decoding data in binary format using the minimum amount of bits. For examples…

Library features

Possible use are:

  • Reduces size of data (strings, structures, arrays) in memory to the minimum amount of bits required.
  • Help reading/updating bit-field structures.
  • Handles bit serialization data to/from buffers.

Usage

Create an instance of BitReader, BitWriter or BitAddress depending on the following optimization switches:

  • USE_BITADDRESS_SETTER_GETTER
  • USE_SINGLEBIT_MACROS
  • USE_BITADDRESS_READ_WRITE

Then assign a reading or writing buffer using the setBuffer() method.

Then call the write() method for writing bits to the assigned buffer or call the read() method for reading bits from the assigned buffer. Each method allows one to specify the amount of bits to read or write.

Making text strings shorter

Storing words composed of only lower case letters, spaces and dots requires only 5 bits per characters instead of 8 bits (which saves 3 bits per character). The phase “hello my name is antoine. i wrote the bitreader library.” takes 56 bytes as an array of char but uses 35 bytes as 5 bits chunks.

Allow 6 bits per characters and you can also include capital letters and numbers: “Hello my name is Antoine. I wrote the BitReader library when I was 34 years old.” takes 80 bytes as an array of char but only requires 60 bytes as 6 bits chunks.

Shorter structures definition

The library is particularly useful for dealing with bit-field structures. Consider the following:

The Person structure requires 16 bytes in memory (10+1+1+2+1+1) or 128 bits. However, using bit-field structure, the Person structure can be defined as the following:

The Person2 structure contains the same information as the Person structure but instead of using 128 bits in memory it only requires 105 bits (80+5+3+4+1+12).

A Person could also be defined with masks:

In the last 2 scenarios (Person2 and Person3), the BitReader library allows one to decode a Person structure from a binary buffer.

Make data arrays much shorter

Consider an algorithm that plays a Morse code. Morse code defines 3 symbols that can be played: dots, dashes and pauses.

For example, the following string “Hello my name is Antoine. I wrote the BitReader library when I was 34 years old.” (56 bytes) translate into Morse code as

according to this translator. The whole code takes 267 bytes in memory. However, using 2 bits per code, the whole string can be encoded in a char buffer with only 534 bits (~67 bytes).

The same concept applies to all numeric array.

Demo

The following demo show how to use the library:
(download the BitReader v1.0.70 benchmark demo.ino (754 downloads) )

License

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3.0 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License (LGPL-3.0) for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

DISCLAIMER:
This software is furnished “as is”, without technical support, and with no warranty, express or implied, as to its usefulness for any purpose.

Download

You can download the BitReader arduino library by clicking on the following link:

Download “BitReader v1.1.110 arduino library” BitReader-v1.1.110.zip – Downloaded 605 times – 10 KB

Leave a Reply