为什么 Java 和 C# 中的逻辑运算符和按位运算符之间存在区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14114179/
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-10-31 15:07:04  来源:igfitidea点击:

Why is there a distinction between logical and bitwise operators in Java and C#?

c#javabitwise-operatorslogical-operators

提问by Askaga

Languages like i.e. Java and C# have both bitwise and logical operators.

像 ie Java 和 C# 这样的语言既有按位运算符,也有逻辑运算符。

Logical operators make only sense with boolean operands, bitwise operators work with integer types as well. Since C had no boolean type and treats all non-zero integers as true, the existence of both logical and bitwise operators makes sense there. However, languages like Java or C# have a boolean type so the compiler could automatically use the right kind of operators, depending on the type context.

逻辑运算符只对布尔操作数有意义,按位运算符也适用于整数类型。由于 C 没有布尔类型并将所有非零整数视为真,因此逻辑和位运算符的存在在那里是有意义的。但是,Java 或 C# 等语言具有布尔类型,因此编译器可以根据类型上下文自动使用正确类型的运算符。

So, is there some concrete reason for having both logical and bitwise operators in those languages? Or were they just included for familiarity reasons?

那么,在这些语言中同时使用逻辑运算符和按位运算符有什么具体原因吗?或者他们只是出于熟悉的原因包括在内?

(I am aware that you can use the "bitwise" operators in a boolean context to circumvent the short-circuiting in Java and C#, but i have never needed such a behaviour, so i guess it might be a mostly unused special case)

(我知道您可以在布尔上下文中使用“按位”运算符来规避 Java 和 C# 中的短路,但我从来不需要这样的行为,所以我想这可能是一个大部分未使用的特殊情况)

回答by Henk Holterman

1) is there some concrete reason for having both logical and bitwise operators in those languages?

1) 在这些语言中同时使用逻辑运算符和按位运算符是否有一些具体的原因?

Yes:

是的:

  • We have boolean operators to do boolean logic (on boolean values).
  • We have bitwise operators to do bitwise logic (on integer values).
  • 我们有布尔运算符来执行布尔逻辑(在布尔值上)。
  • 我们有按位运算符来执行按位逻辑(对整数值)。

2) I am aware that you can use the "bitwise" operators in a boolean context to circumvent the short-circuiting in Java and C#,

2)我知道您可以在布尔上下文中使用“按位”运算符来规避 Java 和 C# 中的短路,

For as far as C# goes this simply is not true.
C# has for example 2 boolean AND operators: &(full) and &&(short) but it does not allow bitwise operations on booleans.

就 C# 而言,这根本不是真的。
例如,C# 有 2 个布尔 AND 运算符:(&完整)和&&(短),但它不允许对布尔值进行按位运算。

So, there really is no 'overlap' or redundancy between logical and bitwise operators. The two do not apply to the same types.

因此,逻辑运算符和按位运算符之间确实没有“重叠”或冗余。两者不适用于同一类型。

回答by Keith Nicholas

in C#, with booleans

在 C# 中,使用布尔值

  • && is a short circuiting logical operator
  • & is a non short circuiting logical operator
  • && 是一个短路逻辑运算符
  • & 是一个非短路逻辑运算符

bitwise, it just uses & as a legacy syntax from C / C++.... but it's really quite different. If anything, it would be better as a completely different symbol to avoid any confustion. But there aren't really many left, unless you wanted to go for &&& or ||| but thats a bit ugly.

按位,它只是使用 & 作为 C/C++ 的遗留语法......但它真的很不同。如果有的话,最好作为一个完全不同的符号来避免任何混淆。但是剩下的真的不多了,除非你想选择 &&& 或 ||| 但这有点难看。

回答by candied_orange

Late answer, but I'll try to get to your real point.

迟到的答案,但我会尽力达到你的真正观点。

You are correct. And the easiest way to make your point is to mention that other typed languages (like Visual Basic) have logical operators that can act on both Boolean and Integer expressions.

你是对的。表达观点的最简单方法是提及其他类型语言(如 Visual Basic)具有可以作用于布尔表达式和整数表达式的逻辑运算符。

VB Or operator: http://msdn.microsoft.com/en-us/library/06s37a7f.aspx

VB Bitwise example: http://visualbasic.about.com/od/usingvbnet/a/bitops01_2.htm

VB 或运营商:http: //msdn.microsoft.com/en-us/library/06s37a7f.aspx

VB 按位示例:http: //visualbasic.about.com/od/usingvbnet/a/bitops01_2.htm

This was very much a language design decision. Java and C# didn't have to be the way they are. They just are the way they are. Java and C# did indeed inherit much of their syntax from C for the sake of familiarity. Other languages didn't and work just fine.

