Bitmask index to array

Is there an easy way to convert bitmasks to array index?

T. If I have enum

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

and I want to store the released data in an array, is there an easy way so that I can convert a to 0, b to 1, c to 2, etc.?

Many thanks

+3
source share
5 answers
r =   ln base 2 
and programmatically,

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

r is your answer
+6
source

I’m not sure if this is what you are asking for, but why don’t you just take a 2 basic magazine?

+4
source

2 n?

+3

, , ?

.

+1

std:: map:

#include <map>

std::map <my_enum, my_datatype> m;
m[ a ] = whatever;
+1

Source: https://habr.com/ru/post/1705559/


All Articles