delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/12/22/14:20:08

From: dscott AT networkusa DOT net (SCOTT19U.ZIP_GUY)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Somebody please explain : Length of bit fields
Date: Wed, 22 Dec 1999 18:12:22 GMT
Organization: Retired Electronic Engineer and Y2K expert
Lines: 105
Message-ID: <83r0pm$2ddu$1@news.gate.net>
References: <83p1rp$oiu$1 AT newsg3 DOT svr DOT pol DOT co DOT uk>
NNTP-Posting-Host: ppp-76.elpasonet.net
X-Trace: news.gate.net 945882742 79294 216.84.6.105 (22 Dec 1999 17:12:22 GMT)
X-Complaints-To: abuse AT gate DOT net
NNTP-Posting-Date: 22 Dec 1999 17:12:22 GMT
X-Newsreader: News Xpress 2.01
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

In article <83p1rp$oiu$1 AT newsg3 DOT svr DOT pol DOT co DOT uk>, "Mike Collins" <Mike AT NO_SPAM DOT e-col DOT freeserve DOT co DOT uk> wrote:
>Folks,
>
>I came across an interesting thing: In the program below, I would have
>expected a structure length of 6 bytes, but in fact, when I compile it and
>single step it, I read the values for a, b, c, d printed below. Of
>particular interest is the value of a ( the sizeof the entire structure) =
>8.
>
>
>#include <stdio.h>
>
>struct CATALOG
>{ unsigned unit:8,   // 4 bytes
>           category:8,
>           refresh :1;
>  short num;           // 2 bytes
>};
>
>
>main()
>{ int a, b, c, d, e;
>  struct CATALOG ctg;
>  a = sizeof ctg;
>  b = sizeof(ctg.num);
>  c = sizeof(ctg.unit);
>  d = sizeof(ctg.category);
>//  e = sizeof(ctg.refresh);   // DJGPP doesn't like sizeof(a_bit_field);
>}
>
> a: 8
> b: 2
> c: 4
> d: 4
>
>If I move the short in front of the unsigned to change the word boundaries,
>like this :
>
>struct CATALOG
>{ short num;
>  unsigned unit:8,
>           category:8,
>           refresh :1;
>};
>
>.... I still get exactly the same results.
>
>If I now reduce the length of catalog.category to a bit field of 7, and
>remove the line that reads
>"d = ... "  (because DJGPP doesn't like sizeof(a_bit_field))
>I get the results printed below.
>
>struct CATALOG
>{ short num;
>  unsigned unit:8,
>           category:7,
>           refresh :1;
>};
>
> a: 4
> b: 2
> c: 4
>
>Why does the length of the structure change? The length of all bit fields in
>a single unsigned integer should always be 4 bytes (sizeof(unsigned) == 4)
>as long as the total number of bits declared is less than 32.
>
>Again, putting the "short num" at the head of the structure or at its tail
>makes no difference to the results.
>Another thing that interests me is that sizeof(a_bit_field) compiles ok if
>the bit field is 8 bits long, but if it is a fraction of a byte, the compile
>throws up an error.
>
>Your learned opinions will be most appreciated.

  Bit fields are very fun to play with. I have had trouble getting them to
do what I want. I have a progrom that works on packed data of 19 bits
and it casued a lot of pain to get the complier to do what I wanted.
If you want to see a working example of code using DJGPP C that
uses bit fields all over the place look at more source code for
scott19u.zip  its free the source code is included.




David A. Scott
--

SCOTT19U.ZIP NOW AVAILABLE WORLD WIDE
http://www.jim.com/jamesd/Kong/scott19u.zip
                    
Scott famous encryption website NOT FOR WIMPS
http://members.xoom.com/ecil/index.htm

Scott rejected paper for the ACM
http://members.xoom.com/ecil/dspaper.htm

Scott famous Compression Page WIMPS allowed
http://members.xoom.com/ecil/compress.htm

**NOTE EMAIL address is for SPAMERS***

I leave you with this final thought from President Bill Clinton:

   "The road to tyranny, we must never forget, begins with the destruction of the truth." 

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019