Guarantees regarding bitfield layout - Unix

This is a discussion on Guarantees regarding bitfield layout - Unix ; 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 ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Guarantees regarding bitfield layout

  1. 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

  2. Re: Guarantees regarding bitfield layout

    Christoph Bartoschek 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@mansr.com

  3. Re: Guarantees regarding bitfield layout

    On Apr 19, 6:53 am, Christoph Bartoschek 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

+ Reply to Thread