0

How do I determine the order of a bitfield? I can change an element of a bitfield by doing x.c where x is a variable of the struct type. I have tried to bitmask it and print it out, but I can only print each element in the struct and not the entire variable. I have tried to assign a pointer to the elements of a bitfield but it comes up with an error. What is the best way to determine the order of a bitfield for a C compiler? For example which order would this struct be represented as?

struct bit_fields
{
unsigned int a : 6,
             b : 8,
             c : 18;
}
  • 1
    Prepare an instance with well-recognizable bit patterns for each field, `memcpy` it to a suitably sized `unsigned char` array and see where each field has gone. – Matteo Italia Aug 16 '17 at 13:10

1 Answers1

0

According to C11, chapter §6.7.2.1/P11

[....] 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.

The best way to know, is to check your compiler documentation and the ABI on your platform.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Sorry to sound like a complete beginner but what is ABI? And where can I check it? – danielwestfall Aug 16 '17 at 13:30
  • @danielwestfall https://en.wikipedia.org/wiki/Application_binary_interface – Bob__ Aug 16 '17 at 13:33
  • @danielwestfall The ABI is the "application binary interface", a low-level framework for your specific system that specifies how things are allocated in memory, among other things. – Lundin Aug 16 '17 at 13:33