如何按位和"&"逻辑上工作?

时间:2015-10-03 03:38:24

标签: c# bitwise-operators logical-operators

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

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

和BonusType是一个枚举:

[Flags]
public enum BonusType
{
    None,
    DestroyWholeRowColumn
}

请解释这部分的工作原理?

返回(bt& BonusType.DestroyWholeRowColumn)             == BonusType.DestroyWholeRowColumn;

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

提前致谢

4 个答案:

答案 0 :(得分:1)

在这种情况下,

bt(可能)是一个字符串。 {{1}使用常量然后进行比较的操作称为屏蔽。

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

AND

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

read =    001
write =   010
execute = 111

如果我们只检查userPermissions = 011 明显错误,请userPermissions == write。然而

011 != 010

如果用户有了

userPermissions & write = 010 = write

然后

userPermissions = 101

因此,你可以看到它如何允许数据存储为位串,然后“屏蔽”以查看它是否设置了特定的位。

对于任何位串 userPermissions & write = 000 != write b,如果a只有一位设置,那么a将为b&a或{ {1}}。

答案 1 :(得分:1)

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
    }

可能会对你有所帮助。

答案 2 :(得分:1)

[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)。

答案 3 :(得分:0)

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

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

表达式

  bt == BonusType.DestroyWholeRowColumn 
如果DestroyWholeRowColumn和bt中的任何其他标志都为真,

将返回false,这不是他们想要的。

相关问题