473,387 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

enums and bitfileds and signs

If I have a struct defined as follows:

enum ABCD { A = 0, B = 1, C = 2, D = 3 };
struct Foo { ABCD val : 2; }

and I do:

Foo f;
f.val = C;
ABCD v = f.val;

With g++, v will have the value 'C'. With Microsoft's compiler it will
have a value of -2.

Which of these is the correct behavior? Or is using signed and/or enum
values in bit fields not well defined?
Nov 15 '07 #1
2 1884
Jason Kraftcheck wrote:
If I have a struct defined as follows:

enum ABCD { A = 0, B = 1, C = 2, D = 3 };
struct Foo { ABCD val : 2; }

and I do:

Foo f;
f.val = C;
ABCD v = f.val;

With g++, v will have the value 'C'. With Microsoft's compiler it
will have a value of -2.

Which of these is the correct behavior? Or is using signed and/or
enum values in bit fields not well defined?
I don't belive the signedness is specified for enum-based bit-fields.
The requirement is that the size of the bit-field should allow storing
of _all_ values of the enum. If 'ABCD' has 'int' for its underlying
type, and since 'int' is signed, then 2 bits is not enough for 'val'
member, it has to have 3 (one for the sign bit, which should stay
unused, however). That's how I read it, anyway.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 15 '07 #2
On Nov 15, 11:58 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Jason Kraftcheck wrote:
If I have a struct defined as follows:
enum ABCD { A = 0, B = 1, C = 2, D = 3 };
struct Foo { ABCD val : 2; }
and I do:
Foo f;
f.val = C;
ABCD v = f.val;
With g++, v will have the value 'C'. With Microsoft's compiler it
will have a value of -2.
Which of these is the correct behavior? Or is using signed and/or
enum values in bit fields not well defined?
I don't belive the signedness is specified for enum-based
bit-fields. The requirement is that the size of the bit-field
should allow storing of _all_ values of the enum. If 'ABCD'
has 'int' for its underlying type, and since 'int' is signed,
then 2 bits is not enough for 'val' member, it has to have 3
(one for the sign bit, which should stay unused, however).
That's how I read it, anyway.
What about §7.2/7:

For an enumeration where emin is the smallest enumerator
and emax is the largest, the values of the enumeration
are the values in the range bmin to bmax, defined as
follows: Let K be 1 for a two's complement
representation and 0 for a one's complement or
sign-magnitude representation. bmax is the smallest
value greater than or equal to max(|emin|-K, |emax|) and
equal to 2M -1, where M is a non-negative integer. bmin
is zero if emin is non-negative and -(bmax+K) otherwise.
The size of the smallest bit-field large enough to hold
all the values of the enumeration type is max(M,1) if
bmin is zero and M+1 otherwise. It is possible to define
an enumeration that has values not defined by any of its
enumerators.

By my calculations, given his values above, M is 2, so the
size of the smallest bit-field large enough to hold all the
values of the enumeration type is 2. The values of the
enumeration type are A, B, C and D---if he assigns one of
these values to a bit-field of at least 2 bits, he is
guaranteed to get it back when he rereads it.

Note that this is a subtle difference between C and C++. C
doesn't make any real guarantees with regards to enums in
bit fields, and Microsoft's implementation would presumably
be legal in C (although from a quality of implementation
point of view, I think it should be considered a bug even in
C).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 16 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could...
2
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
27
by: Mark A. Gibbs | last post by:
i have been toying with the idea of making my enums smarter - ie, more in line with the rest of the language. i haven't tested it yet, but what i came up with is a template like this: template...
2
by: SpotNet | last post by:
Hello CSharpies, Can Enums in C# be UInt32 Types, and not just Int32 Types. I have a lot of constant declarations that are UInt32 that I want to put in Enums to ease the use of Intellisence. I...
4
by: Martin Pritchard | last post by:
Hi, I'm working on a project that historically contains around 40 enums. In the database various fields refer to the int values of these enums, but of course ref integrity is not enofrced and...
2
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
2
by: Simon Elliott | last post by:
I have some legacy C++ code which requires some enums to be 1 or 2 bytes in size. I'd ideally like to be able to specify that a few carefully selected enums are a particular size. By default,...
11
by: Marc Gravell | last post by:
This one stumped me while refactoring some code to use generics... Suppose I declare an enum MyEnum {...} Is there a good reason why MyEnum doesn't implement IEquatable<MyEnum> ? Of course,...
4
by: Jon Slaughter | last post by:
is there a simple way to "step" through enums? I have a button that I want to click and have it "cycle" through a set of states defined by enums but the only way I can think of doing this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.