[英]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
是(可能)是位字符串。 的操作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
将为a
或0
。
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
。
对于BonusType,==运算符也可能会重载,这可能会更改预期的行为。
两者都是非常非常糟糕的事情(IMO)。
在这种情况下,bt可能是一个位字段,包含多个标志值。
在这里,他们只想检查一小部分bt,而忽略其余部分。
表达方式
bt == BonusType.DestroyWholeRowColumn
如果DestroyWholeRowColumn和bt中的其他任何标志都为true,则返回false,这不是他们想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.