Alrighty.... give this a try....
Code:
#include <stdio.h>
#include <stdlib.h>
typedef union t_var
{ unsigned char MyByte;
struct t_bits
{ unsigned char b0 : 1,
b1 : 1,
b2 : 1,
b3 : 1,
b4 : 1,
b5 : 1,
b6 : 1,
b7 : 1; } bits; }
tvar;
int main(void)
{ tvar testvar;
testvar.MyByte = 56;
printf("%d%d%d%d %d%d%d%d = %d \n",
testvar.bits.b7,testvar.bits.b6,testvar.bits.b5,testvar.bits.b4,
testvar.bits.b3,testvar.bits.b2,testvar.bits.b1,testvar.bits.b0,
testvar.MyByte);
// output : 0011 1000 = 56
testvar.MyByte = 0; // clear the bits
testvar.bits.b2 = 1;
printf("We got %d\n\n",testvar.MyByte); // = 4
testvar.bits.b4 = 1;
printf("We got %d\n\n",testvar.MyByte); // = 20
return EXIT_SUCCESS;
}
The problem appears to be that without the nested struct, the union was laying all the bits on top of each othe as well....