Reputation: 1291
What is the difference in += and normal add
a = a + b;
a += b;
what is the different in above two lines?
Is there any increase in CPU cycles for "+=" operator?
Which would be the preferable method of coding.?
Upvotes: 4
Views: 424
Reputation: 1
So, all the above answers given are accurate and cool, but what I want to add is for large or looping operations like for instance(refer the snippet) will give you tle(occurs very rarely but good to know). Especially if the array or the element we are adding to are of long/double/BigInteger format. Then it is better to use normal addition.
for(int i = 0; i < n; i++) {
a += arr[0] * arr[i];
}
// for such cases just use
for(int i = 0; i < n; i++ ) {
a = a + arr[0] * arr[i];
}
Upvotes: 0
Reputation: 37914
Beside single evaluation of first operand, there is second difference, that occurs when b
is an expression, involving operators with lower precedence. For instance:
int a = 1;
a += 0 || 1;
yields 2
, while:
int a = 1;
a = a + 0 || 1;
stores 1
into a
. The equivalent of the former statement would be:
a = a + (0 || 1);
Upvotes: 3
Reputation: 5241
When I compile the following two programs,
int main(int argc, char** argv)
{
int a = 5, b = 10;
a = a + b;
return 0;
}
and
int main(int argc, char** argv)
{
int a = 5, b = 10;
a += b;
return 0;
}
with gcc file.c -S -O0
, I get the following assembler output (this is just the relevant section)
movl $5, -4(%rbp) # Assign 5 to a
movl $10, -8(%rbp) # Assign 10 to b
movl -8(%rbp), %eax # Move b to eax
addl %eax, -4(%rbp) # Add eax to a, store in a
This same output is produced with both implementations.
However, although everything works out nicely with integers and a single addition, there are cases where you may get different results, consider for double a,b
the operation a *= b*b
and a = b*a*b
. If the result of the particular multiplication can't be exactly represented then the two operations will produce different results.
Upvotes: 2
Reputation: 106012
There is a difference in between them and it is explained in the C standard:
Acompound assignment of the form
E1 op= E2
is equivalent to the simple assignment expressionE1 = E1 op (E2)
, except that the lvalueE1
is evaluated only once, and with respect to an indeterminately-sequenced function call, the operation of a compound assignment is a single evaluation.
Upvotes: 3
Reputation: 11032
Here is a good explanation..Quoting from the source
x += y means
But x = x + y means:
Evaluate x+y
Find the place identified by x
Upvotes: 0
Reputation: 108978
Difference is only in source. The output binary (executable) is exactly the same (with normal current compilers).
You might like one or the other better in different conditions.
I like the short form better, especially when a
is complicated
vector[savedpos] = vector[savedpos] + 1000;
vector[savedpos] += 1000;
Upvotes: 1
Reputation: 4041
Both the expressions are same. Second one is short hand assignment operator. Which will help us to reduce the reptitive coding.
a = a + b; // Here a will come twice.
a += b;
Here the repitition of code will be avoided.
Upvotes: 0