Thread: Range of an enumeration...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    30

    Range of an enumeration...

    I need an explination of this:

    ..." The range of an enumeration holds all the enumeration's enumerator values rounded up to the nearest larger binary power minus one"...

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    The values used for an enumeration are being stored in a signed variable ????

  3. #3
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    3 enumerations - largest binary power is 2 ^ 2 - 1 = 3
    17 enumerations - largest binary power is 2 ^ 5 - 1 = 31
    43 enumerations - largest binary power is 2 ^ 6 - 1 = 65
    65000 enumerations - largest binary power is 2 ^ 16 - 1 = 65535

    etc...
    hasafraggin shizigishin oppashigger...

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    421

    Lightbulb

    Hi,

    Lets say you have the following:

    enum { IDLE, STARTING, RUNNING, ENDING, INVALID };

    the size of the enumeration MUST be big enough to contain highest value of the enumeration, in this case it must be 4 (since INVALID == 4), and it must be a value that is a power of 2, minus 1.

    So for us to be able to store the value 4, the enumeration must be a power of 2 minus one that is bigger or equal to 4.. in this case that's 2^3 - 1 = 8 - 1 = 7. So this enumeration can store from 0-7 (not just the 0-4 that we need).

    If you had:
    enum { IDLE = 23, STARTING, RUNNING, ENDING, INVALID };

    then the enumeration would need to hold a max value of 27, the nearest power of two minus one is 31... so the size of the enumeration would be 31.

    hope that helps.
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overflow and range checking for mul/div
    By Elysia in forum C++ Programming
    Replies: 28
    Last Post: 06-06-2008, 02:09 PM
  2. Srand () w/ range
    By xsbinary in forum C Programming
    Replies: 9
    Last Post: 10-21-2007, 03:24 PM
  3. Enumeration Issues
    By cdn_bacon in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2007, 02:26 PM
  4. Please help to check.
    By nicoleha in forum C Programming
    Replies: 16
    Last Post: 12-07-2005, 03:29 PM
  5. Random Numbers within a range OTHER than 1-X
    By Kaelin in forum C++ Programming
    Replies: 11
    Last Post: 02-16-2005, 11:57 AM