13

I was wondering if there is a GCC C Compiler directive that allows me to determine the bit order for packing of a structure? Something to the likes of:

#pragma bit_order left

The rationale for such a need is that I have the following structure:

struct {
       union {
             unsigned char BYTE;
             struct {
                 unsigned char B0: 1;
                 unsigned char B1: 1;
                 unsigned char B2: 1;
                 unsigned char B3: 1;
                 unsigned char B4: 4;
             }BIT;
       }ITEM;
} myStruct;

With this structure, I would like the compiler to pack it this way:

Bit order: | 7  6  5  4  3  2  1  0 |
Label:     |B0 B1 B2 B3 B4 B5 B6 B7 |

Rather than how GCC does it:

Bit order: | 7  6  5  4  3  2  1  0 |
Label:     |B7 B6 B5 B4 B3 B2 B1 B0 |

I am dealing with MCUs that have huge header files that have structures that compute bit offsets according to stipulated hardware addresses. I am hoping that there is a compiler directive in GCC C Compiler that does the bit order swap for me before I attempt the flip all the fields in the manufacturer supplied file.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Vern
  • 2,393
  • 1
  • 15
  • 18
  • size and order and compiler superset https://stackoverflow.com/questions/1490092/c-c-force-bit-field-order-and-alignment does `packed` work subset: https://stackoverflow.com/questions/1756811/does-gccs-attribute-packed-retain-the-original-ordering – Ciro Santilli OurBigBook.com Jul 30 '17 at 11:05

1 Answers1

17

Which version of GCC are you using and which platform? A pragma exists that may do the trick, but it doesn't work on x86 starting with GCC 4.

#pragma reverse_bitfields on

More details at:

http://groups.google.com/group/gnu.gcc.help/browse_thread/thread/747918655affa5c0?pli=1

If you don't mind rebuilding GCC, all the relevant build settings are here (search for bitfield):

http://gcc.gnu.org/onlinedocs/gccint/Storage-Layout.html

Some details about bitfields being bad:

C/C++: Force Bit Field Order and Alignment

Community
  • 1
  • 1
kichik
  • 33,220
  • 7
  • 94
  • 114
  • 2
    Thanks alot mate! That was really informative! The branch that they took the GCC off was version 4.3.0. The documentation of the GCC C Compiler that I have for that target MCU is pretty sparse and I'm trying to make the best out of it. Looks like the only way out is to re-order the structs myself. Thanks once again for helping me expedite the process of arriving at a feasible solution :) – Vern Jul 18 '11 at 07:45