这在很大程度上是一个语言设计决定。Java 和 C# 不一定是它们的样子。他们就是这样。为熟悉起见,Java 和 C# 确实从 C 继承了大部分语法。其他语言没有并且工作得很好。

A design decision like this has consequences. Short circuit evaluation is one. Disallowing mixed types (which can be confusing for humans to read) is another. I've come to like it but maybe I've just been staring at Java for too long.

像这样的设计决策会产生后果。短路评估就是其中之一。禁止混合类型(这可能会让人难以阅读)是另一个问题。我开始喜欢它,但也许我只是盯着 Java 太久了。

Visual Basic added AndAlso and OrElse as a way to do short circuit evaluation. Unlike basics other logical operators these work only on Booleans.

Visual Basic 添加了 AndAlso 和 OrElse 作为进行短路评估的一种方式。与基本的其他逻辑运算符不同,这些运算符仅适用于布尔值。

VB OrElse: http://msdn.microsoft.com/en-us/library/ea1sssb2.aspx

Short-circuit description: http://support.microsoft.com/kb/817250

VB 或其他:http: //msdn.microsoft.com/en-us/library/ea1sssb2.aspx

短路说明:http: //support.microsoft.com/kb/817250

The distinction wasn't made because strong typing makes it impossible to only have one set of logic operators in a language. It was made because they wanted short circuit evaluation and they wanted a clear way to signal to the reader what was happening.

之所以没有区别,是因为强类型使得在一种语言中不可能只有一组逻辑运算符。之所以制作它,是因为他们想要进行短路评估,并且他们想要一种清晰的方式向读者传达正在发生的事情。

The other reason (besides short circuit) that c and c++ have different kinds of logical operators is to allow any non-zero number to be reinterpreted as TRUE and zero reinterpreted as FALSE. To do that they need operators that tell them to interpret that way. Java rejects this whole reinterpret idea and throws an error in your face if you try to do that using it's logical operators. If it wasn't for short circuit evaluation the only reason left would simply be because they wanted the operators to look different when doing different things.

c 和 c++ 具有不同类型的逻辑运算符的另一个原因(除了短路)是允许将任何非零数字重新解释为 TRUE,将零重新解释为 FALSE。要做到这一点,他们需要告诉他们以这种方式解释的操作员。Java 拒绝整个重新解释的想法,如果您尝试使用它的逻辑运算符来执行此操作,则会在您面前抛出错误。如果不是为了短路评估,剩下的唯一原因就是因为他们希望操作员在做不同的事情时看起来不同。

So yes, if the Java and C# language designers hadn't cared about any of that they could have used one set of logical operators for both bitwise and boolean logic and figured out which to do based on operand type like some other languages do. They just didn't.

所以是的,如果 Java 和 C# 语言设计者不关心其中的任何一个,他们可以使用一组逻辑运算符来处理位逻辑和布尔逻辑,并像其他一些语言一样根据操作数类型找出要执行的操作。他们只是没有。

回答by madhead

I'll say for Java

我会说Java

  • Logical operator are user with booleans and bitwise operators are used with ints. They can't be mixed.
  • Why not reduce them to one operator such as "&" or "|"? Java was designed to be friendly for C/C++ users, so it got their syntax. Nowadays these operators cannot be reduced because of backwards compatibility.
  • 逻辑运算符是使用布尔值的用户,按位运算符与整数一起使用。它们不能混合。
  • 为什么不将它们简化为一个运算符,例如“&”或“|”?Java 被设计为对 C/C++ 用户友好,所以它有他们的语法。如今,由于向后兼容,无法减少这些运算符。

回答by Radu Stoenescu

As you have already said, there's some difference between &and &&(the same goes for |and ||) so you need two sets of boolean operators. Now, independently from those above, you may need bitwise operators and the best choice is &, |s.o. since you don't have to avoid any confusion.

正如您已经说过的,&and之间存在一些差异&&|and 也是||如此),因此您需要两组布尔运算符。现在,独立于上述那些,您可能需要按位运算符,最好的选择是&|因此您不必避免任何混淆。



Why complicate things and use the two-character version ?

为什么把事情复杂化并使用两个字符的版本?

回答by piotrek

compiler cannot infer proper operator looking only at arguments. it's a business decision which one to choose. it's about lazy calculations. e.g.

编译器无法推断出仅查看参数的正确运算符。选择哪一个是商业决策。这是关于懒惰的计算。例如

public boolean a() {
  doStuffA();
  return false;
}

public boolean b() {
  doStuffB();
  return true;
}

and now: a() & b()will execute doStuffB()while a() && b()will not

而现在: a() & b()将执行doStuffB()a() && b()不会