【问题标题】:How Bitwise AND "&" Works Logically?按位与“&”如何在逻辑上工作?
【发布时间】:2015-10-03 03:38:23
【问题描述】:

我需要了解一点代码,请任何有运营经验的人,我有一个开源代码,我需要了解这部分:

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;

提前致谢

【问题讨论】:

  • 要么有人在热狗代码,要么枚举中有/将会有更多的值。

标签: c# bitwise-operators logical-operators


【解决方案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)。

【讨论】:

  • 感谢您提供这些有用的信息 :) 我在两种情况下进行了测试,结果相同!
【解决方案2】:

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

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

read =    001
write =   010
execute = 111

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

userPermissions = 011

如果我们只是检查userPermissions == write,那显然是错误的,就像011 != 010。不过

userPermissions & write = 010 = write

如果用户有

userPermissions = 101

然后

 userPermissions & write = 000 != write

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

实际上,对于任何位串 ba,如果 a 只设置了一个位,那么 b&a 将是 a0

【讨论】:

    【解决方案3】:
    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
        }
    

    也许对你有帮助,谢谢。

    【讨论】:

      【解决方案4】:

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

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

      表达式

        bt == BonusType.DestroyWholeRowColumn 
      

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

      【讨论】:

      • 在给定的具体情况下,它不会有所作为。 bt = BonusType.Nonebt = BonusType.None | BonusType.DestroyWholeRowColumn 都会满足 bt == BonusType.DestroyWholeRowColumn
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-10
      • 2019-05-29
      相关资源
      最近更新 更多