A while ago i explained why do you need a [Flags] attribute on the Enum here.
We'll since then i was happy with my flags until i got to the point of having 16 flags.
So why is that a problem? Well it's not really but 2^16 is how much?
If you had to open the all mighty calculator you see my point. I stop counting powers of 2 after 4096 = 2^12.
I see no reason to remember those.
That's why I wanted to write my enums a bit simpler. Like saying this is 2 to the power of 1, 2, 3, 4 etc
Using Math.Pow won't work and it's not pretty. And C# doesn't have a Power operator.
So what to do?
Bit-shifting anyone? :)
I thought this didn't work in an enum but it does, so from now on i'm writing my enums like this:
[Flags]
enum bq
{
a = 0, // returns 0
b = 2 >> 1, // returns 1 = 2 to power of 0
c = 2 << 0, // returns 2 = 2 to power of 1
d = 2 << 1, // returns 4 = 2 to power of 2
e = 2 << 2, // returns 8 = 2 to power of 3
f = 2 << 3, // returns 16 = 2 to power of 4
g = 2 << 4 // returns 32 = 2 to power of 5
// etc, etc, etc
}
Quick bit-shifting lesson:
2 << n: a left arithmetic shift by n is equivalent to multiplying by 2n
2 >> n: a right arithmetic shift by n is equivalent to dividing by 2n
Of course you have to watch for overflow with this.
Happy enuming. :)