Quick questions about unions in bit fields

I would like to create a bit field struct composed of enums. However, I need to include a union of enums for one of the bit fields because how its contents is interpreted will depend on the first enum in the class. Is something like this allowed?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
enum EnumIdentifier { Enum1, Enum2, Enum3, Enum4.... }; // max of 7
enum Enum1 { .... }; // max of 127
enum Enum2 { .... }; // max of 127
enum Enum3 { .... }; // max of 127
enum Enum4 { .... }; // max of 127

union EnumBits
{
	Enum1 enum1;
	Enum2 enum2;
	Enum3 enum3;
	Enum4 enum4;
}

struct InstanceID // total of 4 bytes or 32 bits
{
	EnumIdentifier enumID : 4
	EnumBits enumBits : 8 // IS THIS ALLOWED?
	unsigned int instance : 20
}


ALSO (EDIT added): Is this structure guaranteed to take only 4 bytes or will data alignment create a problem? Is this the most efficient way to do this or would it be more efficient to do my own bitwise operations (using masks etc...)
Last edited on
Nope.... guess not.
Topic archived. No new replies allowed.