473,386 Members | 1,679 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,386 software developers and data experts.

Stroustrup 4.8 Enumerations

after reading that sections, i know:

- an "enum" is user-defined type
- there is no implicit conversion from an "int" to an "enum" BUT an
"int" can be converted to an "enum" *explicitly*.

the thing i did not understand is the "range" of an "enum":

enum e1 { dark, light } // range 0:1

that is fine as dark == 0 and light == 1 (both are of type e1, not
"int")

enum e2 {a = 3, b = 9} // range 0:15

how come ? i think the range here is 0 - 9.

enum { min = -10, max = 1000000 } // range -1048576:1048575

hoe come here ?

Stroustrup also explained it:

"the range of an enumeration holds all the enumeration's enumerator
values rounded upto the nearest largest binary power minus 1. the
range goes down to zero if the smallest enumerator is non-negative and
nearest lesser negative binary power if the smallest enumeration is
negative. this defines the smallest bit-field capable of holding the
enumerator values."

so by this rule range of "e2" must be: 0 to 256 (which is 2^8,
because 9 -1 == 8)

i do not understand anything here :-(

even the last line is simply alien to me: "this defines the smallest
bit-field capable of holding the enumerator values"

what exactly is "bit-field" ?

Mar 26 '07 #1
9 1317
On Mar 26, 12:02 pm, "arnuld" <geek.arn...@gmail.comwrote:
after reading that sections, i know:

- an "enum" is user-defined type
- there is no implicit conversion from an "int" to an "enum" BUT an
"int" can be converted to an "enum" *explicitly*.

the thing i did not understand is the "range" of an "enum":

enum e1 { dark, light } // range 0:1

that is fine as dark == 0 and light == 1 (both are of type e1, not
"int")

enum e2 {a = 3, b = 9} // range 0:15

how come ? i think the range here is 0 - 9.

enum { min = -10, max = 1000000 } // range -1048576:1048575

hoe come here ?

Stroustrup also explained it:

"the range of an enumeration holds all the enumeration's enumerator
values rounded upto the nearest largest binary power minus 1. the
range goes down to zero if the smallest enumerator is non-negative and
nearest lesser negative binary power if the smallest enumeration is
negative. this defines the smallest bit-field capable of holding the
enumerator values."

so by this rule range of "e2" must be: 0 to 256 (which is 2^8,
because 9 -1 == 8)

i do not understand anything here :-(
OK, i red this thread:

http://groups.google.com/group/comp....5d16e2bfded281

and understood the "range" phenomenon.

but still, i did not understand this

"this defines the smallest bit-field capable of holding the enumerator
values"

what exactly is "bit-field" ?

?

Mar 26 '07 #2
arnuld a écrit :
after reading that sections, i know:

- an "enum" is user-defined type
- there is no implicit conversion from an "int" to an "enum" BUT an
"int" can be converted to an "enum" *explicitly*.

the thing i did not understand is the "range" of an "enum":

enum e1 { dark, light } // range 0:1

that is fine as dark == 0 and light == 1 (both are of type e1, not
"int")

enum e2 {a = 3, b = 9} // range 0:15

how come ? i think the range here is 0 - 9.
You have the answer:
rounded upto the nearest largest binary power minus 1
Binary powers: 2,4,8,16,32 ....

nearest largest binary of 9 is 16.
Minus one: 15

Thus range is 0-15
>
enum { min = -10, max = 1000000 } // range -1048576:1048575

hoe come here ?
In this case, there is a negative value.
With only positives, range would have been 0-1048575.
Since there is a neg value and since C++ has 1 complement signed
representation, the equivalent integer (2^21) has the range:
-1048576:1048575

>
Stroustrup also explained it:

"the range of an enumeration holds all the enumeration's enumerator
values rounded upto the nearest largest binary power minus 1. the
range goes down to zero if the smallest enumerator is non-negative and
nearest lesser negative binary power if the smallest enumeration is
negative. this defines the smallest bit-field capable of holding the
enumerator values."

so by this rule range of "e2" must be: 0 to 256 (which is 2^8,
because 9 -1 == 8)
8 is 2^3 so Three bits is enough for range 0-7 (in unsigned case).
4 bits represent the range 0-15 (in unsigned case).
>
i do not understand anything here :-(

even the last line is simply alien to me: "this defines the smallest
bit-field capable of holding the enumerator values"

what exactly is "bit-field" ?
a bit field is an integer represented on a specified number of bits.

int i:4; //is a 4 bits signed integer (range -8 - 7)
unsigned integer u:6; //is a 4 bit unsigned integer (range 0 - 15)
If you have the standard, it should be specified somewhere;
Michael
Mar 26 '07 #3
Michael DOUBEZ a écrit :
arnuld a écrit :
>what exactly is "bit-field" ?

a bit field is an integer represented on a specified number of bits.

int i:4; //is a 4 bits signed integer (range -8 - 7)
unsigned integer u:6; //is a 4 bit unsigned integer (range 0 - 15)
[bug] :)
unsigned integer u:4; //is a 4 bit unsigned integer (range 0 - 15)
Michael
Mar 26 '07 #4
On Mar 26, 12:29 pm, Michael DOUBEZ <michael.dou...@free.frwrote:
Michael DOUBEZ a écrit :arnuld a écrit :
what exactly is "bit-field" ?
a bit field is an integer represented on a specified number of bits.
int i:4; //is a 4 bits signed integer (range -8 - 7)
unsigned integer u:6; //is a 4 bit unsigned integer (range 0 - 15)

[bug] :)
unsigned integer u:4; //is a 4 bit unsigned integer (range 0 - 15)
nearest largest binary power: 2, 4, 6, 8 is 6.

minus 1 == 6 - 1 == 5

so the range is 0-5, how it is 0-15 ?

Mar 26 '07 #5
* arnuld:
On Mar 26, 12:29 pm, Michael DOUBEZ <michael.dou...@free.frwrote:
>Michael DOUBEZ a écrit :arnuld a écrit :
>>>what exactly is "bit-field" ?
a bit field is an integer represented on a specified number of bits.
int i:4; //is a 4 bits signed integer (range -8 - 7)
unsigned integer u:6; //is a 4 bit unsigned integer (range 0 - 15)
[bug] :)
unsigned integer u:4; //is a 4 bit unsigned integer (range 0 - 15)

