Bit manipulations good practices

后端 未结 7 536
情书的邮戳
情书的邮戳 2021-02-03 20:28

As a beginner C programmer, I am wondering, what would be the best easy-to-read and easy-to-understand solution for setting control bits in a device. Are there any standards

7条回答
  •  情书的邮戳
    2021-02-03 20:58

    The problem with bit fields is that the C standard does not dictate that the order in which they are defined is the same as the order that they are implemented. So you may not be setting the bits you think you are.

    Section 6.7.2.1p11 of the C standard states:

    An implementation may allocate any addressable storage unit large enough to hold a bit- field. If enough space remains, a bit-field that immediately follows another bit-field in a structure shall be packed into adjacent bits of the same unit. If insufficient space remains, whether a bit-field that does not fit is put into the next unit or overlaps adjacent units is implementation-defined. The order of allocation of bit-fields within a unit (high-order to low-order or low-order to high-order) is implementation-defined. The alignment of the addressable storage unit is unspecified.

    As an example, look at the definition of struct iphdr, which represents an IP header, from the /usr/include/netinet/ip.h file file on Linux:

    struct iphdr
      {
    #if __BYTE_ORDER == __LITTLE_ENDIAN
        unsigned int ihl:4;
        unsigned int version:4;
    #elif __BYTE_ORDER == __BIG_ENDIAN
        unsigned int version:4;
        unsigned int ihl:4;
    #else
    # error "Please fix "
    #endif
        u_int8_t tos;
        ...
    

    You can see here that the bitfields are placed in a different order depending on the implementation. You also shouldn't use this specific check because this behavior is system dependent. It is acceptable for this file because it is part of the system. Other systems may implement this in different ways.

    So don't use a bitfield.

    The best way to do this is to set the required bits. However, it would make sense to define named constants for each bit and to perform a bitwise OR of the constants you want to set. For example:

    const uint8_t BIT_BYTE =     0x1;
    const uint8_t BIT_HW   =     0x2;
    const uint8_t BIT_WORD =     0x4;
    const uint8_t BIT_GO   =     0x8;
    const uint8_t BIT_I_EN =     0x10;
    const uint8_t BIT_REEN =     0x20;
    const uint8_t BIT_WEEN =     0x40;
    const uint8_t BIT_LEEN =     0x80;
    
    DMA_base_ptr[DMA_CONTROL_OFFS] = BIT_LEEN | BIT_GO | BIT_WORD;
    

提交回复
热议问题