From: Christoph Bartoschek on
Hi,

does POSIX guarantee anything about bitfield layout:

union Test {
unsigned char a;
struct Data {
unsigned first:4;
unsigned second:3;
unsigned third:1;
} data;
};

If I assign 0xF1 to a. Can one assume that first has the value 0xF, second
the value 0x0 and third the value 0x1.

Thanks
Christoph Bartoschek
From: Måns Rullgård on
Christoph Bartoschek <bartoschek(a)gmx.de> writes:

> Hi,
>
> does POSIX guarantee anything about bitfield layout:

Very little.

> union Test {
> unsigned char a;
> struct Data {
> unsigned first:4;
> unsigned second:3;
> unsigned third:1;
> } data;
> };
>
> If I assign 0xF1 to a. Can one assume that first has the value 0xF, second
> the value 0x0 and third the value 0x1.

No. To be on the safe side, don't assume anything at all.

--
M�ns Rullg�rd
mans(a)mansr.com
From: David Schwartz on
On Apr 19, 6:53 am, Christoph Bartoschek <bartosc...(a)gmx.de> wrote:
> Hi,
>
> does POSIX guarantee anything about bitfield layout:
>
> union Test {
> unsigned char a;
> struct Data {
> unsigned first:4;
> unsigned second:3;
> unsigned third:1;
> } data;
>
> };
>
> If I assign 0xF1 to a. Can one assume that first has the value 0xF, second
> the value 0x0 and third the value 0x1.

No. The effect of reading from an entry in a union other than the one
you last wrote to is undefined.

DS