C# | 之间的区别 和 || 或 & 和 && 进行比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1279217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:37:00  来源:igfitidea点击:

Difference between | and || or & and && for comparison

c#comparisonoperators

提问by Josh Mein

Possible Duplicate:
A clear, layman’s explanation of the difference between | and || in c# ?

可能的重复:
对 | 之间差异的清晰,外行解释 和 || 在 C# 中?

What is the difference between comparing with | and || or & and && in C# and Javascript?

和 | 比较有什么区别?和 || 或 & 和 && 在 C# 和 Javascript 中?

Examples:

例子:

if(test == test1 | test1 == test2) or if(test == test1 || test1 == test2)
if(test == test1 & test1 == test2) or if(test == test1 && test1 == test2)

采纳答案by dustmachine

in C (and other languages probably) a single |or &is a bitwise comparison.
The double ||or &&is a logical comparison.
Edit: Be sure to read Mehrdad's comment below regarding "without short-circuiting"

在 C(可能还有其他语言)中,单个|&按位比较。
double ||or&&是一个逻辑比较。
编辑:请务必阅读下面关于“无短路”的 Mehrdad 评论

In practice, since trueis often equivalent to 1and falseis often equivalent to 0, the bitwise comparisons can sometimes be valid and return exactly the same result.

在实践中,由于true通常等价于1并且false通常等价于0,按位比较有时可能是有效的,并返回完全相同的结果

There was once a mission critical software component I ran a static code analyzer on and it pointed out that a bitwise comparison was being used where a logical comparison should have been. Since it was written in C and due to the arrangement of logical comparisons, the software worked just fine with either. Example:

曾经有一个关键任务软件组件我运行了一个静态代码分析器,它指出在应该进行逻辑比较的地方使用了按位比较。由于它是用 C 编写的,并且由于逻辑比较的安排,该软件与任何一个都可以正常工作。例子:

if ( (altitide > 10000) & (knots > 100) )
...

回答by David

& and | are binary operators while || and && are boolean.

& 和 | 是二元运算符而 || 和 && 是布尔值。

The big difference:
(1 & 2) is 0, false
(1 && 2) is true

最大的区别:
(1 & 2) 为 0,false
(1 && 2) 为 true

回答by Zac

The instance in which you're using a single character (i.e. | or &) is a bitwise comparison of the results. As long as your language evaluates these expressions to a binary value they should return the same results. As a best practice, however, you should use the logical operator as that's what you mean (I think).

您使用单个字符(即 | 或 &)的实例是结果的按位比较。只要您的语言将这些表达式计算为二进制值,它们就应该返回相同的结果。但是,作为最佳实践,您应该使用逻辑运算符,因为这就是您的意思(我认为)。

回答by Graeme Perrow

(Assuming C, C++, Java, JavaScript)

(假设 C、C++、Java、JavaScript)

|and &are bitwise operators while ||and &&are logical operators. Usually you'd want to use ||and &&for if statements and loops and such (i.e. for your examples above). The bitwise operators are for setting and checking bits within bitmasks.

|and&是按位运算符,而||and&&是逻辑运算符。通常你会想要使用||and &&for if 语句和循环等(即上面的例子)。按位运算符用于设置和检查位掩码内的位。

回答by Richard Dunlap

& and | are bitwise operators that can operate on both integer and Boolean arguments, and && and || are logical operators that can operate only on Boolean arguments. In many languages, if both arguments are Boolean, the key difference is that the logical operators will perform short circuit evaluation and not evaluate the second argument if the first argument is enough to determine the answer (e.g. in the case of &&, if the first argument is false, the second argument is irrelevant).

& 和 | 是可以对整数和布尔参数进行运算的按位运算符,以及 && 和 || 是只能对布尔参数进行操作的逻辑运算符。在许多语言中,如果两个参数都是布尔值,关键区别在于如果第一个参数足以确定答案(例如在 && 的情况下,如果第一个参数),逻辑运算符将执行短路评估而不评估第二个参数论点为假,第二个论点无关紧要)。

回答by Daniel

The & and | are usually bitwise operations.

& 和 | 通常是按位运算。

Where as && and || are usually logical operations.

哪里作为 && 和 || 通常是逻辑运算。

For comparison purposes, it's perfectly fine provided that everything returns either a 1 or a 0. Otherwise, it can return false positives. You should avoid this though to prevent hard to read bugs.

出于比较的目的,只要一切都返回 1 或 0 就完全没问题。否则,它可能会返回误报。您应该避免这种情况,以防止难以阅读的错误。