Explain, please, how is the structure created here? What does the colon do:?

 /** * @brief Bit-field structure of the state of the packet reception */ typedef struct{ uint32_t Length :16; /*!< The number of bytes in the packet including header and CRC. */ uint32_t PF_ERR :1; /*!< A sign package PAUSE. */ uint32_t CF_ERR :1; /*!< A sign Management Pack (filtering by MAC and special tags in the field length - 13.14 - octets). */ uint32_t LF_ERR :1; /*!< A sign excess packet length 1518 octets. */ uint32_t SF_ERR :1; /*!< A sign of lack of packet length 64 octets. */ uint32_t LEN_ERR :1; /*!< A sign mismatch between the actual length and the length specified in the length field - 13.14 octets. */ uint32_t DN_ERR :1; /*!< A sign bit of the packet is not a multiple of 8. */ uint32_t CRC_ERR :1; /*!< A sign mismatch packet CRC. */ uint32_t SMB_ERR :1; /*!< A sign of the presence in the packet error nibbles. */ uint32_t MCA :1; /*!< A sign group package (MAC matches HASH). */ uint32_t BCA :1; /*!< A sign of the broadcast packet (MAC = FF:FF:FF:FF:FF:FF). */ uint32_t UCA :1; /*!< A sign individual package (MAC corresponds to the set). */ }ETH_StatusPacketReceptionBitFileds; 

    2 answers 2

    The name of the ETH_StatusPacketReception BitFileds structure itself contains the BitFileds phrase. This structure specifies bit fields, that is, a more compact form of recording the integer data members of the structure, since it is known in advance that these data members will store bounded values, and it’s enough to select several bits to represent them.

    For example, you could define this structure as follows:

     typedef struct{ uint32_t Length; /*!< The number of bytes in the packet including header and CRC. */ uint32_t PF_ERR; /*!< A sign package PAUSE. */ //... }ETH_StatusPacketReceptionBitFileds; 

    But in this case, each member of the data structure would occupy a memory of 32 bits, or 4 bytes. And if it is known, for example, that the data member of the PF_ERR structure can take only two values ​​0 or 1, then it will be wasteful to store these values ​​in a data member that has 32 bits, since only one bit is enough to represent 0 or 1. Therefore, a structure is defined as a structure with bit fields with a specified number of bits. These bit fields are packed by the compiler into objects, as specified in the declaration of the bit fields of the structure, of type uint32_t . That is, in principle, 32 bit fields with a size of 1 bit can be packed in one object of this type. This saves memory allocated for objects of the structure.

    From the C ++ standard (9.6 Bit-fields [class.bit])

    1 A member-declarator of the form

     identifieropt attribute-specifier-seqopt: constant-expression 

    specifies a bit-field; its length is set to a bit.

    • one
      No, uint32_t is not the type in which the field is packed, but the type of the temporary variable in which the field is unpacked during operations with it. I generally doubt that an object in which the fields are packed is associated with any type (at least the type available in the code) - avp
    • @avp You can send your report of a standard defect to the standardization committee. Actually type plays a role. For example, the size of the structure, the sign, the possible additions to the structure, how bits will be interpreted: as signed or unsigned. - Vlad from Moscow
    • What am I talking about? We always indicate the type of field and the number of its low-order bits , which are packaged in a container, and not the type of container (container fields) in which the field is packed. And just in case, it will not work to make the size of the field larger than the size of its type. - avp

    After the colon, the member of the structure is set to the size of this member in bits. Those. such member is a bit field. This is mainly used for data packaging and at the same time preserving convenient change access and reading values. At the same time, the operation of taking an address is prohibited for a bit field.

    Actually, the comment to the structure already contains the answer:

     Bit-field structure ...