|和||或运营商有什么区别?

时间:2022-09-06 13:22:06

I have always used || (two pipes) in OR expressions, both in C# and PHP. Occasionally I see a single pipe used: |. What is the difference between those two usages? Are there any caveats when using one over the other or are they interchangeable?

我一直在c#和PHP中使用||(两个管道)或表达式。有时我看到使用了一个管道:|。这两种用法有什么不同?在使用一个或多个时,是否有任何注意事项?

13 个解决方案

#1


395  

Just like the & and && operator, the double Operator is a "short-circuit" operator.

就像&和&&操作者,双重操作者是一个“短路”操作者。

For example:

例如:

if(condition1 || condition2 || condition3)

If condition1 is true, condition 2 and 3 will NOT be checked.

如果条件1为真,则不检查条件2和条件3。

if(condition1 | condition2 | condition3)

This will check conditions 2 and 3, even if 1 is already true. As your conditions can be quite expensive functions, you can get a good performance boost by using them.

这将检查条件2和3,即使1已经为真。由于您的条件可能非常昂贵,您可以通过使用它们来获得良好的性能提升。

There is one big caveat, NullReferences or similar problems. For example:

这里有一个很大的警告,无效引用或类似的问题。例如:

if(class != null && class.someVar < 20)

If class is null, the if-statement will stop after class != null is false. If you only use &, it will try to check class.someVar and you get a nice NullReferenceException. With the Or-Operator that may not be that much of a trap as it's unlikely that you trigger something bad, but it's something to keep in mind.

如果类为空,那么If语句将在类之后停止!= null是假的。如果您只使用&,它将尝试检查类。someVar会得到一个很好的NullReferenceException。对于or运算符来说,这可能不是一个陷阱,因为你不太可能触发不好的事情,但这是需要记住的事情。

No one ever uses the single & or | operators though, unless you have a design where each condition is a function that HAS to be executed. Sounds like a design smell, but sometimes (rarely) it's a clean way to do stuff. The & operator does "run these 3 functions, and if one of them returns false, execute the else block", while the | does "only run the else block if none return false" - can be useful, but as said, often it's a design smell.

不过,没有人会使用单个&或|操作符,除非您有一个设计,其中每个条件都是必须执行的函数。听起来像是一种设计味道,但有时(很少)这是一种干净的做事方式。&操作符“运行这3个函数,如果其中一个返回false,执行else块”,而|只运行else块(如果没有返回false,则只运行else块)。

There is a Second use of the | and & operator though: Bitwise Operations.

|和&操作符还有另一个用途:位操作。

#2


67  

|| is the logical OR operator. It sounds like you basically know what that is. It's used in conditional statements such as if, while, etc.

||是逻辑或运算符。听起来你基本上知道那是什么。它用于条件语句,如if、while等。

condition1 || condition2

Evaluates to true if either condition1 OR condition2 is true.

如果条件1或条件2为真,则计算为true。

| is the bitwise OR operator. It's used to operate on two numbers. You look at each bit of each number individually and, if one of the bits is 1 in at least one of the numbers, then the resulting bit will be 1 also. Here are a few examples:

|是位运算符。它被用来对两个数字进行运算。你单独看每个数字的每一个位,如果其中的一个位至少在一个数字中是1,那么结果的位也是1。这里有几个例子:

A = 01010101
B = 10101010
A | B = 11111111

A = 00000001
B = 00010000
A | B = 00010001

A = 10001011
B = 00101100

A | B = 10101111

Hopefully that makes sense.

希望这是有意义的。

So to answer the last two questions, I wouldn't say there are any caveats besides "know the difference between the two operators." They're not interchangeable because they do two completely different things.

为了回答最后两个问题,除了“知道这两个运算符之间的区别”之外,我不认为还有什么需要注意的地方。它们不能互换,因为它们做了两件完全不同的事。

#3


27  

One is a "bitwise or".

一个是“bitwise or”。

10011b | 01000b => 11011b

| 01000b => 11011b

The other is a logic or.

另一个是逻辑或。

true or false => true

真或假=>真

#4


10  

Good question. These two operators work the same in PHP and C#.

好问题。这两个操作符在PHP和c#中是相同的。

| is a bitwise OR. It will compare two values by their bits. E.g. 1101 | 0010 = 1111. This is extremely useful when using bit options. E.g. Read = 01 (0X01) Write = 10 (0X02) Read-Write = 11 (0X03). One useful example would be opening files. A simple example would be:

