Questions tagged [compound-assignment]

For questions about single operators that assign a value based on both a previous value and an operand (e.g., the += operator in C or Python). Also referred to as "augmented assignment." Use this tag if your problem specifically involves or relates to a compound-assignment operator.

Compound assignment operators, such as +=, combine aspects of assignment with aspects of regular binary operators. For example, i += 42 generally has the effect of i = i + 42, but is more concise.

However, that is not always the case. For example, in a Python 3 class, a custom + can be implemented using the __add__ method, and a custom += can be implemented using the __iadd__ method. Those two methods could behave very differently.

102 questions
13
votes
3 answers

Why doesn't compound assignment in Java catch overflow problems?

To my shock, it turns out that the following code will compile without even warnings: public void test() { int value = 2000000000; long increment = 1000000000; value += increment; } Whereas this gives a compile-time error, as you would…
Hakanai
  • 12,010
  • 10
  • 62
  • 132
13
votes
4 answers

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

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…
Medinoc
  • 6,577
  • 20
  • 42
12
votes
1 answer

Is i += ++i undefined behavior in C++0x?

I'm very convinced with the explanation I've found that said that i = ++i is not undefined as far as C++0x is concerned, but I'm unable to judge whether the behavior of i += ++i is well-defined or not. Any takers?
Saurabh Manchanda
  • 1,115
  • 1
  • 9
  • 21
12
votes
8 answers

What does "|=" operation mean in C++?

I have the following code and I can't understand what does it mean: var1 |= var2>0 ? 1 : 2; Anyone can help me please!
Reem
  • 1,439
  • 2
  • 18
  • 21
9
votes
1 answer

What is the difference between += and =+ C assignment operators

I was wondering if there was a difference between =+ and += (and other assignment operators too). I tried and both did the same thing. So is there a difference or is there a convention? Do both work because my compilers dont check for…
Michael
  • 577
  • 1
  • 11
  • 21
8
votes
1 answer

Auto-(un)boxing fail for compound assignment

Thanks to the implicit casting in compound assignments and increment/decrement operators, the following compiles: byte b = 0; ++b; b++; --b; b--; b += b -= b *= b /= b %= b; b <<= b >>= b >>>= b; b |= b &= b ^= b; And thanks to auto-boxing and…
7
votes
4 answers

Purpose of subsequent use of OR and AND compound assignment operators on registers

In C and C++ code, in particular that for embedded systems, I regularly stumble upon assignments that take the following shape: A |= B; A &= B; Not sure if relevant, but A and B are registers here. See an example…
JJM Driessen
  • 522
  • 3
  • 13
7
votes
8 answers

Assigning using += gives NaN in javascript

Assignment a number to an attribute using the += operator gives me NaN in JavaScript. This code works as expected: > var result = {}; undefined > result['value'] = 10; 10 > result['value'] += 10; 20 But here we get NaN: > var test = {}; undefined >…
jsbisht
  • 9,079
  • 7
  • 50
  • 55
7
votes
1 answer

ObjC properties and C operators

Given the following property declaration: @property NSInteger foo; How do the increment, decrement, and compound assignment operators actually work on self.foo? It was my understanding that self.foo was merely syntactic sugar for the actual…
nhgrif
  • 61,578
  • 25
  • 134
  • 173
5
votes
1 answer

Why does the compound assignment of a string and an int work, but the assignment of a string to a string + int doesn't in C++?

I'm learning C++ and I ran into an interesting predicament that I don't quite understand. My goal is to concatenate a string with the char representation of an int value, say 'a'. My problem is that: string a = "bo"; a = a + 97; Doesn't work,…
5
votes
2 answers

How is A *= B *= A *= B evaluated?

public static void main(String[] args) { int A=5; int B=2; A *= B*= A *= B ; System.out.println(A); System.out.println(B); } When I calculated this problem on paper I found A=200 B=20, but when I write it down to eclipse it…
5
votes
1 answer

Compound assignment operators, what happens if the value is modified (in the meanwhile)?

Consider the following pseudocode (language agnostic): int f(reference int y) { y++; return 2; } int v = 1; v += f(v); When the function f changes y (that is v) while evaluating v += f(v), is the original value of v "frozen" and changes to…
5
votes
3 answers

Is there a compound assignment operator for a = b a (where is not commutative)?

In a lot of languages a = a + b can be written as a += b In case of numerical operations, a + b is same as b + a, so the single compound operator suffices. Also, a = a - b can be written as a -=b . However, a-b is not equal to b-a. Hence, the…
asheeshr
  • 4,088
  • 6
  • 31
  • 50
4
votes
3 answers

Using multiple compound assignments in a single expression

I am preparing for a Java exam and I am trying to understand operator precedence and compound assignment operators in depth. I played around with a few expressions which use compound assignment during the evaluation of an expression. However, I do…
Rauni Lillemets
  • 2,299
  • 1
  • 26
  • 39
4
votes
1 answer

Difference between tensor addition assignment and assignment in pytorch

I found that pytorch treats tensor assignment and addition assignment differently. Examples are shown below x = torch.tensor(3.0) print(id(x)) x = x + 5 print(id(x)) The results are 1647247869184 1647248066816 If we run the following code x =…
Andy
  • 79
  • 6