From: "Mike Collins" Newsgroups: comp.os.msdos.djgpp Subject: Re: A bug in DJGPP? (length of bit fields) Date: Sat, 1 Jan 2000 23:11:20 -0000 Organization: Customer of Planet Online Lines: 43 Message-ID: <84m1em$69p$1@newsg1.svr.pol.co.uk> References: <84fmpj$ptf$1 AT newsg3 DOT svr DOT pol DOT co DOT uk> <84gdqg$29fi$1 AT news DOT gate DOT net> <84il97$7ir$1 AT news6 DOT svr DOT pol DOT co DOT uk> <84j1nf$22eo$1 AT news DOT gate DOT net> NNTP-Posting-Host: modem-65.thalium.dialup.pol.co.uk X-Trace: newsg1.svr.pol.co.uk 946768150 6457 62.136.40.65 (1 Jan 2000 23:09:10 GMT) NNTP-Posting-Date: 1 Jan 2000 23:09:10 GMT X-Complaints-To: abuse AT theplanet DOT net X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2314.1300 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com > >Yes, I know that a bit field can be defined as short, in which case, it > >would occupy a short int, equal to 2 bytes, but in my structure, I defined > >the bit fields inside an unsigned int. Shouldn't that force the length of > >the bit fields to be 4, and any other stuff in the structure should be added > >to that? > > > > But the bit fields did not use all the space so the compiler is free to > add or subtract additional space as it see fit. > Since in thoeory you are not using this extra space. If you are using > the extra space you should define it. > Thanks, David, I think I had misunderstood the meaning of the definition of a bit field. I don't have a copy of K&R, but I looked it up in the library. I had previously learned that when I defined : unsigned bit_field: 6; .. it meant that 6 bits of an unsigned int would be used for my bit field. The length used up in the structure would necessarily be sizeof)unsigned). I now reallise that it is the 6 bit field which contains an unsigned number, as it might have contained a signed one. I had not reallised that you could define: int bit_field_1:3, bit_field_2:6; ... and that both bit fields would contain signed numbers. Well, you live and learn. My thanks to yourself and Phrasant, who also spent time with me on e-mail trying to sort this out, and my apologies to DJGPP, which doesn't have a bug after all! Mike.