By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,911 Members | 1,171 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,911 IT Pros & Developers. It's quick & easy.

enums and bitfields

P: n/a
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

Dec 23 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"James Brown" <em***@viahomepage.comwrote in message
news:2s******************************@pipex.net
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/
--
John Carson
Dec 24 '06 #2

P: n/a

"John Carson" <jc****************@netspace.net.auwrote in message
news:45***********************@uv-55king-reader-01.melbourne.pipenetworks.com.au...
"James Brown" <em***@viahomepage.comwrote in message
news:2s******************************@pipex.net
>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/
--
John Carson
Thanks!

James

Dec 24 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.