0

I came across a problem today where I discovered the way the bit fields in my bytes are ordered is dependent on the endianness of my processor. Take the next example:

struct S {
    uint8_t a : 3;
    uint8_t b : 5;
};

This struct takes one byte but the bit layout depends on the machine:

  • Little endian: b4 b3 b2 b1 b0 a2 a1 a0
  • Big endian: a2 a1 a0 b4 b3 b2 b1

So on a little endian machine it starts filling from the LSB and on a big endian machine it start filling from the MSB. I once heard Stroustrup say it's a main goal to achieve portability across platforms but leaving some things like this is not portable at all. If I were to send this struct over a connection to someone, how would he know which bits mapped to which fields in the struct? Would it not have been easier if the order was fixed? What's the reasoning behind the choice of leaving this open to the processor and compiler? The only safe option is to use bit shifts and masks which uses a lot more code. It would have been so much easier if me and my co-workers could have counted on a fixed order, the little endian way for example, but there certainly must have been a reason why chose not to do it.

17
  • 2
    Probably has nothing to do with the endianness of the processor and everything to do with an arbitrary decision by the compiler writers. Which compilers did you observe this with? Nov 30, 2017 at 23:08
  • 1
    @MarkRansom His question is about why the language leaves the decision to compiler writers in the first place.
    – Barmar
    Nov 30, 2017 at 23:12
  • 1
    C++ doesn't dictate memory layouts in regards to padding either, so your over-the-wire argument doesn't hold.
    – kmdreko
    Nov 30, 2017 at 23:13
  • 1
    No matter where you ask, you'll just get speculation. Gotta ask Stroustrup.
    – Barmar
    Nov 30, 2017 at 23:19
  • 1
    The rules in C are like that; C++ inherited the rules from C. And the rules in C are like that because the state of the world (of compilers) when it was standardized was as chaotic as the rules suggest. The C89/C90 standard committee tried to avoid specifying the standard in a way that meant that working compilers could not be compatible with it without breaking the code that their users had written for it. Dec 1, 2017 at 0:08

1 Answer 1

1

They're not defined by the language so that it can accommodate the exact situation you've discovered. For bit fields in a byte it makes no difference, but bit fields can occupy larger integer types. Consider this example:

struct bf
{
    uint16_t a : 5;
    uint16_t b : 11;
};

In order for the bits of a field to be adjacent in memory, the bits in a byte must be laid out differently depending on the endianness of the processor.

Big endian:

aaaaabbb bbbbbbbb

Little endian:

bbbbbbbb bbbaaaaa
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.