有哪些|和 ^ 运算符用于什么?
Possible Duplicate:
What are bitwise operators?
Recently I came across a few samples that used the |
and ^
operators. I am guessing these are or
and negation
operators.
So what actually do these operators stands for?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
|
=(按位/非短路)“or”^
=“xor”,作为信息,“not”(按位求反)是
~< /代码>
|
= (bitwise/non-short-circuiting) "or"^
= "xor"and for info, "not" (bitwise negation) is
~
如果将其应用于整数类型,它们是按位或和异或运算符。
但是,如果将它们应用于布尔类型,它们是逻辑或和异或。
查看或运算符的说明并异或运算符
您可以了解更多详细信息来自维基百科的布尔运算真值表
If you apply it to integral types, they are bitwise or and xor operator.
However if you apply them to boolean types, they are logical or and xor.
Look at an explanation of or operator and xor operator
You can get more details on boolean operations from the wikipedia truth table
MSDN 上有关于所有 C# 运算符的文档:
http://msdn.microsoft。 com/en-us/library/6a71f45d.aspx
编辑 - Jon B 评论说链接文档中的相关引用会很有用。
|
是逻辑或运算符。^
是逻辑异或运算符。MSDN has documentation on all the C# operators at:
http://msdn.microsoft.com/en-us/library/6a71f45d.aspx
EDIT - Jon B commented that a relevant quote from the linked documentation would be useful.
|
is the Logical OR operator.^
is the Logical XOR operator.它们是逻辑按位运算符
|是一个 Or 链接
^ 是 XOR 链接
they are logical bitwise operators
| is a Or link
^ is XOR link
对于整数类型,|是按位或, ^ 按位异或,为了完整起见 &是按位与。
对于布尔类型,|是布尔或,^ 布尔异或,&布尔值 &.
相比之下,||是一个短路布尔值或 - 如果第一个操作数计算为 true,则不计算第二个操作数。 &&是一个短路布尔值,并且 - 如果第一个操作数为 false,则不计算第二个操作数。不存在短路^,因为不存在不需要评估第二个的情况。
||和&&比 | 更常用和&在布尔情况下,因为通常至少会带来微小的效率增益,而不会造成损失。但是,如果右侧操作数具有在所有情况下都必须触发的副作用,那么 |或&将是使用的那个。在实践中,这种情况很少见,而且很难闻(如果副作用很重要,则应在单独的表达式中对其进行评估,以使目的更清晰)。
编辑:潜在混淆的一个来源是,在某些其他语言中,整数类型可以用作布尔值(例如,您可以执行
if(53)
,它与if(true)< 相同/code>)这使得上述运算符之间的区别非常不同:如果使用“纯”布尔类型(仅具有 true 和 false 作为其可能值),则它们是相同的,但其他情况下则不同。 C# 故意不允许对整型进行布尔运算,正是为了防止此类语言中存在错误的可能性。
With integral types, | is a bitwise or, ^ a bitwise xor and for completeness & is a bitwise and.
With boolean types, | is a boolean or, ^ a boolean xor and & a boolean &.
In comparison, || is a short-circuit boolean or - if the first operand evaluates as true the second operand isn't evaluated. && is a short-circuit boolean and - if the first operand is false, the second isn't evaluated. There is no short-circuit ^ because there is no case where the second need not be evaluated.
|| and && are more often used than | and & in boolean cases as there is normally at least a tiny efficiency gain and never a loss. However if the right-hand operand had a side-effect that it was important to trigger in all cases, then | or & would be the one to use. In practice this is rare, and a bad smell (if the side-effect is important, it should be evaluated in a separate expression to make the purpose clearer).
Edit: A source of potential confusion, is that in some other languages integral types can be used as booleans (e.g. you can do
if(53)
and it's the same asif(true)
) this makes the distinctions between the above operators quite different: They're the same if a "purely" boolean type is used (that has only true and false as its possible values) but not otherwise. C# deliberately doesn't allow boolean operations on integral types precisely to prevent the possibilities for mistakes that exist in such languages.^
逻辑 XOR|
逻辑 OR更新
在 C# 中也有其逻辑操作:
^
Logical XOR|
Logical ORupdate
in c# its logic operations too: