Skip to main content
Rollback to Revision 4
Source Link
Quill
  • 12.1k
  • 5
  • 41
  • 94

EDIT:

Final Solution: Use the [Flags] attribute on the enum. Create an EnumExtensions class if more methods are needed.

[Flags] //Flags attribute creates a bitmask out of the enum.
public enum Day { None = 0x0, Monday = 0x1, Tuesday = 0x2, Wednesday = 0x4, Thursday = 0x8, Friday = 0x16, Saturday = 0x32, Sunday = 0x64 }

public static class EnumExtensions
{
    public static List<string> ToList(this Enum item)
    {
        return item.ToString().Split(',').Select(m => m.Trim()).ToList();   
    }
    
    //To match Linq's nomenclature.
    public static bool Contains(this Enum item, Enum value)
    {
        return item.HasFlag(value); 
    }
}

public class Program
{       
    public static void Main()
    {
        var freeDays = Day.Monday | Day.Tuesday | Day.Friday;
        
        var onSaturday = freeDays.Contains(Day.Saturday) ? "Yes" : "No";
        Console.WriteLine(String.Format("Am I free on Saturday? {0}", onSaturday));
        
        var onTuesday = freeDays.Contains(Day.Tuesday) ? "Yes" : "No";
        Console.WriteLine(String.Format("Am I free on Tuesday? {0}", onTuesday));
        
        //Use EnumExtensions to get the list of
        foreach (var item in freeDays.ToList())
        {
            Console.WriteLine(item);
        }
        
        //Print csv
        Console.WriteLine(freeDays);
    }
}

EDIT:

Final Solution: Use the [Flags] attribute on the enum. Create an EnumExtensions class if more methods are needed.

[Flags] //Flags attribute creates a bitmask out of the enum.
public enum Day { None = 0x0, Monday = 0x1, Tuesday = 0x2, Wednesday = 0x4, Thursday = 0x8, Friday = 0x16, Saturday = 0x32, Sunday = 0x64 }

public static class EnumExtensions
{
    public static List<string> ToList(this Enum item)
    {
        return item.ToString().Split(',').Select(m => m.Trim()).ToList();   
    }
    
    //To match Linq's nomenclature.
    public static bool Contains(this Enum item, Enum value)
    {
        return item.HasFlag(value); 
    }
}

public class Program
{       
    public static void Main()
    {
        var freeDays = Day.Monday | Day.Tuesday | Day.Friday;
        
        var onSaturday = freeDays.Contains(Day.Saturday) ? "Yes" : "No";
        Console.WriteLine(String.Format("Am I free on Saturday? {0}", onSaturday));
        
        var onTuesday = freeDays.Contains(Day.Tuesday) ? "Yes" : "No";
        Console.WriteLine(String.Format("Am I free on Tuesday? {0}", onTuesday));
        
        //Use EnumExtensions to get the list of
        foreach (var item in freeDays.ToList())
        {
            Console.WriteLine(item);
        }
        
        //Print csv
        Console.WriteLine(freeDays);
    }
}
Added my final solution.
Source Link
christo8989
  • 467
  • 3
  • 9
  • 16

EDIT:

Final Solution: Use the [Flags] attribute on the enum. Create an EnumExtensions class if more methods are needed.

[Flags] //Flags attribute creates a bitmask out of the enum.
public enum Day { None = 0x0, Monday = 0x1, Tuesday = 0x2, Wednesday = 0x4, Thursday = 0x8, Friday = 0x16, Saturday = 0x32, Sunday = 0x64 }

public static class EnumExtensions
{
    public static List<string> ToList(this Enum item)
    {
        return item.ToString().Split(',').Select(m => m.Trim()).ToList();   
    }
    
    //To match Linq's nomenclature.
    public static bool Contains(this Enum item, Enum value)
    {
        return item.HasFlag(value); 
    }
}

