For starters, you really should not use unions this way because the standard doesn't guarantee it will actually work. You should only ever access the last field you have written to:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
union foo
{
int a;
short b;
};
foo f;
f.a = 5;
cout << f.a; // OK, f.a was the last one written to
cout << f.b; // BAD
f.b = 2;
cout << f.a; // BAD
cout << f.b; // OK
|
Also, bools are not 1 bit wide, so an array of 8 of them will likely be larger than 1 byte.
There is no [blatantly easy] way to get individual bits of a value like this from a union. You
could use bitfields, but that'd be a mess:
1 2 3 4 5 6 7 8 9 10 11 12
|
union
{
unsigned long v;
struct
{
unsigned bit0:1;
unsigned bit1:1;
unsigned bit2:1;
unsigned bit3:1;
//...
};
};
|
But of course, whether or not bit0 is actually bit0 depends on several factors:
1) Whether your compiler decides to make unions work this way (most compilers do, but again it's not guaranteed)
2) The endianness of your system (little endian will have a different result from big endian).