在我的生活中,我不记得如何设置,删除,切换或测试位域中的位。要么我不确定,要么我把它们弄混了,因为我很少需要它们。所以有一个“bit-cheat-sheet”会很好。

例如:

flags = flags | FlagsEnum.Bit4;  // Set bit 4.

or

if ((flags & FlagsEnum.Bit4)) == FlagsEnum.Bit4) // Is there a less verbose way?

你能给出所有其他常见操作的例子吗,最好是在c#语法中使用[Flags]枚举?


当前回答

习惯用法是使用按位或等于操作符来设置位:

flags |= 0x04;

为了澄清一点,这个习语是按位使用,并带否定:

flags &= ~0x04;

有时你有一个偏移量来标识你的位,然后习惯用法是将这些偏移量与左移结合使用:

flags |= 1 << offset;
flags &= ~(1 << offset);

其他回答

为了获得最佳性能和零垃圾,请使用以下代码:

using System;
using T = MyNamespace.MyFlags;

namespace MyNamespace
{
    [Flags]
    public enum MyFlags
    {
        None = 0,
        Flag1 = 1,
        Flag2 = 2
    }

    static class MyFlagsEx
    {
        public static bool Has(this T type, T value)
        {
            return (type & value) == value;
        }

        public static bool Is(this T type, T value)
        {
            return type == value;
        }

        public static T Add(this T type, T value)
        {
            return type | value;
        }

        public static T Remove(this T type, T value)
        {
            return type & ~value;
        }
    }
}

我在这些扩展上做了更多的工作-你可以在这里找到代码

我写了一些扩展方法来扩展System。枚举,我经常使用…我并不是说它们是无懈可击的,但它们确实有所帮助……删除注释……

namespace Enum.Extensions {

    public static class EnumerationExtensions {

        public static bool Has<T>(this System.Enum type, T value) {
            try {
                return (((int)(object)type & (int)(object)value) == (int)(object)value);
            } 
            catch {
                return false;
            }
        }

        public static bool Is<T>(this System.Enum type, T value) {
            try {
                return (int)(object)type == (int)(object)value;
            }
            catch {
                return false;
            }    
        }


        public static T Add<T>(this System.Enum type, T value) {
            try {
                return (T)(object)(((int)(object)type | (int)(object)value));
            }
            catch(Exception ex) {
                throw new ArgumentException(
                    string.Format(
                        "Could not append value from enumerated type '{0}'.",
                        typeof(T).Name
                        ), ex);
            }    
        }


        public static T Remove<T>(this System.Enum type, T value) {
            try {
                return (T)(object)(((int)(object)type & ~(int)(object)value));
            }
            catch (Exception ex) {
                throw new ArgumentException(
                    string.Format(
                        "Could not remove value from enumerated type '{0}'.",
                        typeof(T).Name
                        ), ex);
            }  
        }

    }
}

然后像下面这样使用它们

SomeType value = SomeType.Grapes;
bool isGrapes = value.Is(SomeType.Grapes); //true
bool hasGrapes = value.Has(SomeType.Grapes); //true

value = value.Add(SomeType.Oranges);
value = value.Add(SomeType.Apples);
value = value.Remove(SomeType.Grapes);

bool hasOranges = value.Has(SomeType.Oranges); //true
bool isApples = value.Is(SomeType.Apples); //false
bool hasGrapes = value.Has(SomeType.Grapes); //false

为了测试一点,你可以做以下事情: (假设flags是一个32位的数字)

测试点: if((flags & 0x08) == 0x08)(如果第4位设置为真) 切换回(1 - 0或0 - 1):flags = flags ^ 0x08; 重置位4到零:flags = flags & 0xFFFFFF7F;

c++语法,假设第0位是LSB,假设flags是unsigned long:

检查是否设置:

flags & (1UL << (bit to test# - 1))

检查是否未设置:

invert test !(flag & (...))

Set:

flag |= (1UL << (bit to set# - 1))

明确:

flag &= ~(1UL << (bit to clear# - 1))

切换:

flag ^= (1UL << (bit to set# - 1))

习惯用法是使用按位或等于操作符来设置位:

flags |= 0x04;

为了澄清一点,这个习语是按位使用,并带否定:

flags &= ~0x04;

有时你有一个偏移量来标识你的位,然后习惯用法是将这些偏移量与左移结合使用:

flags |= 1 << offset;
flags &= ~(1 << offset);