|是位或。它将用它们的位来比较两个值。例如,1101 | 0010 = 1111。这在使用位选项时非常有用。例如,Read = 01 (0X01) Write = 10 (0X02) Read-Write = 11 (0X03)。一个有用的例子是打开文件。一个简单的例子是:

File.Open(FileAccess.Read | FileAccess.Write);  //Gives read/write access to the file

|| is a logical OR. This is the way most people think of OR and compares two values based on their truth. E.g. I am going to the store or I will go to the mall. This is the one used most often in code. E.g.

||是一个逻辑OR。这是大多数人基于他们的真理来思考或比较两种价值观的方式。我要去商店,否则我要去购物中心。这是在代码中最常用的。如。

if(Name == "Admin" || Name == "Developer) { //allow access } //checks if name equals Admin OR Name equals Developer

PHP Resource: http://us3.php.net/language.operators.bitwise

PHP资源:http://us3.php.net/language.operators.bitwise

C# Resources: http://msdn.microsoft.com/en-us/library/kxszd0kx(VS.71).aspx

c#资源:http://msdn.microsoft.com/en-us/library/kxszd0kx(VS.71). aspx

http://msdn.microsoft.com/en-us/library/6373h346(VS.71).aspx

http://msdn.microsoft.com/en-us/library/6373h346(VS.71). aspx

#5


4  

Simple example in java

简单的例子在java中

public class Driver {

  static int x;
  static int y;

public static void main(String[] args) 
throws Exception {

System.out.println("using double pipe");
    if(setX() || setY())
        {System.out.println("x = "+x);
        System.out.println("y = "+y);
        }



System.out.println("using single pipe");
if(setX() | setY())
    {System.out.println("x = "+x);
    System.out.println("y = "+y);
    }

}

 static boolean setX(){
      x=5;
     return true;
  }
 static boolean setY(){
      y=5;
      return true;
  }
}

output :

输出:

using double pipe
x = 5
y = 0
using single pipe
x = 5
y = 5

#6


1  

The single pipe, |, is one of the bitwise operators.

单管|是位运算符之一。

From Wikipedia:

从*:

In the C programming language family, the bitwise OR operator is "|" (pipe). Again, this operator must not be confused with its Boolean "logical or" counterpart, which treats its operands as Boolean values, and is written "||" (two pipes).

在C编程语言族中,位或操作符是“|”(管道)。同样,这个操作符不能与它的布尔逻辑“逻辑或”对等体混淆,后者将其操作数视为布尔值,并写入“||”(两个管道)。

#7


1  

& - (Condition 1 & Condition 2): checks both cases even if first one is false

& -(条件1和条件2):检查两种情况,即使第一个是假的

&& - (Condition 1 && Condition 2): dosen't bother to check second case if case one is false

&& &(条件1和条件2):如果条件1是假的,不要麻烦检查第二个情况

&& - operator will make your code run faster, professionally & is rarely used

&&操作员将使您的代码运行得更快,专业&很少使用

| - (Condition 1 | Condition 2): checks both cases even if case 1 is true

| -(条件1 |条件2):检查两种情况,即使情况1为真

|| - (Condition 1 || Condition 2): dosen't bother to check second case if first one is true

|| -(条件1 ||条件2):如果第一个条件为真,不要费事检查第二个条件

|| - operator will make your code run faster, professionally | is rarely used

|| -操作者将使您的代码运行得更快,专业的|很少被使用

#8


0  

The | operator performs a bitwise OR of its two operands (meaning both sides must evaluate to false for it to return false) while the || operator will only evaluate the second operator if it needs to.

|操作符执行一个位运算符或它的两个操作数(意味着两边都必须计算为false才能返回false),而||操作符只在需要时计算第二个操作符。

http://msdn.microsoft.com/en-us/library/kxszd0kx(VS.71).aspx

http://msdn.microsoft.com/en-us/library/kxszd0kx(VS.71). aspx

http://msdn.microsoft.com/en-us/library/6373h346(VS.71).aspx

http://msdn.microsoft.com/en-us/library/6373h346(VS.71). aspx

#9


0  

The singe pipe "|" is the "bitwise" or and should only be used when you know what you're doing. The double pipe "||" is a logical or, and can be used in logical statements, like "x == 0 || x == 1".

singe pipe“|”是“bitwise”,只有当你知道自己在做什么时才应该使用。双管“||”是一个逻辑或,可以用于逻辑语句,如“x = 0 || x = 1”。

Here's an example of what the bitwise or does: if a=0101 and b=0011, then a|b=0111. If you're dealing with a logic system that treats any non-zero as true, then the bitwise or will act in the same way as the logical or, but it's counterpart (bitwise and, "&") will NOT. Also the bitwise or does not perform short circuit evaluation.

下面是位运算的一个例子:如果a=0101和b=0011,那么|b=0111。如果您正在处理一个逻辑系统,该逻辑系统将任何非零都视为真,那么位或将以与逻辑or相同的方式操作,但它的对等物(位和“&”)将不会。同样,按位或不执行短路评估。

#10


0  

By their mathematical definition, OR and AND are binary operators; they verify the LHS and RHS conditions regardless, similarly to | and &.

根据它们的数学定义,或和是二进制运算符;无论如何,他们都验证LHS和RHS条件,类似于|和&。

|| and && alter the properties of the OR and AND operators by stopping them when the LHS condition isn't fulfilled.

||和&改变OR和操作符的属性,当LHS条件不满足时停止它们。

#11


-1  

A single pipe (|) is the bitwise OR operator.

单管(|)是位运算符。

Two pipes (||) is the logical OR operator.

两个管道(||)是逻辑或操作符。

They are not interchangeable.

他们不可以互换。

#12


-1  

|| (two pipes) is usually a logical or while | (one pipe) is a binary or. Off the top of my head, I can't think of any time the difference would be a big gotcha (other than when you're assigning the result to something else). However I sure someone else will have a situation where it matters.

