Guarantees regarding bitfield layout
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
Re: Guarantees regarding bitfield layout
Christoph Bartoschek <bartoschek@gmx.de> writes:
[color=blue]
> Hi,
>
> does POSIX guarantee anything about bitfield layout:[/color]
Very little.
[color=blue]
> 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.[/color]
No. To be on the safe side, don't assume anything at all.
--
Måns Rullgård
[email]mans@mansr.com[/email]
Re: Guarantees regarding bitfield layout
On Apr 19, 6:53 am, Christoph Bartoschek <bartosc...@gmx.de> wrote:[color=blue]
> 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.[/color]
No. The effect of reading from an entry in a union other than the one
you last wrote to is undefined.
DS