enums and bit fields

Discussion in 'C Programming' started by Randy Budd, Jun 22, 2010.

  1. Randy Budd

    Randy Budd Guest

    can I use enums for a bit field (as opposed to an int)?

    is it valid at all?

    is it undefined across platforms? (i.e. not very portable)

    is it ok as long as the enums don't overrun the bitfield?

    is an enum signed or unsigned?

    EX

    typedef enum {
    TYPE_A, TYPE_B, TYPE_C,
    } Type;

    typedef struct {
    unsigned int aType:5;
    Type bType:5; /* is this O.K. ? */
    unsigned int aFlag:1;
    } Struct;

    main()
    {
    Struct a;

    a.aType = TYPE_B;
    a.bType = TYPE_C;

    ....

    }
    Randy Budd, Jun 22, 2010
    #1
    1. Advertising

  2. Randy Budd <> writes:
    > can I use enums for a bit field (as opposed to an int)?


    Not portably. C99 6.7.2.1p4:

    A bit-field shall have a type that is a qualified or unqualified
    version of _Bool, signed int, unsigned int, or some other
    implementation-defined type.

    > is it valid at all?
    >
    > is it undefined across platforms? (i.e. not very portable)
    >
    > is it ok as long as the enums don't overrun the bitfield?
    >
    > is an enum signed or unsigned?


    These are all implementation-defined.

    [...]

    > main()


    Make this "int main(void)".

    [...]

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 22, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    1,720
    Timothy Bendfelt
    Jan 19, 2007
  2. =?utf-8?b?QXNiasO4cm4gU8OmYsO4?=

    Enums without identifier, enums and typedef

    =?utf-8?b?QXNiasO4cm4gU8OmYsO4?=, Jan 19, 2007, in forum: C Programming
    Replies:
    10
    Views:
    1,102
    Keith Thompson
    Jan 20, 2007
  3. Replies:
    9
    Views:
    941
    Juha Nieminen
    Aug 22, 2007
  4. Mike -- Email Ignored

    Bit Order in Bit Fields

    Mike -- Email Ignored, May 2, 2009, in forum: C++
    Replies:
    0
    Views:
    367
    Mike -- Email Ignored
    May 2, 2009
  5. Jeff.M
    Replies:
    6
    Views:
    163
    Lasse Reichstein Nielsen
    May 4, 2009
Loading...

Share This Page