Subject: Re: Bitfields and kernel
To: m. k. buelow <token@mayn.de>
From: Steven J. Dovich <dovich@lethe.tiac.net>
List: tech-kern
Date: 09/28/1999 17:23:06
m. k. buelow wrote:
> IMHO, implementing binary flags is best done with constants and
> the common C idioms of setting or clearing bits.  It might
> also be considered good style to define constants with enum in
> ANSI C instead of #defining them with the preprocessor because
> why should the preprocessor do something that the compiler itself
> can do as well if not better.

The problem with this is that ANSI C does not define the behavior for
enums as bit masks. The C++ committee ran into this and departed from
C in this respect because there was significant use of the construct
in the IOSTREAMS classes. As a result, C++ guarantees sufficient storage
to represent values between the enum-max and enum-min values. ANSI C on
the other hand permits the compiler to do all manner of value compression
to minimize the bits necessary to represent the enumerated values. As
a result, unless the enum type contains an enumerated symbol for the
value, you can't rely on the language to store the value in an object
of that type.

In practice it will generally work. The standardese in this area is
rather like wearing one of those hospital robes: everything you can see
looks like it is covered, but your backside feels a bit drafty.

/sjd