有哪些|和 ^ 运算符用于什么?

发布于 2024-09-19 20:51:30 字数 299 浏览 9 评论 0原文

可能的重复:
什么是按位运算符?

最近我遇到了一些使用 |< 的示例/code> 和 ^ 运算符。我猜这些是 ornegation 运算符。

那么这些运算符到底代表什么?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

中性美 2024-09-26 20:51:30
  • | =(按位/非短路)“or”
  • ^ =“xor”

,作为信息,“not”(按位求反)是 ~< /代码>

  • | = (bitwise/non-short-circuiting) "or"
  • ^ = "xor"

and for info, "not" (bitwise negation) is ~

疑心病 2024-09-26 20:51:30

如果将其应用于整数类型,它们是按位或和异或运算符。
但是,如果将它们应用于布尔类型,它们是逻辑或和异或。
查看或运算符的说明并异或运算符

您可以了解更多详细信息来自维基百科的布尔运算真值表

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

习ぎ惯性依靠 2024-09-26 20:51:30

MSDN 上有关于所有 C# 运算符的文档:

http://msdn.microsoft。 com/en-us/library/6a71f45d.aspx


编辑 - Jon B 评论说链接文档中的相关引用会很有用。

|逻辑或运算符

二进制 |运算符是为整数类型和布尔值预定义的。对于整数类型,|计算其操作数的按位或。对于布尔操作数,|计算其操作数的逻辑或;也就是说,当且仅当两个操作数都为 false 时,结果才为 false。

^逻辑异或运算符

二元 ^ 运算符是为整数类型和布尔值预定义的。对于整型,^ 计算其操作数的按位异或。对于 bool 操作数,^ 计算其操作数的逻辑异或;也就是说,当且仅当其操作数之一为 true 时,结果才为 true。

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.

Binary | operators are predefined for the integral types and bool. For integral types, | computes the bitwise OR of its operands. For bool operands, | computes the logical OR of its operands; that is, the result is false if and only if both its operands are false.

^ is the Logical XOR operator.

Binary ^ operators are predefined for the integral types and bool. For integral types, ^ computes the bitwise exclusive-OR of its operands. For bool operands, ^ computes the logical exclusive-or of its operands; that is, the result is true if and only if exactly one of its operands is true.

鹤仙姿 2024-09-26 20:51:30

它们是逻辑按位运算符

|是一个 Or 链接

^ 是 XOR 链接

they are logical bitwise operators

| is a Or link

^ is XOR link

挽手叙旧 2024-09-26 20:51:30

对于整数类型,|是按位或, ^ 按位异或,为了完整起见 &是按位与。

对于布尔类型,|是布尔或,^ 布尔异或,&布尔值 &.

相比之下,||是一个短路布尔值或 - 如果第一个操作数计算为 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 as if(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.

千里故人稀 2024-09-26 20:51:30

^ 逻辑 XOR

| 逻辑 OR

更新

在 C# 中也有其逻辑操作:

    Console.WriteLine(true ^ false);  // logical exclusive-or
    Console.WriteLine(false ^ false); // logical exclusive-or

^ Logical XOR

| Logical OR

update

in c# its logic operations too:

    Console.WriteLine(true ^ false);  // logical exclusive-or
    Console.WriteLine(false ^ false); // logical exclusive-or
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文