bitfields considered harmful?

Simon Elliott sme at computing-maths.cardiff.ac.uk
Tue May 2 23:59:32 AEST 1989


In article <1473 at uwbull.uwbln.UUCP>, ckl at uwbln.UUCP (Christoph Kuenkel) writes:
> We have some software using C bitfields like in
> 
> [deleted] 
> 
> I like them cause they save space and are much more readable than 
> oriing/anding with # defines and i dont have to bother with questions like
> how many flags fit into one int.

Well, you may be right about readability, but I don't think you'll find that
you've saved much space in your program.  Oh, you might save it in the
source, but the same shifting and masking is going on under the hood.

Now to the real question - how much space have you saved by packing the data?
probably not a lot, unless you are using tens of booleans.  How much space have
you wasted by generating code to shift and mask?  Depends how often you look
at the flag, right?  Whether or not you save space is a trade-off, like so
many of these religiously-held ideas.  There is no universally-right answer.


-- 
--------------------------------------------------------------------------
Simon Elliott            Internet: sme%v1.cm.cf.ac.uk at nsfnet-relay.ac.uk
UWCC Computer Centre     JANET:    sme at uk.ac.cf.cm.v1
40/41 Park Place         UUCP:     {backbones}!mcvax!ukc!reading!cf-cm!sme
Cardiff, Wales           PHONE:    +44 222 874300



More information about the Comp.lang.c mailing list