Bitwise AND “&” 在逻辑上是如何工作的?

How Bitwise AND "&" Works Logically?

我需要了解一些代码,请任何有操作员经验的人,我有一个开源代码,我需要了解这部分:

public static bool ContainsDestroyWholeRowColumn(BonusType bt)
{
    return (bt & BonusType.DestroyWholeRowColumn) 
        == BonusType.DestroyWholeRowColumn;
}

BonusType 是一个枚举:

[Flags]
public enum BonusType
{
    None,
    DestroyWholeRowColumn
}

请解释一下这部分是如何工作的?

return (bt & BonusType.DestroyWholeRowColumn) == BonusType.DestroyWholeRowColumn;

为什么不写:return bt == BonusType.DestroyWholeRowColumn; ?

提前致谢

在这种情况下,bt可能是一个位域,包含多个标志值。

在这里,他们只想检查 bt 的一位,而忽略其余部分。

表达式

  bt == BonusType.DestroyWholeRowColumn 

如果 DestroyWholeRowColumn 和 bt 中的任何其他标志都为真,return 将为假,这不是他们想要的。

bt 在这种情况下(可能)是一个位串。 AND将它与一个常量进行比较,然后进行比较的操作称为掩码。

这是一个例子。我们将使用权限。说

read =    001
write =   010
execute = 111

假设用户对特定文件具有权限,我们想测试他们是否可以写入文件。

userPermissions = 011

如果我们简单地检查 userPermissions == write 那显然是错误的,因为 011 != 010。然而

userPermissions & write = 010 = write

如果相反,用户有

userPermissions = 101

然后

 userPermissions & write = 000 != write

因此,您可以看到这如何允许将数据存储为位串,然后 "masked" 查看它是否设置了特定的位。

真正的要点是,对于任何位串 ba,如果 a 恰好设置了一位,那么 b&a 要么是 a0.

In & or && Both are doing AND operation but behaviour is very different when you try to compare with'&' then if first is true or not it's doesn't effect on it and always it goes to second statement and set it value like if(a==b & a=b) it's always goes to the second statement but when you use '&&' operator like if(a==b && a=b) in that case if first condition is true hen only give to the second statement .and the same thing happening in your condition also .
But you can compare bt == BonusType.DestroyWholeRowColumn but because you are comparing enum value which is basically taking int value .if you want to know some more in detail so follow this program do some r&d on it.basically '&' or '|' this operator work on binary format.
 class Program
    {
        static void Main(string[] args)
        {
            BonusType bt=(BonusType)1;
            Console.WriteLine(ContainsDestroyWholeRowColumn(bt));
            Console.ReadLine();
        }

        public static bool ContainsDestroyWholeRowColumn(BonusType bt)
        {
            return (bt == BonusType.DestroyWholeRowColumn);
               // == BonusType.DestroyWholeRowColumn;
        }
    }

    [Flags]
    public enum BonusType
    {
        None=1,
        DestroyWholeRowColumn=0,
        abc,
        xyz
    }

可能对你有帮助谢谢。

[TestClass]
public class EnumTest
{
    [TestMethod]
    public void FlagsTest()
    {
        var test1 = BonusType.None;
        Assert.That(ContainsDestroyWholeRowColumn(test1), Is.False);
        var test2 = BonusType.DestroyWholeRowColumn;
        Assert.That(ContainsDestroyWholeRowColumn(test2));
        var test3 = BonusType.None | BonusType.DestroyWholeRowColumn;
        Assert.That(ContainsDestroyWholeRowColumn(test3));

        Assert.That(test3 == BonusType.DestroyWholeRowColumn);

        Assert.That(ContainsDestroyWholeRowColumn((BonusType)5));
    }

}

[Flags]
public enum BonusType
{
    None,
    DestroyWholeRowColumn
}

public static bool ContainsDestroyWholeRowColumn(BonusType bt)
{
    return (bt & BonusType.DestroyWholeRowColumn)
        == BonusType.DestroyWholeRowColumn;
}

如您在此示例中所见,行为与相等运算符不同的唯一情况是将 int 强制转换为 BonusType

也有可能为 BonusType 重载 == 运算符,这可能会改变预期的行为。

这两件事都是非常非常糟糕的事情 (IMO)。