In c / c++, we can define a variable with 1 bit in memory: like unsigned char value : 1;
Is there a way to declare an array of 1 bit elements? like in sudo code below:
unsigned char : 1 data[10];
In c / c++, we can define a variable with 1 bit in memory: like unsigned char value : 1;
Is there a way to declare an array of 1 bit elements? like in sudo code below:
unsigned char : 1 data[10];
The problem is that in most implementations, the 1 bit variable will still occupy 1 byte of memory because that's the way the memory is addressed. If you have a big array of such values, however, then you can work around that. One such solution is std::bitset. You can make one like this:
#include <bitset>
std::bitset<64> data;
You can manipulate bits using the set, reset and flip operations (setting it to 1, 0 or toggling it). You can access a bit using [], for instance
if (data[5]) { ...
Check out a nice example here.
unsigned int a: 1;) into one struct and if you're lucky, the compiler will optimize it properly. You can then use those structs to make your own bitset. I wouldn't recommend it, however.