Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Large flags enumerations in C#

Hey everyone, got a quick question that I can't seem to find anything about...

I'm working on a project that requires flag enumerations with a large number of flags (up to 40-ish), and I don't really feel like typing in the exact mask for each enumeration value:

public enum MyEnumeration : ulong
{
    Flag1 = 1,
    Flag2 = 2,
    Flag3 = 4,
    Flag4 = 8,
    Flag5 = 16,
    // ...
    Flag16 = 65536,
    Flag17 = 65536 * 2,
    Flag18 = 65536 * 4,
    Flag19 = 65536 * 8,
    // ...
    Flag32 = 65536 * 65536,
    Flag33 = 65536 * 65536 * 2
    // right about here I start to get really pissed off
}

Moreover, I'm also hoping that there is an easy(ier) way for me to control the actual arrangement of bits on different endian machines, since these values will eventually be serialized over a network:

public enum MyEnumeration : uint
{
    Flag1 = 1,     // BIG: 0x00000001, LITTLE:0x01000000
    Flag2 = 2,     // BIG: 0x00000002, LITTLE:0x02000000
    Flag3 = 4,     // BIG: 0x00000004, LITTLE:0x03000000
    // ...
    Flag9 = 256,   // BIG: 0x00000010, LITTLE:0x10000000
    Flag10 = 512,  // BIG: 0x00000011, LITTLE:0x11000000
    Flag11 = 1024  // BIG: 0x00000012, LITTLE:0x12000000
}

So, I'm kind of wondering if there is some cool way I can set my enumerations up like:

public enum MyEnumeration : uint
{
     Flag1 = flag(1), // BOTH: 0x80000000
     Flag2 = flag(2), // BOTH: 0x40000000
     Flag3 = flag(3), // BOTH: 0x20000000
     // ...
     Flag9 = flag(9), // BOTH: 0x00800000
}

What I've Tried:

// this won't work because Math.Pow returns double
// and because C# requires constants for enum values
public enum MyEnumeration : uint
{
    Flag1 = Math.Pow(2, 0),
    Flag2 = Math.Pow(2, 1)
}

// this won't work because C# requires constants for enum values
public enum MyEnumeration : uint
{
    Flag1 = Masks.MyCustomerBitmaskGeneratingFunction(0)
}

// this is my best solution so far, but is definitely
// quite clunkie
public struct EnumWrapper<TEnum> where TEnum
{
    private BitVector32 vector;
    public bool this[TEnum index]
    {
         // returns whether the index-th bit is set in vector
    }
    // all sorts of overriding using TEnum as args
}

Just wondering if anyone has any cool ideas, thanks!

like image 594
LorenVS Avatar asked May 07 '10 22:05

LorenVS


People also ask

What is the size of enum in C?

The C standard specifies that enums are integers, but it does not specify the size. Once again, that is up to the people who write the compiler. On an 8-bit processor, enums can be 16-bits wide. On a 32-bit processor they can be 32-bits wide or more or less.

Are enumerations available in C sharp?

Enumeration (or enum) is a value data type in C#. It is mainly used to assign the names or string values to integral constants, that make a program easy to read and maintain.

What are enumerations explain with example?

An enumeration is used in any programming language to define a constant set of values. For example, the days of the week can be defined as an enumeration and used anywhere in the program. In C#, the enumeration is defined with the help of the keyword 'enum'.

What is Flag in C sharp?

Flags are used when an enumerable value represents a collection of enum members. here we use bitwise operators, | and &


1 Answers

Why not just do:

public enum MyEnumeration : ulong 
{ 
    Flag1 = 1, 
    Flag2 = 1 << 1, 
    Flag3 = 1 << 2, 
    Flag4 = 1 << 3, 
    .
    .
    .
    Flag30 = 1 << 29, 
    Flag31 = 1 << 30, 
    Flag32 = 1 << 31
}
like image 59
Kelsey Avatar answered Sep 19 '22 14:09

Kelsey