compound-assignment

Addition assignment += behavior in expression

折月煮酒 提交于 2019-12-02 16:05:13
Recently I came across this question: Assignment operator chain understanding . While answering this question I started doubting my own understanding of the behavior of the addition assignment operator += or any other operator= ( &= , *= , /= , etc.). My question is, when is the variable a in the expressions below updated in place, so that its changed value is reflected in other places in the expression during evaluation, and what is the logic behind it? Please take a look at following two expressions: Expression 1 a = 1 b = (a += (a += a)) //b = 3 is the result, but if a were updated in place

Shortcut “or-assignment” (|=) operator in Java

▼魔方 西西 提交于 2019-12-02 10:46:53
I have a long set of comparisons to do in Java, and I'd like to know if one or more of them come out as true. The string of comparisons was long and difficult to read, so I broke it up for readability, and automatically went to use a shortcut operator |= rather than negativeValue = negativeValue || boolean . boolean negativeValue = false; negativeValue |= (defaultStock < 0); negativeValue |= (defaultWholesale < 0); negativeValue |= (defaultRetail < 0); negativeValue |= (defaultDelivery < 0); I expect negativeValue to be true if any of the default<something> values are negative. Is this valid?

Sequence point within assignment operators

青春壹個敷衍的年華 提交于 2019-12-01 10:34:12
Let's just take for example the specific compound assignment operator ^= . This stackoverflow page says modification of the left operand may have not been done after the evaluation of ^= , and thus making the code a ^= b ^= a ^= b undefined behaivor. But this does not seem to be the case. The standard says in 5.17 [expr.ass] that In all cases, the assignment is sequenced after the value computation of the right and left operands, and before the value computation of the assignment expression. There are two keypoints in this statement. 1) What does the subject assignment refers to? In my opinion

What does compound let/const assignment mean?

☆樱花仙子☆ 提交于 2019-11-30 07:57:48
问题 There is an article Optimization killers in wiki of Bluebird library. In this article there is a phrase: Currently not optimizable: ... Functions that contain a compound let assignment Functions that contain a compound const assignment What does compound let assignment and compound const assignment mean? In ECMAScript 5.1 there was notion of compound assignment but in ECMAScript 2015, it seems there is no notion of any compound assignment there is only regular assignments. I suspect that

What does compound let/const assignment mean?

别来无恙 提交于 2019-11-29 05:28:35
There is an article Optimization killers in wiki of Bluebird library. In this article there is a phrase: Currently not optimizable: ... Functions that contain a compound let assignment Functions that contain a compound const assignment What does compound let assignment and compound const assignment mean? In ECMAScript 5.1 there was notion of compound assignment but in ECMAScript 2015, it seems there is no notion of any compound assignment there is only regular assignments. I suspect that compound let and const assignment, it is just compound assignment after declaration. For example: let n = 1

Lua operators, why isn't +=, -= and so on defined?

纵然是瞬间 提交于 2019-11-28 22:25:37
This is a question I've been mildly irritated about for some time and just never got around to search the answer to. However I thought I might at least ask the question and perhaps someone can explain. Basically many languages I've worked in utilize syntactic sugar to write (using syntax from C++): int main() { int a = 2; a += 3; // a=a+3 } while in lua the += is not defined, so I would have to write a=a+3 , which again is all about syntactical sugar. when using a more "meaningful" variable name such as: bleed_damage_over_time or something it starts getting tedious to write: bleed_damage_over

Are there sequence points in the expression a^=b^=a^=b, or is it undefined?

谁说我不能喝 提交于 2019-11-28 13:20:16
The allegedly "clever" (but actually inefficient) way of swapping two integer variables, instead of using temporary storage, often involves this line: int a = 10; int b = 42; a ^= b ^= a ^= b; /*Here*/ printf("a=%d, b=%d\n", a, b); But I'm wondering, compound assignment operators like ^= are not sequence points, are they? Does this mean it's actually undefined behavior? a ^= b ^= a ^= b; /*Here*/ It is undefined behavior. You are modifying an object ( a ) more than once between two sequence points. (C99, 6.5p2) "Between the previous and next sequence point an object shall have its stored value

Why is “a^=b^=a^=b;” different from “a^=b; b^=a; a^=b;”?

喜夏-厌秋 提交于 2019-11-28 07:31:01
I tried some code to swap two integers in Java without using a 3rd variable, using XOR. Here are the two swap functions I tried: package lang.numeric; public class SwapVarsDemo { public static void main(String[] args) { int a = 2984; int b = 87593; swapDemo1(a,b); swapDemo2(a,b); } private static void swapDemo1(int a, int b) { a^=b^=a^=b; System.out.println("After swap: "+a+","+b); } private static void swapDemo2(int a, int b) { a^=b; b^=a; a^=b; System.out.println("After swap: "+a+","+b); } } The output produced by this code was this: After swap: 0,2984 After swap: 87593,2984 I am curious to

Why are there no ||= or &&= operators in C#?

懵懂的女人 提交于 2019-11-27 23:24:39
We have equivalent assignment operators for all Logical operators, Shift operators, Additive operators and all Multiplicative operators. Why did the logical operators get left out? Is there a good technical reason why it is hard? Why did the logical operators get left out? Is there a good technical reason why it is hard? They didn't . You can do &= or |= or ^= if you want. bool b1 = false; bool b2 = true; b1 |= b2; // means b1 = b1 | b2 The || and && operators do not have a compound form because frankly, they're a bit silly. Under what circumstances would you want to say b1 ||= b2; b1 &&= b2;

Why doesn't Java have compound assignment versions of the conditional-and and conditional-or operators? (&&=, ||=)

孤街浪徒 提交于 2019-11-27 08:08:46
问题 So for binary operators on booleans, Java has & , | , ^ , && and || . Let's summarize what they do briefly here: JLS 15.22.2 Boolean Logical Operators &, ^, and | JLS 15.23 Conditional-And Operator && JLS 15.24 Conditional-Or Operator || For & , the result value is true if both operand values are true ; otherwise, the result is false . For | , the result value is false if both operand values are false ; otherwise, the result is true . For ^ , the result value is true if the operand values are