bitfields considered harmful?

Gregory Kemnitz kemnitz at mitisft.Convergent.COM
Wed May 3 04:39:54 AEST 1989


In article <885 at twwells.uucp>, bill at twwells.uucp (T. William Wells) writes:
> In article <1473 at uwbull.uwbln.UUCP> ckl at uwbln.UUCP (Christoph Kuenkel) writes:
> : We have some software using C bitfields like in
> :
> :       struct ... {
> :               unsigned        active : 1,
> :                               waiting : 1;
> :               ...
> :       };
> :

I BELIEVE (I may be wrong) that if a bitfield field (need a better word)
is NEVER used for ANYTHING ELSE (like masks, etc), then code that uses
bitfields is portable (at least between 680X0's and 386's).  However, when
you use a bitfield field with masks and attempt to set the bitfield field
and hope the constitutient bitfields are set right, then you are making
(nonportable) assumptions about the bit ordering in your machine.   Therefore
NEVER use bitfield fields for anything else but bitfields.

				Greg Kemnitz



More information about the Comp.lang.c mailing list