enums and bitfields

J

James Brown

All,

Suppose I have the following enum and structure definitions:

enum e
{
ename1, ename2, ename3, ename4
};

struct s
{
int ibitfield : 20;
enum e ebitfield : 2;
};


My question is - can I mix 'int' and 'enum e' in a bitfield in this manner?
I suspect the answer is yes but don't fully trust my compiler (VC6). I
think also that as long as I reserve enough bits for 'ebitfield' which will
hold all possible values of the enumeration (2 bits = 4, in this case) then
this is also ok?

TIA,
James
 
J

John Carson

James Brown said:
All,

Suppose I have the following enum and structure definitions:

enum e
{
ename1, ename2, ename3, ename4
};

struct s
{
int ibitfield : 20;
enum e ebitfield : 2;
};


My question is - can I mix 'int' and 'enum e' in a bitfield in this
manner? I suspect the answer is yes but don't fully trust my compiler
(VC6). I think also that as long as I reserve enough bits for
'ebitfield' which will hold all possible values of the enumeration (2
bits = 4, in this case) then this is also ok?

Yes. This is covered in section 9.6 of the Standard.

Fields must be of integer or enumeration type. There is no rule against
mixing them. As to your second question, from 9.6/4:

<quote>
If the value of an enumerator is stored into a bit-field of the same
enumeration type and the number of bits in the bit-field is large enough to
hold all the values of that enumeration type, the original enumerator value
and the value of the bit-field shall compare equal.
[Example:
enum BOOL { f=0, t=1 };
struct A {
BOOL b:1;
};
A a;
void f() {
a.b = t;
if (a.b == t) // shall yield true
{ /* ... */ }
}
</quote>

The use of BOOL as the name of the enum is a little confusing here, but you
get the point.

Comeau compiles your code without complaint, which is always a very good
sign.

http://www.comeaucomputing.com/tryitout/
 
J

James Brown

John Carson said:
James Brown said:
All,

Suppose I have the following enum and structure definitions:

enum e
{
ename1, ename2, ename3, ename4
};

struct s
{
int ibitfield : 20;
enum e ebitfield : 2;
};


My question is - can I mix 'int' and 'enum e' in a bitfield in this
manner? I suspect the answer is yes but don't fully trust my compiler
(VC6). I think also that as long as I reserve enough bits for
'ebitfield' which will hold all possible values of the enumeration (2
bits = 4, in this case) then this is also ok?

Yes. This is covered in section 9.6 of the Standard.

Fields must be of integer or enumeration type. There is no rule against
mixing them. As to your second question, from 9.6/4:

<quote>
If the value of an enumerator is stored into a bit-field of the same
enumeration type and the number of bits in the bit-field is large enough
to hold all the values of that enumeration type, the original enumerator
value and the value of the bit-field shall compare equal.
[Example:
enum BOOL { f=0, t=1 };
struct A {
BOOL b:1;
};
A a;
void f() {
a.b = t;
if (a.b == t) // shall yield true
{ /* ... */ }
}
</quote>

The use of BOOL as the name of the enum is a little confusing here, but
you get the point.

Comeau compiles your code without complaint, which is always a very good
sign.

http://www.comeaucomputing.com/tryitout/

Thanks!

James
 

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

Alternative approach to bitfields 9
Bitfields 3
_Bool bitfields and cast / assignment 5
Bitmask vs bitfields 6
portable and efficient signed bitfields 4
Classes for enums 5
Printing the bitfields 18
enums and scope 5

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top