||(两个管道)通常是逻辑的,而|(一个管道)则是二进制或。在我的脑海中,我想不出任何时候的差异会是一个大问题(除了当你把结果分配给其他东西时)。不过,我肯定别人会有这样的情况。

Edit: Wow, six other answers in the time it took me to write this.

编辑:哇,在我写这篇文章的时间里,还有六个答案。

#13


-1  

Bitwise (|) vs. logical(||)! Think of logical as the Comparable objects in Java, comparing some distinguishable "parts" while the bitwise operator looks at these objects and instead of seeing if they are visually twins (like logical does), does a DNA sample and looks at the 0's and 1's instead.

位(|)与逻辑(| |)!将逻辑看作Java中的可比对象,比较一些可区分的“部分”,而位运算符则观察这些对象,而不是观察它们是否在视觉上是孪生的(就像逻辑一样),而是做一个DNA样本,然后观察0和1。

#1


395  

Just like the & and && operator, the double Operator is a "short-circuit" operator.

就像&和&&操作者,双重操作者是一个“短路”操作者。

For example:

例如:

if(condition1 || condition2 || condition3)

If condition1 is true, condition 2 and 3 will NOT be checked.

如果条件1为真,则不检查条件2和条件3。

if(condition1 | condition2 | condition3)

This will check conditions 2 and 3, even if 1 is already true. As your conditions can be quite expensive functions, you can get a good performance boost by using them.

这将检查条件2和3,即使1已经为真。由于您的条件可能非常昂贵,您可以通过使用它们来获得良好的性能提升。

There is one big caveat, NullReferences or similar problems. For example:

这里有一个很大的警告,无效引用或类似的问题。例如:

if(class != null && class.someVar < 20)

If class is null, the if-statement will stop after class != null is false. If you only use &, it will try to check class.someVar and you get a nice NullReferenceException. With the Or-Operator that may not be that much of a trap as it's unlikely that you trigger something bad, but it's something to keep in mind.

如果类为空,那么If语句将在类之后停止!= null是假的。如果您只使用&,它将尝试检查类。someVar会得到一个很好的NullReferenceException。对于or运算符来说,这可能不是一个陷阱,因为你不太可能触发不好的事情,但这是需要记住的事情。

No one ever uses the single & or | operators though, unless you have a design where each condition is a function that HAS to be executed. Sounds like a design smell, but sometimes (rarely) it's a clean way to do stuff. The & operator does "run these 3 functions, and if one of them returns false, execute the else block", while the | does "only run the else block if none return false" - can be useful, but as said, often it's a design smell.

不过,没有人会使用单个&或|操作符,除非您有一个设计,其中每个条件都是必须执行的函数。听起来像是一种设计味道,但有时(很少)这是一种干净的做事方式。&操作符“运行这3个函数,如果其中一个返回false,执行else块”,而|只运行else块(如果没有返回false,则只运行else块)。

