Page 1 of 1
a = a += a++;
Posted: Thu Feb 22, 2007 5:23 pm
by Chris Corbyn
Code: Select all
int a = 1;
a = a += a++;
//and
a = a += ++a;
NOTE: This isn't just Java, it's something you should be able to apply to most languages.
Logically, what would you expect the final values of a to be after both of those two expressions?
Now go off and try it... in a handful of different languages.
Posted: Thu Feb 22, 2007 5:30 pm
by feyd
It circles around the precedence ordering of = vs +=. Typically += has a higher precedence. ++ almost always has a higher precedence than either of those so the first, being a post increment would be 2 * a + 1 (I believe) where as the second would also be the same, but happen slightly different.
At any rate, it's poor programming practice.
Posted: Thu Feb 22, 2007 5:34 pm
by Chris Corbyn
It's interesting to see how different languages actually parse it differently and therefore produce completely different results
I should note, I would never put something like this in my code

Posted: Thu Feb 22, 2007 5:52 pm
by Weirdan
3 in both cases
Re: a = a += a++;
Posted: Thu Feb 22, 2007 6:53 pm
by volka
d11wtq wrote:Logically, what would you expect the final values of a to be after both of those two expressions?
First I'd expect the leading a= to be irrelevant in both cases. And then I expect the pre/post-increment to happen immediatly before/after the involved operands are pushed onto the stack.
a += a++
load
a [a=1, stack=1]
load
a [a=1, stack=1,1] first load the operand
inc
a [a=2, stack=1,1] then increment -> post-increment
add [a=2, stack=2]
store a [
a=2]
a += ++a++
load
a [a=1, stack=1]
inc
a [a=2, stack=1] first increment
load
a [a=2, stack=1,2] then load operand -> pre-increment
add [a=2, stack=3]
store a [
a=3]
But php prints 3 for
$a = $a += $a++; and 4 for
$a = $a += ++$a;.
I don't have another compiler/language at hand right now but obviously I'm missing something ...

Posted: Thu Feb 22, 2007 7:04 pm
by Chris Corbyn
PHP 5.2:
Code: Select all
$a = 1;
$a = $a += $a++;
//3
$a = 1;
$a = $a += ++$a;
//4
Java 5:
Code: Select all
Integer a;
a = 1;
a = a += a++;
//2
a = 1;
a = a += ++a;
//3
JavaScript (in Safari):
Code: Select all
var a;
a = 1;
a = a += a++;
//2
a = 1;
a = a += ++a;
//3
Perl:
Code: Select all
$a = 1;
$a = $a += $a++;
//3
$a = 1;
$a = $a += ++$a;
//4
C++:
Code: Select all
int a = 1;
a = a += a++;
//3
int a = 1;
a = a += ++a;
//4
Posted: Thu Feb 22, 2007 7:38 pm
by Jenk
Smalltalk can't replicate this.. there's no precedence at all.. left to right and that's that

(except for parenthesis, and there is also no equivalent of += or ++.)
Posted: Fri Feb 23, 2007 4:17 am
by volka
One thing about gcc I really like is that you can always ask it "why are you doing this?"
Compile
Code: Select all
#include <stdio.h>
void main() {
int a = 1;
a = a += a++;
printf("%d\n", a);
}
with
Code: Select all
gcc -fdump-tree-gimple -c testIncrement.c
and you get a file called testIncrement.c.t03.gimple containing
Code: Select all
main ()
{
int a;
a = 1;
a = a + a;
a = a;
a = a + 1;
printf (&"%d\n"[0], a);
}
Seems like the post-inc is performed after everything else in the statement is done, no matter how many operations there are.
Code: Select all
#include <stdio.h>
void main() {
int a = 2;
int b = 3;
a = b = b++ * a;
printf("%d %d\n", a, b);
}
prints
6 7
and the gimple code is
Code: Select all
main ()
{
int a;
int b;
a = 2;
b = 3;
b = b * a;
a = b;
b = b + 1;
printf (&"%d %d\n"[0], a, b);
}
That's something I really didn't expect.
Posted: Fri Feb 23, 2007 4:32 am
by Chris Corbyn
I didn't realise gcc could do that. That's awesome

Posted: Fri Feb 23, 2007 8:33 am
by onion2k
Anyone know of something similar for PHP? That'd be handy.
Posted: Fri Feb 23, 2007 1:24 pm
by Chris Corbyn
onion2k wrote:Anyone know of something similar for PHP? That'd be handy.
Eclipse IDE/Aptana can give you a good insight into what's going on in your code, although not on quite the same scale.
Posted: Sat Feb 24, 2007 10:42 am
by Weirdan
onion2k wrote:Anyone know of something similar for PHP? That'd be handy.
Parsekit