Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Bit-field Question

Reply
Thread Tools

Bit-field Question

 
 
Fao, Sean
Guest
Posts: n/a
 
      01-13-2006
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
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      01-13-2006
"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) kst- <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.
 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      01-13-2006
On Fri, 13 Jan 2006 01:16:47 GMT, Keith Thompson <kst-> 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/~a...FAQ-acllc.html
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-13-2006
Jack Klein <> writes:
> On Fri, 13 Jan 2006 01:16:47 GMT, Keith Thompson <kst-> 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) kst- <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.
 
Reply With Quote
 
Roberto Waltman
Guest
Posts: n/a
 
      01-14-2006
<-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 ]
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-14-2006
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) kst- <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57