Flags Enum over 2 ^ 32

I use Enum flags in my application. Enum can have about 50+ values, so the values ​​reach 2 ^ 50. I'm just wondering if I can use Math.Pow(2, variable) to calculate them?

When I try to do this, I get a constant compile-time error. Is there any other way besides calculating these permissions 2 manually and entering it?

That's what I'm doing:

 [Flags] internal enum RiskStates : long { None = 0, AL = Convert.ToInt64(Math.Pow(2,0)), AK = 2, AZ = 4, AR = 8, CA = 16, CO = 32, CT = 64, DC = 128, DE = 256, FL = 512, GA = 1024, HI = 2048, ID = 4096, IL = 8192, IN = 16384, IA = 32768, KS = 65536, KY = 131072, LA = 262144, ME = 524288, MD = 1048576, MA = 2097152, MI = 4194304 } 
+63
enums c # flags
Sep 26 '13 at 7:20
source share
3 answers

When I try to do this, I get a constant compile-time value error.

Actually, it would be nice if you used the suffix L to make it be a long literal, but still not perfect to specify them all manually. (This is not "obviously correct" when reading code.)

You cannot use Math.Pow , since the expression must be a compile-time constant, but you can use a bit shift:

 None = 0, AL = 1L << 0, AK = 1L << 1, AZ = 1L << 2 

etc .. I would argue that all this can be read :)

+121
Sep 26 '13 at 7:21
source share

If you switch to the use of non-decimal notations, where powers 2 are more regular, you will no longer need to generate them automatically, for example:

 // octal AL = 0001L, AK = 0002L, AZ = 0004L, AR = 0010L, CA = 0020L, CO = 0040L, CT = 0100L, ... // hexadecimal AL = 0x001L, AK = 0x002L, AZ = 0x004L, AR = 0x008L, CA = 0x010L, CO = 0x020L, CT = 0x040L, ... 
+8
Sep 30 '13 at 23:18
source share

I will be tempted to consider using BitArray as a base structure.

+1
01 Oct '13 at 23:12
source share



All Articles