bitfields considered harmful?

Jim Patterson jimp at asterix.UUCP
Fri May 5 02:26:11 AEST 1989


In article <705 at cf-cm.UUCP> sme at computing-maths.cardiff.ac.uk (Simon Elliott) writes:
>In article <1473 at uwbull.uwbln.UUCP>, ckl at uwbln.UUCP (Christoph Kuenkel) writes:
>> We have some software using C bitfields like in
>> I like them cause they save space ...
>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.

This may be so if the computer doesn't have bitfield operations, but
many (e.g. VAX, MC68020, 80286) have instructions that implement
bitfields directly. Even single-bit test and set instructions are
worthwhile as bit-flags are quite common. These instructions can
result in considerably shorter code sequences as they deal with small
numbers (usually in the range 0 to 31, i.e. 5 bits) instead of large
32-bit masks.

Most compilers aren't smart enough to "optimize" a series of ands/ors
and shifts into bitfield access.
-- 
Jim Patterson                              Cognos Incorporated
UUCP:decvax!utzoo!dciem!nrcaer!cognos!jimp P.O. BOX 9707    
PHONE:(613)738-1440                        3755 Riverside Drive
                                           Ottawa, Ont  K1G 3Z4



More information about the Comp.lang.c mailing list