Bit manipulations good practices

后端 未结 7 539
情书的邮戳
情书的邮戳 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 21:09

    There is no standard for bit fields. Mapping and bit operation are dependent on compiler in this case. Binary values such as 0b0000 are not standardized also. Usual way to do is defining hexadecimal values for each bit. For example:

    #define BYTE (0x01)
    #define HW   (0x02)
    /*etc*/
    

    When you want to set bits, you can use:

    DMA_base_ptr[DMA_CONTROL_OFFS] |= HW;
    

    Or you can clear bits with:

    DMA_base_ptr[DMA_CONTROL_OFFS] &= ~HW;
    
    0 讨论(0)
提交回复
热议问题