开发者

How to set all bits of enum flag

开发者 https://www.devze.com 2023-04-05 14:49 出处:网络
I wonder a generic way for setting all bits of enum flag to 1. I just would like to have an enum which returns for all comparisons, regardless of other enums.

I wonder a generic way for setting all bits of enum flag to 1. I just would like to have an enum which returns for all comparisons, regardless of other enums.

And this code works;

[Flags]
public enum SomeRightEnum : uint
{
    CanDoNothing = 0,
    CanDoSomething = 1 << 0,
    CanDoSomethingElse = 1 << 1,
    CanDoYetAnotherThing = 1 << 2,
    ...
    DoEverything = 0xFFFFFFFF 
}

But at the code above since it is uint we set the number of "F"s, it wouldn't work if it was int.

So I'll appreciate a generic way o开发者_开发知识库f setting all bits of enum flag to 1, regardless of the datatype (int, int64, uint etc)


Easiest is probably:

enum Foo
{
  blah = 1,
  ....
  all = ~0
}

For unsigned based enum:

enum Foo : uint
{
  blah = 1,
  ....
  all = ~0u;
}


[Flags]
public enum MyEnum
{
    None   = 0,
    First  = 1 << 0,
    Second = 1 << 1,
    Third  = 1 << 2,
    Fourth = 1 << 3,
    All = ~(-1 << 4)
}


internal static class Program
{
    private static void Main()
    {
        Console.WriteLine(Foo.Everything.HasFlag(Foo.None)); // False
        Console.WriteLine(Foo.Everything.HasFlag(Foo.Baz)); // True
        Console.WriteLine(Foo.Everything.HasFlag(Foo.Hello)); // True
    }
}

[Flags]
public enum Foo : uint
{
    None = 1 << 0,
    Bar = 1 << 1,
    Baz = 1 << 2,
    Qux = 1 << 3,
    Hello = 1 << 4,
    World = 1 << 5,
    Everything = Bar | Baz | Qux | Hello | World
}

Was this what you wanted?


In case someone is wondering: I needed to do the same building a Bindable enumconverter for WPF.
Since I don't know what the values mean in Reflection, I needed to manually been able to switch values (binding them to a checkbox p.e.)
There is a problem setting the value of a Flagged enum to -1 to set all the bits.
If you set it to -1 and you unflag all values it will not result in 0 because all unused bits are not unflagged.
This is wat worked best for my situation.

SomeRightEnum someRightEnum = SomeRightEnum.CanDoNothing;
Type enumType = someRightEnum.GetType();
int newValue = 0;
var enumValues = Enum.GetValues(enumType).Cast<int>().Where(e => e == 1 || e % 2 == 0);
foreach (var value in enumValues)
{
    newValue |= value;
}
Console.WriteLine(newValue);

Or if you would want an extension method:

public static class FlagExtensions
{
    public static TEnum AllFlags<TEnum>(this TEnum @enum)
        where TEnum : struct
    {
        Type enumType = typeof(TEnum);
        long newValue = 0;
        var enumValues = Enum.GetValues(enumType);
        foreach (var value in enumValues)
        {
            long v = (long)Convert.ChangeType(value, TypeCode.Int64);
            if(v == 1 || v % 2 == 0)
            {
               newValue |= v; 
            }
        }
        return (TEnum)Enum.ToObject(enumType , newValue);
    }
}


I find All = ~0 more elegant, but to have more transparency starting from C# 7.0 you can use

binary literals like 0b0101010111 together with digit separators _ like 0b_0101_0101_11.

So to set all flags for the Int32 you can chose one of the following possibilities, all of them are the same:

All = unchecked((int)0b_1111_1111_1111_1111_1111_1111_1111_1111)
All = unchecked((int)0b_11111111_11111111_11111111_11111111)
All = unchecked((int)0b11111111111111111111111111111111)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号