There is a Second use of the | and & operator though: Bitwise Operations.

|和&操作符还有另一个用途:位操作。

#2


67  

|| is the logical OR operator. It sounds like you basically know what that is. It's used in conditional statements such as if, while, etc.

||是逻辑或运算符。听起来你基本上知道那是什么。它用于条件语句,如if、while等。

condition1 || condition2

Evaluates to true if either condition1 OR condition2 is true.

如果条件1或条件2为真,则计算为true。

| is the bitwise OR operator. It's used to operate on two numbers. You look at each bit of each number individually and, if one of the bits is 1 in at least one of the numbers, then the resulting bit will be 1 also. Here are a few examples:

|是位运算符。它被用来对两个数字进行运算。你单独看每个数字的每一个位,如果其中的一个位至少在一个数字中是1,那么结果的位也是1。这里有几个例子:

A = 01010101
B = 10101010
A | B = 11111111

A = 00000001
B = 00010000
A | B = 00010001

A = 10001011
B = 00101100

A | B = 10101111

Hopefully that makes sense.

希望这是有意义的。

So to answer the last two questions, I wouldn't say there are any caveats besides "know the difference between the two operators." They're not interchangeable because they do two completely different things.

为了回答最后两个问题,除了“知道这两个运算符之间的区别”之外,我不认为还有什么需要注意的地方。它们不能互换,因为它们做了两件完全不同的事。

#3


27  

One is a "bitwise or".

一个是“bitwise or”。

10011b | 01000b => 11011b

| 01000b => 11011b

The other is a logic or.

另一个是逻辑或。

true or false => true

真或假=>真

#4


10  

Good question. These two operators work the same in PHP and C#.

好问题。这两个操作符在PHP和c#中是相同的。

| is a bitwise OR. It will compare two values by their bits. E.g. 1101 | 0010 = 1111. This is extremely useful when using bit options. E.g. Read = 01 (0X01) Write = 10 (0X02) Read-Write = 11 (0X03). One useful example would be opening files. A simple example would be:

|是位或。它将用它们的位来比较两个值。例如,1101 | 0010 = 1111。这在使用位选项时非常有用。例如,Read = 01 (0X01) Write = 10 (0X02) Read-Write = 11 (0X03)。一个有用的例子是打开文件。一个简单的例子是:

File.Open(FileAccess.Read | FileAccess.Write);  //Gives read/write access to the file

|| is a logical OR. This is the way most people think of OR and compares two values based on their truth. E.g. I am going to the store or I will go to the mall. This is the one used most often in code. E.g.

||是一个逻辑OR。这是大多数人基于他们的真理来思考或比较两种价值观的方式。我要去商店,否则我要去购物中心。这是在代码中最常用的。如。

