Re: bitfields

From: Edward Almasy (
Date: 11/26/95

Pink Floyd writes:
> I was under the impression that you can do the following with a bitfield:
> Say you want to read in a status byte from a piece of hardware, but are only
> interested in, say, the 5th and 6th bytes.  You can:
> ...
> struct status_byte {
>   unsigned buf      :   4;  /* this will hold the first 4 bytes */
>   unsigned status_5 :   1;
>   unsigned status_6 :   1;
>   unsigned buf2     :   2;
> ...
> This will accurately allow you to get just the 5th and 6th bytes, or at least
> it should, otherwise bitfields wouldn't be nearly as useful! So I guess I'm
> not sure, will it?

They're BITfields, not BYTEfields, so status_5 and status_6 will give you
two one-bit values stored somewhere within a structure of type status_byte.

It isn't safe to use bitfield declarations to read from a status port or
from an external file (that your code didn't generate), because there are
no guarantees as to the order or actual size of the fields.  The only things
that you can be sure of is that the field will hold at least the specified
number of bits and that it can be referred to as a structure member.

None of these things are arguments against using bitfields in Circle.  It
just means that they can't be treated exactly like shifted bit flags.

 \_\_\_     _/ \_\_\_  axis data: specializing in online system setup & design
 \_  \_     _/ \_  \_    Edward Almasy               
 \_\_\_     _/ \_\_\_    President: Axis Data       Proprietor: NineJackNine
     \_ _/  _/     \_    608-256-5732 (voice)            608-256-5697 (data)
 \_\_\_ _/_/_/ \_\_\_  9jack9: on the bleeding edges of culture and technology

This archive was generated by hypermail 2b30 : 12/07/00 PST