Bit-field Question

Discussion in 'C Programming' started by Fao, Sean, Jan 13, 2006.

  1. Fao, Sean

    Fao, Sean Guest

    I was just thumbing through K&R 2nd Edition and thought I'd read up on
    bit-fields, which I [personally] haven't had much use for in my career.
    Anyhow, K&R says that, "Fields may be declared only as ints; for
    portability, specify signed or unsigned explicitly". So it got me
    wondering what would happen if I tried to declare a field as a short
    rather than an int.

    I compiled my code with GCC 3.3.6 with all warning turned on and I got
    no warnings returned. Have things changed since K&R or is there
    something bad about my code?

    On my implementation, the sizeof() each bit-field is identical to the
    type I used to declare the fields.

    <code>
    #include <stdio.h>

    struct {
    unsigned short admin : 1;
    unsigned short manager : 1;
    unsigned short supervisor : 1;
    unsigned short user : 1;
    unsigned short guest : 1;
    } roles;

    struct {
    unsigned int admin : 1;
    unsigned int manager : 1;
    unsigned int supervisor : 1;
    unsigned int user : 1;
    unsigned int guest : 1;
    } roles2;

    int main(void)
    {
    printf("sizeof(short): %d sizeof(roles): %d\n",
    sizeof(short), sizeof(roles));
    printf("sizeof(int): %d sizeof(roles2): %d\n",
    sizeof(int), sizeof(int));


    return 0;
    }
    </code>

    Just curious...

    Thank you in advance,

    --
    Sean
     
    Fao, Sean, Jan 13, 2006
    #1
    1. Advertising

  2. "Fao, Sean" <-WANT-NO-SPAM> writes:
    > I was just thumbing through K&R 2nd Edition and thought I'd read up on
    > bit-fields, which I [personally] haven't had much use for in my
    > career. Anyhow, K&R says that, "Fields may be declared only as ints;
    > for portability, specify signed or unsigned explicitly". So it got me
    > wondering what would happen if I tried to declare a field as a short
    > rather than an int.


    What will happen depends on the implementation. The standard says
    (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.

    An implementation may support other types for bit fields, but it's not
    required to; any program that uses such an extension is non-portable.

    > I compiled my code with GCC 3.3.6 with all warning turned on and I got
    > no warnings returned. Have things changed since K&R or is there
    > something bad about my code?
    >
    > On my implementation, the sizeof() each bit-field is identical to the
    > type I used to declare the fields.


    C99 6.5.3.4p1:

    The sizeof operator shall not be applied to an expression that has
    function type or an incomplete type, to the parenthesized name of
    such a type, or to an expression that designates a bit-field
    member.

    This is a constraint, so if gcc doesn't give you a diagnostic, it's
    non-conforming. (gcc can generally be persuaded to be conforming, or
    nearly so, with command-line options.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jan 13, 2006
    #2
    1. Advertising

  3. Fao, Sean

    Jack Klein Guest

    On Fri, 13 Jan 2006 01:16:47 GMT, Keith Thompson <> wrote
    in comp.lang.c:

    [snip]

    > > On my implementation, the sizeof() each bit-field is identical to the
    > > type I used to declare the fields.

    >
    > C99 6.5.3.4p1:
    >
    > The sizeof operator shall not be applied to an expression that has
    > function type or an incomplete type, to the parenthesized name of
    > such a type, or to an expression that designates a bit-field
    > member.
    >
    > This is a constraint, so if gcc doesn't give you a diagnostic, it's
    > non-conforming. (gcc can generally be persuaded to be conforming, or
    > nearly so, with command-line options.)


    You missed on this last one, Keith. His remark, which you are
    commenting on here, is inaccurate and does not reflect what his code
    actually does. I have replaced his code below, which you snipped:

    > #include <stdio.h>
    >
    > struct {
    > unsigned short admin : 1;
    > unsigned short manager : 1;
    > unsigned short supervisor : 1;
    > unsigned short user : 1;
    > unsigned short guest : 1;
    > } roles;
    >
    > struct {
    > unsigned int admin : 1;
    > unsigned int manager : 1;
    > unsigned int supervisor : 1;
    > unsigned int user : 1;
    > unsigned int guest : 1;
    > } roles2;
    >
    > int main(void)
    > {
    > printf("sizeof(short): %d sizeof(roles): %d\n",
    > sizeof(short), sizeof(roles));
    > printf("sizeof(int): %d sizeof(roles2): %d\n",
    > sizeof(int), sizeof(int));
    >
    >
    > return 0;
    > }


    Clearly he is not applying the sizeof operator to the bit-field
    members, but to the entire struct type containing the bit-field
    members, and this of course is perfectly valid.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    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, Jan 13, 2006
    #3
  4. Jack Klein <> writes:
    > On Fri, 13 Jan 2006 01:16:47 GMT, Keith Thompson <> wrote
    > in comp.lang.c:
    >
    > [snip]
    >
    >> > On my implementation, the sizeof() each bit-field is identical to the
    >> > type I used to declare the fields.

    >>
    >> C99 6.5.3.4p1:
    >>
    >> The sizeof operator shall not be applied to an expression that has
    >> function type or an incomplete type, to the parenthesized name of
    >> such a type, or to an expression that designates a bit-field
    >> member.
    >>
    >> This is a constraint, so if gcc doesn't give you a diagnostic, it's
    >> non-conforming. (gcc can generally be persuaded to be conforming, or
    >> nearly so, with command-line options.)

    >
    > You missed on this last one, Keith. His remark, which you are
    > commenting on here, is inaccurate and does not reflect what his code
    > actually does. I have replaced his code below, which you snipped:


    [snip again]

    > Clearly he is not applying the sizeof operator to the bit-field
    > members, but to the entire struct type containing the bit-field
    > members, and this of course is perfectly valid.


    Whoops.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jan 13, 2006
    #4
  5. <-WANT-NO-SPAM> wrote:
    >...
    >...
    >int main(void)
    >{
    > printf("sizeof(short): %d sizeof(roles): %d\n",
    > sizeof(short), sizeof(roles));
    > printf("sizeof(int): %d sizeof(roles2): %d\n",
    > sizeof(int), sizeof(int));

    -------------------------------^^^

    You probably meant "roles2" here.


    Roberto Waltman

    [ Please reply to the group,
    return address is invalid ]
     
    Roberto Waltman, Jan 14, 2006
    #5
  6. Roberto Waltman <> writes:
    > <-WANT-NO-SPAM> wrote:
    >>...
    >>...
    >>int main(void)
    >>{
    >> printf("sizeof(short): %d sizeof(roles): %d\n",
    >> sizeof(short), sizeof(roles));
    >> printf("sizeof(int): %d sizeof(roles2): %d\n",
    >> sizeof(int), sizeof(int));

    > -------------------------------^^^
    >
    > You probably meant "roles2" here.


    Apart from that, "%d" isn't a correct format for size_t, which is the
    type that the sizeof operator yields. Either use "%ld" and cast the
    value to unsigned long, or use the C99-specific "%zd" format.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jan 14, 2006
    #6
    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,847
    Timothy Bendfelt
    Jan 19, 2007
  2. JustSomeGuy

    8 bit bit field?

    JustSomeGuy, Feb 27, 2006, in forum: C++
    Replies:
    2
    Views:
    390
    Victor Bazarov
    Feb 27, 2006
  3. Replies:
    9
    Views:
    1,046
    Juha Nieminen
    Aug 22, 2007
  4. Sound
    Replies:
    2
    Views:
    498
    Randy Webb
    Sep 28, 2006
  5. Jeff.M
    Replies:
    6
    Views:
    202
    Lasse Reichstein Nielsen
    May 4, 2009
Loading...

Share This Page