C++ – Bitmask to Array Index

bitmaskc++indexing

Is there a simple way to convert a bitmask in to an array index?

ie. If i've got an enum

a = 0x01,
b = 0x02,
c = 0x04,
d = 0x08,
e = 0x10, 
etc

and I want to store releated data in an array, is there a simple way such that i can convert a to 0, b to 1, c to 2. etc?

Many thanks

Best Solution

r =   ln base 2 
and programmatically,

unsigned int v=yourEnumValue;
unsigned r = 0; 
while (v >>= 1) 
{
   r++;
}

r is your answer