B
benn686
Id like to group 16 booleans into a u16 such that I can either set all
16 variables at once with just a single u16 assigment, or I can
individually change a bit by setting that boolean manually. Either
way, the same 16 bits is occupied in memory.
Something like...
typedef struct
{
bool bSetLed15;
bool bSetLed15;
bool bSetLed14;
bool bSetLed13;
bool bSetLed12;
bool bSetLed11;
bool bSetLed10;
bool bSetLed09;
bool bSetLed08;
bool bSetLed07;
bool bSetLed06;
bool bSetLed05;
bool bSetLed04;
bool bSetLed03;
bool bSetLed02;
bool bSetLed01;
bool bSetLed00;
} sLEDS;
typedef struct
{
union
{
alt_u16 uAllLeds;
sLEDS sSetSingleLed;
} uSelection;
} CONTROL;
The problem is when I set the 16 bit variable, uAllLeds, the
individual bits in the sLEDs structure (sSetSingleLed) arent set
correctly. For example, ideally uAllLeds = 0x0300; would also set
bSetLed09 = 1 and bSetLed08 = 1, with all other values to 0.
Alternatively, if all leds are 0, and I set bSetLed15 = 1, I would
like uAllLeds to automatically change to 0x8000.
Is what Im trying to do possible using Union?
16 variables at once with just a single u16 assigment, or I can
individually change a bit by setting that boolean manually. Either
way, the same 16 bits is occupied in memory.
Something like...
typedef struct
{
bool bSetLed15;
bool bSetLed15;
bool bSetLed14;
bool bSetLed13;
bool bSetLed12;
bool bSetLed11;
bool bSetLed10;
bool bSetLed09;
bool bSetLed08;
bool bSetLed07;
bool bSetLed06;
bool bSetLed05;
bool bSetLed04;
bool bSetLed03;
bool bSetLed02;
bool bSetLed01;
bool bSetLed00;
} sLEDS;
typedef struct
{
union
{
alt_u16 uAllLeds;
sLEDS sSetSingleLed;
} uSelection;
} CONTROL;
The problem is when I set the 16 bit variable, uAllLeds, the
individual bits in the sLEDs structure (sSetSingleLed) arent set
correctly. For example, ideally uAllLeds = 0x0300; would also set
bSetLed09 = 1 and bSetLed08 = 1, with all other values to 0.
Alternatively, if all leds are 0, and I set bSetLed15 = 1, I would
like uAllLeds to automatically change to 0x8000.
Is what Im trying to do possible using Union?