Bit fields struct assignment unexpected behaviour

后端 未结 2 419
天命终不由人
天命终不由人 2021-01-22 05:12

I don\'t know why but the bit fields assignment is not working as expected. Probably is just an stupid thing, but I\'ve not been able to locate the problem.

Any help is

2条回答
  •  误落风尘
    2021-01-22 06:02

    Nearly everything about bit-fields is implementation defined. And particularly the order of bits in a unit.

    (C99, 6.7.2.1p10) "The order of allocation of bit-fields within a unit (high-order to low-order or low-order to high-order) is implementation-defined."

    On your implementation, the bits are stored in a unit lsb (least significant bit) first and not msb (most significant bit) first as you would expect.

    What you have is:

    [a1.0] [a2.0] [a2.1] [a2.2] [a2.0] [a3.1] [a4.0] [a4.1] 
       0      0      1      0      1      0      0      1
     bit 0                     -                      bit 7
    
     lsb                       -                      msb
    

    which is 0x94 if you consider the left most bit is the least significant bit.

提交回复
热议问题