Search code examples
cstructembeddedstm32bit-fields

What is : operator in Embedded C


In a project of STM32, I came through a code like this :

typedef union {
    struct __attribute__ ((packed)){        
        uint8_t ModePin0 :1;                
        uint8_t ModePin1 :1;
        uint8_t ModePin2 :1;
        uint8_t ModePin3 :1;
    } dmxModeBytes;
    uint16_t dmxMode;
} dmxModeUnion;

So, my question is what is the meaning of :1 after ModePin0 variable and for similar variables? Is it related to memory alignment?


Solution

  • It is not an operator it is a declaration of a bit field. The number after the sign ':' specifies the number of bits (the width) in the bit field.

    From the C Standard (6.7.2.1 Structure and union specifiers)

    9 A member of a structure or union may have any complete object type other than a variably modified type.123) In addition, a member may be declared to consist of a specified number of bits (including a sign bit, if any). Such a member is called a bit-field; 124) its width is preceded by a colon.