Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedded c programming

Tags:

c

embedded

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
        };
like image 317
DarshanJoshi Avatar asked Dec 19 '13 10:12

DarshanJoshi


1 Answers

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.

like image 173
Fiddling Bits Avatar answered Sep 20 '22 12:09

Fiddling Bits