public class Program
{       
    public static void Main()
    {
        var freeDays = Day.Monday | Day.Tuesday | Day.Friday;
        
        var onSaturday = freeDays.Contains(Day.Saturday) ? "Yes" : "No";
        Console.WriteLine(String.Format("Am I free on Saturday? {0}", onSaturday));
        
        var onTuesday = freeDays.Contains(Day.Tuesday) ? "Yes" : "No";
        Console.WriteLine(String.Format("Am I free on Tuesday? {0}", onTuesday));
        
        //Use EnumExtensions to get the list of
        foreach (var item in freeDays.ToList())
        {
            Console.WriteLine(item);
        }
        
        //Print csv
        Console.WriteLine(freeDays);
    }
}

EDIT:

Final Solution: Use the [Flags] attribute on the enum. Create an EnumExtensions class if more methods are needed.

[Flags] //Flags attribute creates a bitmask out of the enum.
public enum Day { None = 0x0, Monday = 0x1, Tuesday = 0x2, Wednesday = 0x4, Thursday = 0x8, Friday = 0x16, Saturday = 0x32, Sunday = 0x64 }

public static class EnumExtensions
{
    public static List<string> ToList(this Enum item)
    {
        return item.ToString().Split(',').Select(m => m.Trim()).ToList();   
    }
    
    //To match Linq's nomenclature.
    public static bool Contains(this Enum item, Enum value)
    {
        return item.HasFlag(value); 
    }
}

public class Program
{       
    public static void Main()
    {
        var freeDays = Day.Monday | Day.Tuesday | Day.Friday;
        
        var onSaturday = freeDays.Contains(Day.Saturday) ? "Yes" : "No";
        Console.WriteLine(String.Format("Am I free on Saturday? {0}", onSaturday));
        
        var onTuesday = freeDays.Contains(Day.Tuesday) ? "Yes" : "No";
        Console.WriteLine(String.Format("Am I free on Tuesday? {0}", onTuesday));
        
        //Use EnumExtensions to get the list of
        foreach (var item in freeDays.ToList())
        {
            Console.WriteLine(item);
        }
        
        //Print csv
        Console.WriteLine(freeDays);
    }
}
deleted 99 characters in body; edited tags
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

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

  1. Is the type a struct or class?
  2. Is the type generic or are certain methods generic?
  3. Is it okay to constrain the generics to struct, IConvertible?
  4. 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 & value) == value; } 

    public bool Equals(ulong value) { return _value == value; }
    
    public List<TEnum> ToList<TEnum>() where TEnum : struct, IConvertible
    {
        var local = this;
        var result = ((TEnum[])Enum.GetValues(typeof(TEnum))).Where(m => local.Contains<TEnum>(m)).ToList();
        return result;  
    }   
}

(I couldn't find a good tag for this question and my rep is not high enough to create one.)

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

  1. Is the type a struct or class?
  2. Is the type generic or are certain methods generic?
  3. Is it okay to constrain the generics to struct, IConvertible?
  4. 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 & value) == value; } 

    public bool Equals(ulong value) { return _value == value; }
    
    public List<TEnum> ToList<TEnum>() where TEnum : struct, IConvertible
    {
        var local = this;
        var result = ((TEnum[])Enum.GetValues(typeof(TEnum))).Where(m => local.Contains<TEnum>(m)).ToList();
        return result;  
    }   
}

(I couldn't find a good tag for this question and my rep is not high enough to create one.)

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

  1. Is the type a struct or class?
  2. Is the type generic or are certain methods generic?
  3. Is it okay to constrain the generics to struct, IConvertible?
  4. 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 & value) == value; } 

    public bool Equals(ulong value) { return _value == value; }
    
    public List<TEnum> ToList<TEnum>() where TEnum : struct, IConvertible
    {
        var local = this;
        var result = ((TEnum[])Enum.GetValues(typeof(TEnum))).Where(m => local.Contains<TEnum>(m)).ToList();
        return result;  
    }   
}
Provide an example so there is some context about the type.
Source Link
christo8989
  • 467
  • 3
  • 9
  • 16
Loading
my logic was a little wrong
Source Link
christo8989
  • 467
  • 3
  • 9
  • 16
Loading
Source Link
christo8989
  • 467
  • 3
  • 9
  • 16
Loading