if(Name == "Admin" || Name == "Developer) { //allow access } //checks if name equals Admin OR Name equals Developer

PHP Resource: http://us3.php.net/language.operators.bitwise

PHP资源:http://us3.php.net/language.operators.bitwise

C# Resources: http://msdn.microsoft.com/en-us/library/kxszd0kx(VS.71).aspx

c#资源:http://msdn.microsoft.com/en-us/library/kxszd0kx(VS.71). aspx

http://msdn.microsoft.com/en-us/library/6373h346(VS.71).aspx

http://msdn.microsoft.com/en-us/library/6373h346(VS.71). aspx

#5


4  

Simple example in java

简单的例子在java中

public class Driver {

  static int x;
  static int y;

public static void main(String[] args) 
throws Exception {

System.out.println("using double pipe");
    if(setX() || setY())
        {System.out.println("x = "+x);
        System.out.println("y = "+y);
        }



System.out.println("using single pipe");
if(setX() | setY())
    {System.out.println("x = "+x);
    System.out.println("y = "+y);
    }

}

 static boolean setX(){
      x=5;
     return true;
  }
 static boolean setY(){
      y=5;
      return true;
  }
}

output :

输出:

using double pipe
x = 5
y = 0
using single pipe
x = 5
y = 5

#6


1  

The single pipe, |, is one of the bitwise operators.

单管|是位运算符之一。

From Wikipedia:

从*:

In the C programming language family, the bitwise OR operator is "|" (pipe). Again, this operator must not be confused with its Boolean "logical or" counterpart, which treats its operands as Boolean values, and is written "||" (two pipes).

在C编程语言族中,位或操作符是“|”(管道)。同样,这个操作符不能与它的布尔逻辑“逻辑或”对等体混淆,后者将其操作数视为布尔值,并写入“||”(两个管道)。

#7


1  

& - (Condition 1 & Condition 2): checks both cases even if first one is false

& -(条件1和条件2):检查两种情况,即使第一个是假的

&& - (Condition 1 && Condition 2): dosen't bother to check second case if case one is false

&& &(条件1和条件2):如果条件1是假的,不要麻烦检查第二个情况

&& - operator will make your code run faster, professionally & is rarely used

&&操作员将使您的代码运行得更快,专业&很少使用

| - (Condition 1 | Condition 2): checks both cases even if case 1 is true

| -(条件1 |条件2):检查两种情况,即使情况1为真

|| - (Condition 1 || Condition 2): dosen't bother to check second case if first one is true

|| -(条件1 ||条件2):如果第一个条件为真,不要费事检查第二个条件

|| - operator will make your code run faster, professionally | is rarely used

|| -操作者将使您的代码运行得更快,专业的|很少被使用

#8


0  

The | operator performs a bitwise OR of its two operands (meaning both sides must evaluate to false for it to return false) while the || operator will only evaluate the second operator if it needs to.

|操作符执行一个位运算符或它的两个操作数(意味着两边都必须计算为false才能返回false),而||操作符只在需要时计算第二个操作符。

http://msdn.microsoft.com/en-us/library/kxszd0kx(VS.71).aspx

http://msdn.microsoft.com/en-us/library/kxszd0kx(VS.71). aspx

http://msdn.microsoft.com/en-us/library/6373h346(VS.71).aspx

http://msdn.microsoft.com/en-us/library/6373h346(VS.71). aspx

#9


0  

The singe pipe "|" is the "bitwise" or and should only be used when you know what you're doing. The double pipe "||" is a logical or, and can be used in logical statements, like "x == 0 || x == 1".

singe pipe“|”是“bitwise”,只有当你知道自己在做什么时才应该使用。双管“||”是一个逻辑或,可以用于逻辑语句,如“x = 0 || x = 1”。

Here's an example of what the bitwise or does: if a=0101 and b=0011, then a|b=0111. If you're dealing with a logic system that treats any non-zero as true, then the bitwise or will act in the same way as the logical or, but it's counterpart (bitwise and, "&") will NOT. Also the bitwise or does not perform short circuit evaluation.

下面是位运算的一个例子:如果a=0101和b=0011,那么|b=0111。如果您正在处理一个逻辑系统,该逻辑系统将任何非零都视为真,那么位或将以与逻辑or相同的方式操作,但它的对等物(位和“&”)将不会。同样,按位或不执行短路评估。

#10


0  

By their mathematical definition, OR and AND are binary operators; they verify the LHS and RHS conditions regardless, similarly to | and &.

根据它们的数学定义,或和是二进制运算符;无论如何,他们都验证LHS和RHS条件,类似于|和&。

|| and && alter the properties of the OR and AND operators by stopping them when the LHS condition isn't fulfilled.

||和&改变OR和操作符的属性,当LHS条件不满足时停止它们。

#11


-1  

A single pipe (|) is the bitwise OR operator.

单管(|)是位运算符。

Two pipes (||) is the logical OR operator.

两个管道(||)是逻辑或操作符。

They are not interchangeable.

他们不可以互换。

#12


-1  

|| (two pipes) is usually a logical or while | (one pipe) is a binary or. Off the top of my head, I can't think of any time the difference would be a big gotcha (other than when you're assigning the result to something else). However I sure someone else will have a situation where it matters.

||(两个管道)通常是逻辑的,而|(一个管道)则是二进制或。在我的脑海中,我想不出任何时候的差异会是一个大问题(除了当你把结果分配给其他东西时)。不过,我肯定别人会有这样的情况。

Edit: Wow, six other answers in the time it took me to write this.

编辑:哇,在我写这篇文章的时间里,还有六个答案。

#13


-1  

Bitwise (|) vs. logical(||)! Think of logical as the Comparable objects in Java, comparing some distinguishable "parts" while the bitwise operator looks at these objects and instead of seeing if they are visually twins (like logical does), does a DNA sample and looks at the 0's and 1's instead.

位(|)与逻辑(| |)!将逻辑看作Java中的可比对象,比较一些可区分的“部分”,而位运算符则观察这些对象,而不是观察它们是否在视觉上是孪生的(就像逻辑一样),而是做一个DNA样本,然后观察0和1。