复合比较运算符有什么意义?(What's the point of the compound comparison operators?)

编程入门 行业动态 更新时间:2024-10-27 23:19:06
复合比较运算符有什么意义?(What's the point of the compound comparison operators?)

我读过b1 |= b2是等同于b1 = b1 | b2的快捷方式 b1 = b1 | b2 。 我的问题实际上是双重的:

不是“=”赋值运算符而不是比较运算符? 如果是这样,它在这种情况下做了什么?

或者,它在这里是一个比较运算符,您需要将变量与自身进行比较(即b1 = b1 )?

(我意识到这可能是最新问题的新问题,但我只有非正式的经验来编写bash脚本而没有编程方面的教育背景。请善待。;)

注意:这是参考这里的问题: Java中的快捷方式“or-assignment”(| =)运算符

I have read that b1 |= b2 is the shortcut equivalent to b1 = b1 | b2. My question is really two-fold:

Isn't "=" an assignment operator rather than a comparison operator? If so, what does it do in this context?

Or, is it a comparison operator here and what scenario exists where you would need to compare a variable to itself (i.e. b1 = b1)?

(I realize this is probably the newbie-est newb question to ask, but I've only got informal experience writing bash scripts and no educational background in programming. Be kind. ;)

Note: this is in reference to the question here: Shortcut "or-assignment" (|=) operator in Java

最满意答案

Java中有许多运算符。 但“复合比较运算符”不是其中之一。 你应该从像“Head first Java”这样的好书中阅读Java基础知识。

要回答这个特定问题, b1 |= b2是复合赋值。

=将b1|b2的结果赋给LHS操作数,即b1 。 由于现在很清楚它是一个assignment运算符而不是比较,因此b1 |= b1的结果将与b1 = b1|b1 。

(注意|这里是两个数字之间的逻辑OR而不是|| ,它是一个条件运算符。 |和||有不同的含义)

HTH。

There are many operators in Java. But 'Compound comparison operator' is not one of them. You should read Java basics from a good book like 'Head first Java'.

To answer this particular question, b1 |= b2 is compound assignment.

= assigns the the result of b1|b2 to LHS operand i.e b1. Since it is clear now that it is an assignment operator not comparison, the result of b1 |= b1 will be same as b1 = b1|b1.

(Note | here is the logical OR between two numbers not || which is a conditional operator. | and || have different meanings)

HTH.

更多推荐

本文发布于:2023-08-03 01:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1382435.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么意义   运算符   point   operators   comparison

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!