我读过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.
更多推荐
发布评论