1

I am trying to get 8x8-bits values from a 64 bits structure and store them into an array.

I already did that by accessing the struct members, shifting bits and OR operation. However I thought about using the memcpy function. I am not sure it is reliable. If I understood correctly, there is no padding in my structure since it is padded on a 64 bits basis and my structure is 64 bits length. However, I have not the expected result when I do the copy.

typedef unsigned long long uint64_t;
typedef unsigned char uint8_t;

typedef struct frameHeader_t
    {
        uint64_t encryption       : 2;
        uint64_t frameVersion     : 2;
        uint64_t probeType        : 4;
        uint64_t dataType         : 5;
        uint64_t measurePeriod    : 3;
        uint64_t remontePeriod    : 4;
        uint64_t nbrMeasure       : 2;
        uint64_t year             : 7;
        uint64_t month            : 4;
        uint64_t day              : 5;
        uint64_t hour             : 5;
        uint64_t minute           : 6;
        uint64_t second           : 6;
        uint64_t randomization    : 5;
        uint64_t status           : 4;

    }FrameHeader;

int main()
{
    //declarations
    int i;
    uint8_t test[8];
    FrameHeader frameHeader;

    //put value
    frameHeader.encryption = 2;
    frameHeader.frameVersion = 0;
    frameHeader.probeType = 0;
    frameHeader.dataType = 1;
    frameHeader.measurePeriod = 0;
    frameHeader.remontePeriod = 6;
    frameHeader.nbrMeasure = 1;
    frameHeader.year = 19;
    frameHeader.month = 6;
    frameHeader.day = 25;
    frameHeader.hour = 15;
    frameHeader.minute = 49;
    frameHeader.second = 00;
    frameHeader.randomization = 15;
    frameHeader.status = 0;

    //copy
    memcpy(test, &frameHeader, 8);

    //print result
    for(i=0;i<sizeof(test);i++)
    {
        printf("%02x ", test[i]);
    }

    return 0;
}

I expected : 80 08 64 9B 65 F8 80 F0

I got : 02 01 D6 C4 F2 8B 81 07

Clément
  • 1,128
  • 7
  • 21
  • 2
    [This SO question](https://stackoverflow.com/questions/47600584/bitfield-endianness-in-gcc) might be worth reading. And so is [this article](http://mjfrazer.org/mjfrazer/bitfields/) – Jabberwocky Jun 27 '19 at 09:40
  • 1
    The `memcpy` function is reliable, it is your expectations that are unreliable. – Ian Abbott Jun 27 '19 at 09:54
  • @Jabberwocky Really interesting. I am trying to figure out the best way to deal with it as it is described at the end of the article. – Clément Jun 27 '19 at 10:07
  • 1
    [C11 6.7.2.1 paragraph 11](http://www.iso-9899.info/n1570.html#6.7.2.1p11) states that the order of allocation of bit-fields within a unit (high-to-low or low-to-high) is implementation defined. That's also the case for the latest C standard (C18). The two main things that affect the representation of bit-fields are the "endianness" of the representation of normal integer types, and the packing order of bit-fields. Knowing one does not determine the other. – Ian Abbott Jun 27 '19 at 10:27

0 Answers0