This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Bit field order in C166 compiler

Hi,

I am new to C166 compiler. I would like to know how is the order of the bit field in a structure. i.e. the first member would be a LSB or MSB.

Thanks in anticipation.

Viral.

Parents
  • Hi.
    Compile a program like this one:

    struct bitfield
    {
        unsigned m0 : 1; unsigned m1 : 1; unsigned m2 : 1; unsigned m3 : 1;
        unsigned m4 : 1; unsigned m5 : 1; unsigned m6 : 1; unsigned m7 : 1;
        unsigned m8 : 1; unsigned m9 : 1; unsigned m10 : 1; unsigned m11 : 1;
        unsigned m12 : 1; unsigned m13 : 1; unsigned m14 : 1; unsigned m15 : 1;
    };
    
    union word
    {
        struct bitfield bf;
        unsigned i;
    } w;
    
    void main(void)
    {
        w.bf.m0 = 1;
        for (;;) ;
    }
    
    and run it in simulator. You will see that after the assignment w.bf.m0 = 1 the integer representation of the bitfield will be 1 (LSB).

    Regards,
    - mike

Reply
  • Hi.
    Compile a program like this one:

    struct bitfield
    {
        unsigned m0 : 1; unsigned m1 : 1; unsigned m2 : 1; unsigned m3 : 1;
        unsigned m4 : 1; unsigned m5 : 1; unsigned m6 : 1; unsigned m7 : 1;
        unsigned m8 : 1; unsigned m9 : 1; unsigned m10 : 1; unsigned m11 : 1;
        unsigned m12 : 1; unsigned m13 : 1; unsigned m14 : 1; unsigned m15 : 1;
    };
    
    union word
    {
        struct bitfield bf;
        unsigned i;
    } w;
    
    void main(void)
    {
        w.bf.m0 = 1;
        for (;;) ;
    }
    
    and run it in simulator. You will see that after the assignment w.bf.m0 = 1 the integer representation of the bitfield will be 1 (LSB).

    Regards,
    - mike

Children
  • but that just shows you how the compiler happened to compile that particular piece of code on that particular occasion - you have no guarantee that it will always do it that way under all conditions.

    For the authoritative statement, you need to consult the Manual - and if you upgrade, you will have to re-check the new Manual as the compiler is under no obligation to retain the same implementation.

    Bitfields are notoriously quirky, and may not even be particularly efficient. You might find that it's just as well to do your own masking, etc