nearest largest binary power: 2, 4, 6, 8 is 6.

minus 1 == 6 - 1 == 5

so the range is 0-5, how it is 0-15 ?
4 bits yield 2*2*2*2 = 16 bitpatterns, or values.

Unsigned range 0 through 15, inclusive, which is 16 values.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 26 '07 #6
On Mar 26, 2:28 pm, "Alf P. Steinbach" <a...@start.nowrote:
4 bits yield 2*2*2*2 = 16 bitpatterns, or values.

Unsigned range 0 through 15, inclusive, which is 16 values.
thanks Alf.

now for "signed i = 4" we have 1 bit for reserved for "sign",

so 3 bits = 2*2*2 = 8

how ill i decide the range of values in this case ?

Mar 26 '07 #7
* arnuld:
>On Mar 26, 2:28 pm, "Alf P. Steinbach" <a...@start.nowrote:
>4 bits yield 2*2*2*2 = 16 bitpatterns, or values.

Unsigned range 0 through 15, inclusive, which is 16 values.

thanks Alf.

now for "signed i = 4" we have 1 bit for reserved for "sign",

so 3 bits = 2*2*2 = 8

how ill i decide the range of values in this case ?
Well stop right there. There's a /huge/ difference between

signed x:4;

in a struct somewhere, saying that x is four bits, and

signed x=4;

saying that x is an int (a.k.a. signed int a.k.a. signed) initialized
with the value 4.

Your original problem had to do with understand the range of an enum
type, i.e. what values you can store in a variable of that type.

Here's a simple algorithm:

1. If unsigned int or signed int can hold any of the values specified
in the enum declaration, then the /underlying type/ of the enum is
int, unsigned int or something smaller (such as e.g. short).

2. Otherwise, it's something larger.

Essentially that's all the standard says and all that you can rely on,
namely that the underlying type of an enum will not be more bits than
'int' unless necessary to represent all defined values of the enum.

It's a kind of memory efficiency guarantee, that an enum type will not
be arbitrarily large unless a named value exceeds the range of int or
unsigned int.

