gutjahr@isa.de (Bernd Gutjahr) (09/13/90)
Hello,
I have some questions about enums and bitfields. Maybe some ANSI-gurus
can help me. (Sorry, of have no access to the ANSI-Standard, only K&R 2. ed)
1. Is it defined wether bitfield of enum types like in e.g.:
enum bool { true, false };
struct {
enum bool flag : 1;
} s;
are signed or not ?
As I understand it from K&R, enums behave like ints, and the sign of
sign of int bitfield are implementation-dependent.
2. If enums behave like ints, does this means that either
- int and enum bitfield are signed
- int and enum bitfield are unsigned
is true ?
3: In GNU's gcc 1.35,
- int bitfields are signed
- enum bitfields are signed or unsigned, that depends
on the values of the enum members.
4. If the behavior of enum bitfields is implementation dependent,
is it possible the make them unsigned with something like:
unsigned enum bool flag : 1;
I hope somebody can help me, thanksin advace
Bernd
--
Bernd Gutjahr
I S A GmbH - Informationssysteme fuer computerintegrierte Automatisierung
Azenbergstr. 35, D-7000 Stuttgart 1, West-Germany, Phone: +49-711/22769-0
email: gutjahr@isa.de
drh@cs.Princeton.EDU (Dave Hanson) (09/15/90)
In article <3119@isaak.isa.de> gutjahr@isa.de (Bernd Gutjahr) writes: >1. Is it defined wether bitfield of enum types like in e.g.: > > enum bool { true, false }; > struct { enum bool flag : 1; } s; > > are signed or not ? As I understand it from K&R, enums behave like ints, > and the sign of sign of int bitfield are implementation-dependent. according to sec. 3.5.2.1, bit-fields must be int, signed int, or unsigned. each enum must be compatible with an integer type (sec. 3.5.2.2); if that type is int, signed int, or unsigned, then the enum is a legal bit-field type. otherwise it's illegal. since compilers are free to use different integer types for enums, using enum bit-fields make your program compiler-dependent. >2. If enums behave like ints, does this means that either > - int and enum bitfield are signed > - int and enum bitfield are unsigned if the enum is compatible with int, the bit-field is signed; if the enum is compatible with unsigned, the bit-field is unsigned. >4. If the behavior of enum bitfields is implementation dependent, > is it possible the make them unsigned with something like: > unsigned enum bool flag : 1; no.
gwyn@smoke.BRL.MIL (Doug Gwyn) (09/15/90)
In article <3119@isaak.isa.de> gutjahr@isa.de (Bernd Gutjahr) writes: > struct { > enum bool flag : 1; > } s; Since this violates the Semantics in 3.5.2.1 ("A bit-field shall have a type that is a qualified or unqualified version of one of int, unsigned int, or signed int), there isn't much more that can be said about what is required according to the C standard. According to 1.6, this is explicitly a case in which behavior is undefined. Thus no strictly conforming program may use this construction. An implementation that adds this as an extension need not produce a diagnostic message, but for more details about the rules you would have to consult the implementation's specific documentation to find out what the rules are. However, > unsigned enum bool flag : 1; would violate a combination of syntax rules that basically prohibit attempts to use enum-specifiers with "unsigned" in them in general, not only as the declarator in a struct-declarator.