位字段枚举类型和逻辑否定

时间:2017-01-12 02:54:11

标签: c#

我在C#中有两个枚举

public class PlayerAttributes 
{

        private UInt32 m_AttributeFlagsMask;
        private UInt32 m_AttributeFlagsBitmap;

        [Flags]
        public enum EAttributesFlagsBmp
        {
           AIn = 0,
           BIn = (1 << 1),  //1
           CIn = (1 << 2),  //2
           DIn = (1 << 3),  //4 
           EIn = (1 << 4), //8
           FIn = (1 << 5),  //16
           GIn = (1 << 6)  //32
        }

        [Flags]
        public enum EAttributeFlagsMask
        {
            None = 0,
            AIn = (1 << 1),     //1
            BIn = (1 << 2),  //2
            CIn = (1 << 3),   //4
            DIn = (1 << 4),  //8
            EIn = (1 << 5),  //16
            FIn = (1 << 6)  //32
        }

        public UInt32 AttributeFlagsMask { get { return m_AttributeFlagsMask; } private set { m_AttributeFlagsMask = value; } }
        public UInt32 AttributeFlagsBitmap { get { return m_AttributeFlagsBitmap; } private set { m_AttributeFlagsBitmap = value; } }

        public bool SetAIn(bool enable)
        {
            if(enable)
            {

            }else
            {
                     unset AIn bit but enable AIn flag
            }

            return true;
        }
}

在功能AIn中我想unset AIn bit but enable AIn flag。我该怎么做。我不能在!上使用UInt32运算符

1 个答案:

答案 0 :(得分:1)

int上的按位求反运算符为~

if (enable) {
    m_AttributeFlagsMask |= AIn;
} else {
    m_AttributeFlagsMask &= ~AIn;
}