Back to some basic stuff (enum range).
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Back to some basic stuff (enum range).

1. ## Back to some basic stuff (enum range).

In the book The C++ Programming Language

"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

2. ## Re: Back to some basic stuff (enum range).

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

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

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

3. ## Re: Back to some basic stuff (enum range).

Thanks John for your detailed explanation, I get it now

4. ## Re: Back to some basic stuff (enum range).

Sorry, being a bit dumb there.
dark & light by definition will have the values of 0 & 1

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•