Page 1 of 1

Union/Struct/Bits

Posted: Wed Mar 21, 2018 5:18 am
by DHembree
In previous programming exploits I have used the following sample of code to manipulate individual bits in a byte-sized variable that would subsequently be written to a device (i2c):

union User_Switches_A
{
struct {
bool IN1_Sw :1; //
bool IN2_Sw :1; //
bool IN3_Sw :1; //
bool MON1_Sw :1; //
bool MON2_Sw :1; //
bool MON3_Sw :1; //
bool MUTE_L :1; //
bool MUTE_R :1; //
}Bits;
byte Byte;
} Switches_A;

The Xtime compiler does not like this. Is there any way to alter this to make it compatible with XC, or is there another method I can use to do the same task?

Thanks!
Dan

Re: Union/Struct/Bits

Posted: Wed Mar 21, 2018 9:17 am
by robertxmos
Hi Dan,

Unfortunately for you the XC language does not support bit-fields or a 1-bit boolean type.
The work around is either to write your bit-field code in C/C++
or manually write access functions (which use bit wise operators).

The access functions may be in-lined, and hence produce similar code to if you had been allowed to express your intent directly.
Sorry for the extra layer of complexity that is necessary with both suggestions... unwelcome encapsulation & abstraction!

robert