Jump to content
  • Advertisement
Sign in to follow this  
Headkaze

Enum bits to index

This topic is 2809 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Say I have an enum of bits

#define BIT(n) (1 << (n))

enum MyEnum
{
MYENUM_NONE = 0,
MYENUM_ONE = BIT(0),
MYENUM_TWO = BIT(1),
MYENUM_THREE = BIT(2)
...
};


Is there a simple way to convert their values to indexes in the enum?

Eg.

MYENUM_NONE to 0, MYENUM_ONE to 1, MYENUM_TWO to 2 and MYENUM_THREE to 3 and so on. I understand I could do this in a for loop and test each bit, but I want a single statement like a bitshift or something.

Share this post


Link to post
Share on other sites
Advertisement
Well, chances are that your processor has a single instruction for this kind of thing. The trick is accessing it. For x86 it's BSR, which you can usually grab with inline assembly, if you consider that to be a single statement. If you're using gcc you can use __builtin_ctz() (count trailing zeros). However, these have unpredictable results when you pass in 0 as an argument. If you're willing to settle for multiple statements that don't involve for loops there are various bithacks you can try. However, most of those are severely obfuscated algorithms.

Share this post


Link to post
Share on other sites
This operation is very common in computer chess when using the bitboard representation. The fastest method is often what's labeled "Count the consecutive zero bits (trailing) on the right with multiply and lookup" in the page SiCrane linked to.

On POSIX systems (e.g., Linux) you have a function ffs, which does exactly what you want.

Share this post


Link to post
Share on other sites
Quote:
Original post by Headkaze
I was hoping of a nice single statement in C to do it. Oh well just went with a loop and bit check. Thanks anyway.


It can be done with one line, but it's not pretty or fast:
#include <math.h>

unsigned ffs(unsigned x) {
return x ? 1 + log(double(x))/log(2.0) : 0;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!