Bitmask type to perform binary operations Without any specific implementation details, how would you create a bitmask type that wraps binary arithmetic into something easy to read? Ideally, this would be used with an enum, which is why there are some generic methods.

Some Questions


Is the type a struct or class?
Is the type generic or are certain methods generic?
Is it okay to constrain the generics to struct, IConvertible?
Is a type like this preferred when working with bitmasks?


Starting Example: Here's a fiddle.

//Example Use
var days = new Bitmask((ulong)(Day.Monday | Day.Tuesday));
days.Contains((ulong)Day.Friday); //false
days.Contains((ulong)Day.Monday); //true
//Example Use END    

public enum Day { Monday = 1, Tuesday = 2, Wednesday = 4, Thursday = 8, Friday = 16, Saturday = 32, Sunday = 64 }

public struct Bitmask
{
    //Bitmask Value
    private ulong _value;
    public ulong Value 
    { 
        get { return _value; }
        set { _value = value; }
    }   

    //Constructor
    public Bitmask(ulong value) { _value = value; }


    //Methods
    public bool Any() { return _value != 0; }

    public void Clear() { _value = 0; }

    public bool Contains(ulong value) { return (_value &amp; value) == value; } 

    public bool Equals(ulong value) { return _value == value; }

    public List&lt;TEnum&gt; ToList&lt;TEnum&gt;() where TEnum : struct, IConvertible
    {
        var local = this;
        var result = ((TEnum[])Enum.GetValues(typeof(TEnum))).Where(m =&gt; local.Contains&lt;TEnum&gt;(m)).ToList();
        return result;  
    }   
}
It looks like you are misusing an enum as just a more cumbersome way to write a ulong.

If the days of the week are meant to be treated as a set rather than a single value (i.e., more like checkboxes than radio buttons), then you should be using the [Flags] attribute.  In fact, this is almost exactly the example given in the MSDN documentation:


[Flags]
enum Days2
{
    None = 0x0,
    Sunday = 0x1,
    Monday = 0x2,
    Tuesday = 0x4,
    Wednesday = 0x8,
    Thursday = 0x10,
    Friday = 0x20,
    Saturday = 0x40
}

class MyClass
{
    Days2 meetingDays = Days2.Tuesday | Days2.Thursday;
}



Personally, I'd write the constants using left-shift operators:

[Flags]
enum Days2
{
    None = 0,
    Sunday = 1 &lt;&lt; 0,
    Monday = 1 &lt;&lt; 1,
    Tuesday = 1 &lt;&lt; 2,
    Wednesday = 1 &lt;&lt; 3,
    Thursday = 1 &lt;&lt; 4,
    Friday = 1 &lt;&lt; 5,
    Saturday = 1 &lt;&lt; 6
}