# Back to some basic stuff (enum range).

• September 12th, 2012, 12:53 AM
lucky6969b
Back to some basic stuff (enum range).
In the book The C++ Programming Language

Quote:

"An enumerator can be initialized by a constant expression of integral type. The range
of an enumeration holds all the enumeration's enumerator values rounded up to the nearest binary power minus 1"
Code:

```enum e1 { dark, light };  // range 0:1 enum e2 { a = 3, b = 9 }; // range: 0:15 enum e3 { min = -10, max = 1000000 }; // range -1048576:1048575```
I don't understand how you get the ranges. Could anyone explain a little bit?
Thanks
Jack
• September 12th, 2012, 03:45 AM
JohnW@Wessex
Re: Back to some basic stuff (enum range).
Quote:

enum e1 { dark, light }; // range 0:1
Depends on the values of dark & light.
If dark = 0 & light = 1 then the values can be represented in one bit.
The range of an unsigned 1 bit number is 0 to 1

Quote:

enum e2 { a = 3, b = 9 }; // range: 0:15
In binary 3 = 11 & 9 = 1001
You need a minimum of 4 bits to cover this range.
The range of an unsigned 4 bit number is 0000 to 1111 = 0 to 15

Quote:

enum e3 { min = -10, max = 1000000 }; // range -1048576:1048575
In binary, 1000000 = 11110100001001000000, which needs 20 bits.
As the other number is negative and assuming we are using 2's complement to represent negative numbers,
we need 21 bits.
1000000 = 011110100001001000000
-10 = 111111111111111110110
The range of a 2's complement 21 bit number is -1048576 to 1048575
• September 12th, 2012, 04:56 AM
lucky6969b
Re: Back to some basic stuff (enum range).
Thanks John for your detailed explanation, I get it now
• September 12th, 2012, 10:35 AM
JohnW@Wessex
Re: Back to some basic stuff (enum range).
Sorry, being a bit dumb there. :blush:
dark & light by definition will have the values of 0 & 1