Following snippet from my zigbee wsndemo code is giving me hard time to understand the structure. I went through many structure related articles online but couldnt understand how these structure variables are define and can be used. Please help.
static struct
{
uint8_t appSubTaskPosted : 1;
uint8_t appCmdHandlerTaskPosted : 1;
uint8_t appMsgSenderTaskPosted : 1;
} appTaskFlags =
{
.appSubTaskPosted = false,
.appCmdHandlerTaskPosted = false,
.appMsgSenderTaskPosted = false
};
They're bitfields, which in this case have 1 bit. They can only have the values 0 or 1, true or false. They take less memory than a bool
individually.
You can also define bitfields greater than 1. For example, a bitfield of 2 can have the values 0, 1, 2, and 3. The beauty of bitfields is you don't need an offset to access them which you would have to do if you were using a larger data type's individual bits.
If you're defining a structure with bitfields, define them right next to each other because bitfields like this actually share a larger data type, such as an int 32.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With