Suprisingly there's no such guarantee for 'bool'. In theory, a 'bool'
could be 2 gigabytes -- or more! Needless to say, no compiler
implements it that way (it's typically one byte), but that's the formal.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 26 '07 #8
.. On Mar 26, 3:44 pm, "Alf P. Steinbach" <a...@start.nowrote:
Well stop right there. There's a /huge/ difference between

signed x:4;

in a struct somewhere, saying that x is four bits, and

signed x=4;
interesting, i did not know that. i thought, the "x:4" is an eye-
pleasing thing.

Your original problem had to do with understand the range of an enum
type, i.e. what values you can store in a variable of that type.
yep, you got me ;-)

Here's a simple algorithm:

1. If unsigned int or signed int can hold any of the values specified
in the enum declaration, then the /underlying type/ of the enum is
int, unsigned int or something smaller (such as e.g. short).

2. Otherwise, it's something larger.
you said: it is an algorithm.

how can it be? there is not MATH in it.

Essentially that's all the standard says and all that you can rely on,
namely that the underlying type of an enum will not be more bits than
'int' unless necessary to represent all defined values of the enum.

ok, an enum will always be: int or unsigned int (or short)

EXCEPT if i intentionally put large values into an enum. i got it.

BTW, i know that /plain int/ is always signed.
It's a kind of memory efficiency guarantee, that an enum type will not
be arbitrarily large unless a named value exceeds the range of int or
unsigned int.

Suprisingly there's no such guarantee for 'bool'. In theory, a 'bool'
could be 2 gigabytes -- or more!
=:-0
Needless to say, no compiler
implements it that way (it's typically one byte), but that's the formal.
what is the context of using: formal

Mar 26 '07 #9
arnuld <ge*********@gmail.comwrote:
. On Mar 26, 3:44 pm, "Alf P. Steinbach" <a...@start.nowrote:
>Here's a simple algorithm:

1. If unsigned int or signed int can hold any of the values specified
in the enum declaration, then the /underlying type/ of the enum is
int, unsigned int or something smaller (such as e.g. short).

2. Otherwise, it's something larger.

you said: it is an algorithm.

how can it be? there is not MATH in it.
An algorithm is basically a series of steps used to solve a problem. It
doesn't necessarily have to be a math problem.
>Suprisingly there's no such guarantee for 'bool'. In theory, a 'bool'
could be 2 gigabytes -- or more!

=:-0
>Needless to say, no compiler
implements it that way (it's typically one byte), but that's the formal.

what is the context of using: formal
He is saying that the Standard makes no formal guarantees on the size of
bool, but as a QoI (quality of implementation) issue, nobody would use a
compiler that used 2 gigabyte bools, even though there is nothing in the
Standard preventing someone from doing so.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Mar 26 '07 #10

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

Similar topics

1
by: Joyce | last post by:
In my schema I have 2 enumerations, let's say, country description and country code, and I want to use them so I can map each country description to its precise country code (and no other). So far...
0
by: Plinkerton | last post by:
I'm making an Base Class that will be inherited. In my base class, I have a public enumeration that defines a list of things I want my class to be able to do. I use it for Method input...
21
by: Christopher Benson-Manica | last post by:
I'll try to explain what I want to do: I have foo.h and foo.cpp. Units that include foo.h will define an enumeration bar: enum bar { typeNone, typeBaz, typeQuux, ... , count }; A method...
1
by: someone else | last post by:
I have some code that creates dynamic enumerations for use in a PropertyGrid control. This all works perfectly but the memory usage of the program increases quite quicly when viewing the...
1
by: Oleg Ogurok | last post by:
Hi all, I've added a new DataSet (xsd file) to my project in VS.NET 2003. There I create a simple type as an enumeration of values. <xs:simpleType name="MyCustomType"> <xs:restriction...
4
by: ChrisB | last post by:
Hello: I will be creating 50+ enumerations related to a large number of classes that span a number of namespaces. I was wondering if there are any "best practices" when defining enumerations. ...
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
77
by: Ben Finney | last post by:
Howdy all, PEP 354: Enumerations in Python has been accepted as a draft PEP. The current version can be viewed online: <URL:http://www.python.org/peps/pep-0354.html> Here is the...
1
by: Visame | last post by:
How to understand the following paragraph from Bjarne stroustrup's The C++ Programming Language? 9.4.1: In principle, a variable defined outside any function (that is, global, namespace, and class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.