Suppose I have the following 2 structures based on 2 bytes:
#pragma pack(1)
struct Foo {
unsigned short a : 5;
unsigned short b : 4;
unsigned short c : 2;
unsigned short d : 5;
} ;
struct Bar {
unsigned short a : 5;
unsigned short b : 4;
unsigned short c : 2;
unsigned short d : 3;
unsigned short e : 2;
} ;
And I have a union that contains them:
union Baz {
unsigned short val;
struct Foo foo;
struct Bar bar;
} ;
And then in my program, I can put the value using val and get the values a, b, c, d and e according to their bit fields, without bitwise operations / interfaces and more necessary.
The problem is that I need it to support both large and small endianness, which means that I need my structure to determine the bit fields according to the content at compile time.
So I need something like this:
#pragma pack(1)
#if BIG_ENDIAN
struct Foo {
unsigned short a : 5;
unsigned short b : 4;
unsigned short c : 2;
unsigned short d : 5;
} ;
struct Bar {
unsigned short a : 5;
unsigned short b : 4;
unsigned short c : 2;
unsigned short d : 3;
unsigned short e : 2;
} ;
#else
struct Foo {
unsigned short d : 5;
unsigned short c : 2;
unsigned short b : 4;
unsigned short a : 5;
} ;
struct Bar {
unsigned short e : 2;
unsigned short d : 3;
unsigned short c : 2;
unsigned short b : 4;
unsigned short a : 5;
} ;
#endif
, , , , , .
, , BYTE_ORDER, LITTLE_ORDER, BIG_ORDER .., , , endian.h. , , boosted.hpp , , , - .
?
edit1:
: ++ 03, ++ 11/14 .