473,396 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Bit-field Question

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
Jan 13 '06 #1
5 2297
"Fao, Sean" <en**********@yahoo.comI-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) ks***@mib.org <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.
Jan 13 '06 #2
On Fri, 13 Jan 2006 01:16:47 GMT, Keith Thompson <ks***@mib.org> 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
Jan 13 '06 #3
Jack Klein <ja*******@spamcop.net> writes:
On Fri, 13 Jan 2006 01:16:47 GMT, Keith Thompson <ks***@mib.org> 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) ks***@mib.org <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.
Jan 13 '06 #4
<en**********@yahoo.comI-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 ]
Jan 14 '06 #5
Roberto Waltman <us****@rwaltman.net> writes:
<en**********@yahoo.comI-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) ks***@mib.org <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.
Jan 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Bonj | last post by:
Happy new year to everybody... Just a few questions about porting code to 64-bit environment. 1) Is there a different version of the SDK (i.e., C/C++ compiler) for the 64-bit platform, or does...
13
by: Amy DBA | last post by:
I've been asked to administer a DB2 V 8 (32-bit install) on a Solaris 64-bit platform. It seems like whomever installed DB2 on the server, goofed for not installing DB2 v8 64 bit. Do I understand...
12
by: Jean-Marc Blaise | last post by:
Hi, Is it worth to use 64-bit DB2 instances on a 32-bit kernel, in terms of: - performance - configuration (go beyond the 256 Mb segment for private mem, 1.75 Gb for Bufferpools) - other ? ...
16
by: Mohanasundaram | last post by:
Hi All, We are working on porting a product written in C and C++ from 32 bit to 64 bit. We need to maintain both 32 bit and 64 bit versions in the future. We took the 32 bit source code and...
15
by: Henry Samuelson | last post by:
I am looking for some bit wizardy for the following: We have two 32-bit integers A and B. I need to come up with an operation O that takes A and B as arguments, spitting out a single integer R...
11
by: JDeats | last post by:
1. Will there be different 64-bit .NET implementations for Intel and AMD 64-bit processors or will they share a common 64-bit CLR? 2. Will .NET managed code compiled for the 32-bit CLR be binary...
0
by: Ganapathy | last post by:
I have COM dll code written in VC 6.0. When i tried compiling this code in VC 7, The MIDL cmpiler gets called twice. i.e. it initially compiles fully & immediately a line - 64 bit processing'...
14
by: ern | last post by:
Does a function exist to convert a 128-bit hex number to a string?
14
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I am using C# to develop DLL using Visual Studio 2005 and .Net 2.0, and I have no idea of how to make my DLL work with applications on 64-bit platform. Above all, I do not...
1
by: Chuck Chopp | last post by:
I have some code that is being built on the following: Windows Server 2003, both 32-bit & 64-bit editions Windows Vista, both 32-bit & 64-bit editions Windows Server 2008, both 32-bit & 64-bit...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.