While looking for something total irrelevant I formed the following question (which why google search has not yet answered).
A single Boolean variable take 1 byte to store. An array of of 16 Booleans takes 16 bytes.
What methods (if any) are available to get the array of 16 Booleans to take only 2 bytes and still be able to address the bits indirectly (similar to an array index)?
For this example the memory savings could be 14 bytes, large arrays save even more.
I can see that now. But that would be limited to unsigned longs (?) at 32 bits.
Using 16 in the example didn't work well. So for sake of discussion, let's bump the example to an array of 40 Booleans (a size larger than an unsigned variable type). What methods (if any) are available to get the array of 16 Booleans to take only 2 bytes and still be able to address the bits indirectly (similar to an array index)?
But that would be limited to unsigned longs (?) at 32 bits.
No, write a simple class to build arrays of unsigned long, and extract/store your bits from the array
What methods (if any) are available to get the array of 16 Booleans to take only 2 bytes and still be able to address the bits indirectly (similar to an array index)?
Modulo arithmetic.
in a previous code of mine, I used the following structure that allowed me to access the individual bit in a char. could possible expand that to a uint16_t...
union {
unsigned char val;
struct {
unsigned char b0:1;
unsigned char b1:1;
unsigned char b2:1;
unsigned char b3:1;
unsigned char b4:1;
unsigned char b5:1;
unsigned char b6:1;
unsigned char b7:1;
};
} in;
sherzaad:
in a previous code of mine, I used the following structure that allowed me to access the individual bit in a char. could possible expand that to a uint16_t...
union {
unsigned char val;
struct {
unsigned char b0:1;
unsigned char b1:1;
unsigned char b2:1;
unsigned char b3:1;
unsigned char b4:1;
unsigned char b5:1;
unsigned char b6:1;
unsigned char b7:1;
};
} in;
adwsystems:
and still be able to address the bits indirectly (similar to an array index)?
This won't meet that requirement.
adwsystems:
This won't meet that requirement.
not sure what you mean there... I could set/ clear each bit of the char and also read them independently...
could you clarify what is the requirement not met for you?
sherzaad:
not sure what you mean there... I could set/ clear each bit of the char and also read them independently...
could you clarify what is the requirement not met for you?
you cannot use a variable / programmatic subscript with your method
sherzaad:
not sure what you mean there... I could set/ clear each bit of the char and also read them independently...
could you clarify what is the requirement not met for you?
The ability to loop through the bits. Indirect addressing. in.b0 is a direct address. in[x ] would be a indirect address.
bitRead() and bitWrite() provide an round about way of indirect addressing as the bit parameter of the function call can be x. That will only work to 32 bits (unsigned long).
(also what TolpuddleSartre said)
Would a construct like "myBitarray.read (x);" fulfill your requirement?
It may even be able to overload the [] operator.
TolpuddleSartre:
Would a construct like "myBitarray.read (x);" fulfill your requirement?
It may even be able to overload the [] operator.
At first glance, yes. But I think I need more details and/or information (C programmer for 30 years, C++ not so much).
You could just as easily write it in C, you just wouldn't be able to overload the [] operator.
Divide your index by eight.
This gives you the byte index.
The modulo eight of the index gives you the bit index.
Fetch the byte using the byte index and use bitRead or bitWrite on the byte, using the bit index.