enums and bit fields

R

Randy Budd

can I use enums for a bit field (as opposed to an int)?

is it valid at all?

is it undefined across platforms? (i.e. not very portable)

is it ok as long as the enums don't overrun the bitfield?

is an enum signed or unsigned?

EX

typedef enum {
TYPE_A, TYPE_B, TYPE_C,
} Type;

typedef struct {
unsigned int aType:5;
Type bType:5; /* is this O.K. ? */
unsigned int aFlag:1;
} Struct;

main()
{
Struct a;

a.aType = TYPE_B;
a.bType = TYPE_C;

....

}
 
K

Keith Thompson

Randy Budd said:
can I use enums for a bit field (as opposed to an int)?

Not portably. C99 6.7.2.1p4:

A bit-field shall have a type that is a qualified or unqualified
version of _Bool, signed int, unsigned int, or some other
implementation-defined type.
is it valid at all?

is it undefined across platforms? (i.e. not very portable)

is it ok as long as the enums don't overrun the bitfield?

is an enum signed or unsigned?

These are all implementation-defined.

[...]

Make this "int main(void)".

[...]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top