My current project, i.e. decoding RF Signals from a bunch of wireless thermometers, requires the manipulation of long bit sequences.
As C does not offer a native BIT array I am using INT arrays to store these sequences.
However I will need to reduce the memory footprint and was wondering which route to take.
The available options appear to be either of the following:
Using bitRead(X,n)/bitWrite(X,n) with X being as large as 256 bit - is this possible ??
A bitfield structure like this
struct {
unsigned int bit : 1;
} bits[256];
How large will 'bits' be? 32 byte or 512 byte??
my own 'bit field' class with overloaded [] operator.
But what would {myField[0] = 1;} do if [] was overloaded like so:
unsigned int &operator[] (int i) { /* return bit i */}
Thanks for your input
Sebastian
PS:
The bit sequeces do no follow any 8 or 16 bit pattern. In fact the encoded values each have different bit lengths.
I can't tell exactly because in order to keep the compiler from removing unused variables I needed some Serial output which yielded 1200 byte of global variables.
It uses dynamic memory which makes it slower than what is possible. On the other hand it can work with any element size, but again it is not fast. For raw performance go with the solution of John Wasser in #3
It uses dynamic memory which makes it slower than what is possible. On the other hand it can work with any element size, but again it is not fast. For raw performance go with the solution of John Wasser in #3
Great. I've already taken a look at it.
Could you briefly explain why the public methods get() and set() loop through the entire bit array to get/set a bit value?
I thought this could be implement in one or two assignments (similar to John Wasser's solution)
Could you briefly explain why the public methods get() and set() loop through the entire bit array to get/set a bit value?
The bitArray class can be used also to store an array of elements of any size from 1 to 31 bits in the most efficient way. This can be 1000 elements of 1 bit (boolean) or 2000 elements of 3 bits (throw of a dice) or 500 elements of 10 bits (analogRead)
The get() and set() do not go through the entire array, they only go through every bit of the element to be stored / retrieved. Storing and retrieving elements is done bit by bit to be sure it is correct. At a lower level the memory location of the bits are calculated for every bit. This can be made more efficient but I need a good design to keep it generic without increasing the footprint too much.
A class that only can store 1 bit elements (boolArray?) could be based upon the bitArray class in a more efficient way as the location of every bit is easier to calculate. The footprint of the boolArray would probably be slightly smaller too.
I thought this could be implement in one or two assignments (similar to John Wasser's solution)
Yes, it can if you use an array size smaller than 2000 bits (that would fit in one allocation block)
Yes, the macros will work with variables larger than a byte but on an 8-bit processor the generated code is generally more efficient if you use byte variables.
robtillaart:
A class that only can store 1 bit elements (boolArray?) could be based upon the bitArray class in a more efficient way as the location of every bit is easier to calculate. The footprint of the boolArray would probably be slightly smaller too.
...
I thought this could be implement in one or two assignments (similar to John Wasser's solution)
Yes, it can if you use an array size smaller than 2000 bits (that would fit in one allocation block)
Thanks a lot Rob.
I will probably implement a bit array class tailored to my problem.