compound-assignment

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

泄露秘密 提交于 2019-11-27 07:45:11
问题 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? 回答1: a ^= b ^= a ^= b; /*Here*/ It is undefined behavior. You are modifying an object ( a ) more than once between two

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

試著忘記壹切 提交于 2019-11-27 01:49:49
问题 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); }

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

守給你的承諾、 提交于 2019-11-26 14:23:16
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?

Operator precedence with Javascript Ternary operator

感情迁移 提交于 2019-11-26 11:42:42
I cant seem to wrap my head around the first part of this code ( += ) in combination with the ternary operator. h.className += h.className ? ' error' : 'error' The way i think this code works is as following: h.className = h.className + h.className ? ' error' : 'error' But that isn't correct because that gives a error in my console. So my question is how should i interpet this code correctly? Kobi h.className = h.className + (h.className ? ' error' : 'error') You want the operator to work for h.className , better be specific about it. Of course, no harm should come from h.className += ' error'

Varying behavior for possible loss of precision

末鹿安然 提交于 2019-11-26 11:23:09
In Java, when you do int b = 0; b = b + 1.0; You get a possible loss of precision error. But why is it that if you do int b = 0; b += 1.0; There isn't any error? polygenelubricants That's because b += 1.0; is equivalent to b = (int) ((b) + (1.0)); . The narrowing primitive conversion (JLS 5.1.3) is hidden in the compound assignment operation. JLS 15.26.2 Compound Assignment Operators (JLS Third Edition): A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)) , where T is the type of E1 , except that E1 is evaluated only once. For example, the following

What exactly does += do in python?

我们两清 提交于 2019-11-26 10:14:54
I need to know what += does in python. It's that simple. I also would appreciate links to definitions of other short hand tools in python. Bryan In Python, += is sugar coating for the __iadd__ special method, or __add__ or __radd__ if __iadd__ isn't present. The __iadd__ method of a class can do anything it wants. The list object implements it and uses it to iterate over an iterable object appending each element to itself in the same way that the list's extend method does. Here's a simple custom class that implements the __iadd__ special method. You initialize the object with an int, then can

Operator precedence with Javascript Ternary operator

六眼飞鱼酱① 提交于 2019-11-26 02:32:14
问题 I cant seem to wrap my head around the first part of this code ( += ) in combination with the ternary operator. h.className += h.className ? \' error\' : \'error\' The way i think this code works is as following: h.className = h.className + h.className ? \' error\' : \'error\' But that isn\'t correct because that gives a error in my console. So my question is how should i interpet this code correctly? 回答1: h.className = h.className + (h.className ? ' error' : 'error') You want the operator to

Varying behavior for possible loss of precision

巧了我就是萌 提交于 2019-11-26 02:23:33
问题 In Java, when you do int b = 0; b = b + 1.0; You get a possible loss of precision error. But why is it that if you do int b = 0; b += 1.0; There isn\'t any error? 回答1: That's because b += 1.0; is equivalent to b = (int) ((b) + (1.0)); . The narrowing primitive conversion (JLS 5.1.3) is hidden in the compound assignment operation. JLS 15.26.2 Compound Assignment Operators (JLS Third Edition): A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)) , where