什么是|和^运算符用于? [重复]

时间:2022-11-26 10:48:03

Possible Duplicate:
What are bitwise operators?

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

Recently I came across a few samples that used the | and ^ operator. I am guessing these are or and negation operators.

最近我遇到了一些使用了|的样本和^运算符。我猜这些是或否定运算符。

So what actually do these operators stands for?

那么这些运营商究竟代表什么呢?

6 个解决方案

#1


17  

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

如果将它应用于整数类型,则它们是按位或xor运算符。但是,如果将它们应用于布尔类型,则它们是逻辑或xor。看一下或运算符和xor运算符的解释

You can get more details on boolean operations from the wikipedia truth table

您可以从*真值表中获取有关布尔运算的更多详细信息

#2


25  

  • | = (bitwise/non-short-circuiting) "or"
  • | =(按位/非短路)“或”

  • ^ = "xor"
  • ^ =“xor”

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

而对于信息,“不”(按位否定)是〜

#3


7  

MSDN has documentation on all the C# operators at:

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

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


EDIT - Jon B commented that a relevant quote from the linked documentation would be useful.

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

| is the Logical OR operator.

|是逻辑OR运算符。

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.

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

^ is the Logical XOR operator.

^是逻辑XOR运算符。

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.

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

#4


2  

they are logical bitwise operators

它们是逻辑按位运算符

| is a Or link

|是一个或链接

^ is XOR link

^是异或链接

#5


2  

With integral types, | is a bitwise or, ^ a bitwise xor and for completeness & is a bitwise and.

有积分类型,|是一个按位或,^一个按位xor和完整性&是一个按位和。

With boolean types, | is a boolean or, ^ a boolean xor and & a boolean &.

使用布尔类型,|是一个布尔值,或者是一个布尔值xor和一个布尔值&。

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.

相比之下,||是短路布尔值 - 如果第一个操作数的计算结果为true,则不计算第二个操作数。 &&是一个短路布尔值 - 如果第一个操作数为假,则不计算第二个操作数。没有短路^因为不存在不需要评估第二个的情况。

|| 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.

编辑:潜在混淆的一个原因是,在其他一些语言中,整数类型可以用作布尔值(例如,你可以做(​​53)并且它与if(true)相同)这使得上述运算符之间的区别大不相同:如果使用“纯粹的”布尔类型(它只有true和false作为其可能的值),它们是相同的,但不是这样。 C#故意不允许对整数类型进行精确的布尔运算,以防止这些语言中存在错误的可能性。

#6


-6  

^ Logical XOR

^逻辑异或

| Logical OR

|逻辑或

update

in c# its logic operations too:

在c#中它的逻辑运算:

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

#1


17  

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

如果将它应用于整数类型,则它们是按位或xor运算符。但是,如果将它们应用于布尔类型,则它们是逻辑或xor。看一下或运算符和xor运算符的解释

You can get more details on boolean operations from the wikipedia truth table

您可以从*真值表中获取有关布尔运算的更多详细信息

#2


25  

  • | = (bitwise/non-short-circuiting) "or"
  • | =(按位/非短路)“或”

  • ^ = "xor"
  • ^ =“xor”

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

而对于信息,“不”(按位否定)是〜

#3


7  

MSDN has documentation on all the C# operators at:

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

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


EDIT - Jon B commented that a relevant quote from the linked documentation would be useful.

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

| is the Logical OR operator.

|是逻辑OR运算符。

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.

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

^ is the Logical XOR operator.

^是逻辑XOR运算符。

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.

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

#4


2  

they are logical bitwise operators

它们是逻辑按位运算符

| is a Or link

|是一个或链接

^ is XOR link

^是异或链接

#5


2  

With integral types, | is a bitwise or, ^ a bitwise xor and for completeness & is a bitwise and.

有积分类型,|是一个按位或,^一个按位xor和完整性&是一个按位和。

With boolean types, | is a boolean or, ^ a boolean xor and & a boolean &.

使用布尔类型,|是一个布尔值,或者是一个布尔值xor和一个布尔值&。

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.

相比之下,||是短路布尔值 - 如果第一个操作数的计算结果为true,则不计算第二个操作数。 &&是一个短路布尔值 - 如果第一个操作数为假,则不计算第二个操作数。没有短路^因为不存在不需要评估第二个的情况。

|| 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.

编辑:潜在混淆的一个原因是,在其他一些语言中,整数类型可以用作布尔值(例如,你可以做(​​53)并且它与if(true)相同)这使得上述运算符之间的区别大不相同:如果使用“纯粹的”布尔类型(它只有true和false作为其可能的值),它们是相同的,但不是这样。 C#故意不允许对整数类型进行精确的布尔运算,以防止这些语言中存在错误的可能性。

#6


-6  

^ Logical XOR

^逻辑异或

| Logical OR

|逻辑或

update

in c# its logic operations too:

在c#中它的逻辑运算:

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