[comp.lang.c] Unnamed bitfield

rdw2030@venus.tamu.edu (06/30/90)

From Herbert Schildt's excellent _C - The Complete Reference_ ...

It states that leaving bits in a bitfield unnamed is an excellent way to skip
bits that you don't want to read in a control status.  So, in this case

struct foo
{
   unsigned bit0 : 1 ;
   unsigned bit1 : 1 ;
   unsigned      : 2 ;
   unsigned bit4 : 1 ;
} foobar ;

when reading status from a device that returns info in bits 0, 1, and 4, and
you are not interested in what's in 2 and 3, you can just ignore them.  As
for the assignment statement:

} foobar={1,1,1} ;

it makes no mention of this!  I think that perhaps bit fields must be brute-
force initialized in this case (i.e. foobar.bit4=1).

I experimented with TurboC a bit today.  Trying to assign values for the skipped
bits ( foobar={1,1,0,0,1} ) produced the expected error (too many values).  I
tried brute-force assigning the value 3 for bit1 (with bit0=0) to see if it 
would "overflow" into one of the other values, but it didn't.  (I did all this
with F7 (trace) and watches set for foobar, bit0, bit1, and bit4.  Nothing I
could do would ever change the value of bit4, save for the brute-force method.
This is how it would be done for a status read anyway, so it works.

Skipping of bitfields is a feature, folks!  Don't you love it?  :-)

Mark C. Lowe - KB5III