bitfield and enum, is this legal?

Discussion in 'C Programming' started by Erik Cato, Feb 23, 2004.

  1. Erik Cato

    Erik Cato Guest

    Hi group!

    Is this code legal?

    typedef enum
    {
    FALSE = 0,
    TRUE = 1,
    }t_bool;

    typedef struct
    {
    t_bool b0 : 1,
    b1 : 1,
    b2 : 1,
    b3 : 1,
    b4 : 1,
    b5 : 1,
    b6 : 1,
    b7 : 1;
    }t_bitfield8;

    //Erik
    Erik Cato, Feb 23, 2004
    #1
    1. Advertising

  2. Erik Cato

    Ben Pfaff Guest

    (Erik Cato) writes:

    > Is this code legal?
    >
    > typedef enum
    > {
    > FALSE = 0,
    > TRUE = 1,
    > }t_bool;
    >
    > typedef struct
    > {
    > t_bool b0 : 1,
    > b1 : 1,
    > b2 : 1,
    > b3 : 1,
    > b4 : 1,
    > b5 : 1,
    > b6 : 1,
    > b7 : 1;
    > }t_bitfield8;


    It's not portable. Bit-fields can only portably have type signed
    int or unsigned int.
    --
    "This is a wonderful answer.
    It's off-topic, it's incorrect, and it doesn't answer the question."
    --Richard Heathfield
    Ben Pfaff, Feb 23, 2004
    #2
    1. Advertising

  3. Ben Pfaff <> wrote in message news:<>...
    > (Erik Cato) writes:
    >
    > > Is this code legal?
    > >
    > > typedef enum
    > > {
    > > FALSE = 0,
    > > TRUE = 1,
    > > }t_bool;
    > >
    > > typedef struct
    > > {
    > > t_bool b0 : 1,
    > > b1 : 1,
    > > b2 : 1,
    > > b3 : 1,
    > > b4 : 1,
    > > b5 : 1,
    > > b6 : 1,
    > > b7 : 1;
    > > }t_bitfield8;

    >
    > It's not portable. Bit-fields can only portably have type signed
    > int or unsigned int.


    Or _Bool in C99.

    Note that 'plain' int is also allowed, but it is implementation
    defined as to whether this behaves as signed or unsigned int in the
    context of bitfields.

    --
    Peter
    Peter Nilsson, Feb 23, 2004
    #3
  4. Erik Cato

    Ben Pfaff Guest

    (Peter Nilsson) writes:

    > Ben Pfaff <> wrote in message news:<>...
    >> It's not portable. Bit-fields can only portably have type signed
    >> int or unsigned int.

    >
    > Or _Bool in C99.


    I thought about adding that, but then I decided that C99 was also
    not portable :)
    Ben Pfaff, Feb 24, 2004
    #4
  5. Erik Cato

    Jack Klein Guest

    On Mon, 23 Feb 2004 09:46:36 -0800, Ben Pfaff <>
    wrote in comp.lang.c:

    > (Erik Cato) writes:
    >
    > > Is this code legal?
    > >
    > > typedef enum
    > > {
    > > FALSE = 0,
    > > TRUE = 1,
    > > }t_bool;
    > >
    > > typedef struct
    > > {
    > > t_bool b0 : 1,
    > > b1 : 1,
    > > b2 : 1,
    > > b3 : 1,
    > > b4 : 1,
    > > b5 : 1,
    > > b6 : 1,
    > > b7 : 1;
    > > }t_bitfield8;

    >
    > It's not portable. Bit-fields can only portably have type signed
    > int or unsigned int.


    No, it is undefined behavior under any version of the standard prior
    to C99. Semantics section of 6.5.2.1 of C90/95:

    <quote>
    A bit-field shall have a type that is a qualified or unqualified
    version of one of int, unsigned int, or signed int. Whether the
    high-order bit position of a (possibly qualified) “plain” int
    bit-field is treated as a sign bit is implementation-defined. A
    bit-field is interpreted as an integral type consisting of the
    specified number of bits.
    <unquote>

    The result of violating a "shall" outside of a constraint clause is
    undefined behavior.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Feb 24, 2004
    #5
  6. Erik Cato

    Jack Klein Guest

    On 23 Feb 2004 04:55:53 -0800, (Erik Cato) wrote in
    comp.lang.c:

    > Hi group!
    >
    > Is this code legal?
    >
    > typedef enum
    > {
    > FALSE = 0,
    > TRUE = 1,
    > }t_bool;
    >
    > typedef struct
    > {
    > t_bool b0 : 1,
    > b1 : 1,
    > b2 : 1,
    > b3 : 1,
    > b4 : 1,
    > b5 : 1,
    > b6 : 1,
    > b7 : 1;
    > }t_bitfield8;
    >
    > //Erik


    It is undefined behavior prior to the C99 standard update and allowed
    as an implementation-defined extension in C99.

    The behavior of single bit bit-fields not defines specifically as
    unsigned is very much implementation defined. Any field where the bit
    is set to 1 might be promoted to the integer value -1, rather than +1,
    in expressions.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Feb 24, 2004
    #6
  7. Erik Cato

    Ben Pfaff Guest

    Jack Klein <> writes:

    > On Mon, 23 Feb 2004 09:46:36 -0800, Ben Pfaff <>
    > wrote in comp.lang.c:
    >
    >> (Erik Cato) writes:
    >>
    >> It's not portable. Bit-fields can only portably have type signed
    >> int or unsigned int.

    >
    > No, it is undefined behavior under any version of the standard prior
    > to C99. Semantics section of 6.5.2.1 of C90/95:


    Relying on undefined behavior is nonportable.
    --
    "The fact that there is a holy war doesn't mean that one of the sides
    doesn't suck - usually both do..."
    --Alexander Viro
    Ben Pfaff, Feb 24, 2004
    #7
  8. Erik Cato

    Old Wolf Guest

    > > typedef enum
    > > {
    > > FALSE = 0,
    > > TRUE = 1,
    > > }t_bool;
    > >
    > > typedef struct
    > > {
    > > t_bool b0 : 1,

    ....
    > > }t_bitfield8;
    > >

    >
    > It is undefined behavior prior to the C99 standard update and allowed
    > as an implementation-defined extension in C99.


    What is the difference between these two cases.
    If the standard calls something "undefined" then the implementation
    is allowed to define it anyway. And if it is "allowed as an ID extension"
    but the extension is not provided, then it is UB.
    Old Wolf, Feb 25, 2004
    #8
  9. On 25 Feb 2004 13:26:34 -0800, (Old Wolf)
    wrote:

    >> > typedef enum
    >> > {
    >> > FALSE = 0,
    >> > TRUE = 1,
    >> > }t_bool;
    >> >
    >> > typedef struct
    >> > {
    >> > t_bool b0 : 1,

    >...
    >> > }t_bitfield8;
    >> >

    >>
    >> It is undefined behavior prior to the C99 standard update and allowed
    >> as an implementation-defined extension in C99.

    >
    >What is the difference between these two cases.
    >If the standard calls something "undefined" then the implementation
    >is allowed to define it anyway. And if it is "allowed as an ID extension"
    >but the extension is not provided, then it is UB.


    The difference is:

    If it is undefined but your compiler accepts it today, there is no
    guarantee what will happen tomorrow, if your compiler is updated, or
    if you change systems.

    If it is implementation defined, then every compliant compiler
    must document what it will do (or in this case if the extension is
    allowed).


    <<Remove the del for email>>
    Barry Schwarz, Feb 27, 2004
    #9
    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. zb32

    bitfield optimizations

    zb32, Jul 13, 2004, in forum: C++
    Replies:
    1
    Views:
    1,054
    David Harmon
    Jul 13, 2004
  2. Claudio

    bitfield & union strange ?!

    Claudio, Aug 1, 2004, in forum: C++
    Replies:
    2
    Views:
    4,873
    Gottfried Eibner
    Aug 2, 2004
  3. Jorn Ronnow
    Replies:
    13
    Views:
    1,341
    Bo Persson
    Jun 6, 2006
  4. mc
    Replies:
    5
    Views:
    2,477
  5. Stanley Rice

    bitfield in struct and padding

    Stanley Rice, Nov 10, 2011, in forum: C Programming
    Replies:
    6
    Views:
    410
    Keith Thompson
    Nov 10, 2011
Loading...

Share This Page