We help IT Professionals succeed at work.

enumeration for representing bit values more than 32 bit

ambuli
ambuli asked
on
I have an enumeration that I use for adding listeners to particular events. If I have more than 32 events, how can I extend the enumeration in the following scenario.  I read references that I can use STL bitset for this.  Could not find any good example.  Can you please provide me an example of how to do this
public ListenerMgr
{
    enum Attributes
    {
       ATTR_ONE = 0x01,
       ATTR_TWO = 0x04,
       ...
       
       ATTR_TEN = 0x200,

       ATTR_THIRTY_TWO = 0x8000,000
       ATTR_THIRTY_THREE = .....
    };
    
    public bool addListener(Attributes value );

}

Open in new window

Comment
Watch Question

Top Expert 2012
Commented:
Why not using a 64bit integer instead of an enum? A bitset (http://www.sgi.com/tech/stl/bitset.html) could just extend that to a the same limit, e.g.
#define ATTR_ONE  0x01,
#define ATTR_TWO  0x04,
       ...
       
#define ATTR_TEN  0x200,

#define ATTR_THIRTY_TWO  0x8000
#define ATTR_THIRTY_THREE 

...

#define ATTR_SIXTY_THREE 0x8000000000000000 // not the sky, but this is the limit on 64bit CPUs

std::bitset<64> attrs;

if (attrs[ATTR_SIXTY_THREE]) {

  // do something with this attribute
}

Open in new window

jkr
Top Expert 2012

Commented:
Sorry, that's also the limit on 32bit CPUs.

Author

Commented:
Thanks.  I ended doing this using uint64_t.  I appears to be working so far with my testing.  Do you see any problem with this approach?


namespace Attributes
{
   const uint64_t ATTR_SOMETHING 0x0000000040000000ULL;

}
jkr
Top Expert 2012

Commented:
Excep that there's a '=' missing in the above - no, don't see any potential issues.
evilrixSenior Software Engineer (Avast)

Commented:
If you are using a C++11 standards compliant compiler you can specify the type use for enums.

enum class myenum: int64_t
{
   // your values here
};

http://en.wikipedia.org/wiki/C%2B%2B11#Strongly_typed_enumerations