a = a += a++;

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

a = a += a++;

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :P
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

3 in both cases
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: a = a += a++;

Post 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 ... :(
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Smalltalk can't replicate this.. there's no precedence at all.. left to right and that's that :P (except for parenthesis, and there is also no equivalent of += or ++.)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I didn't realise gcc could do that. That's awesome :)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Anyone know of something similar for PHP? That'd be handy.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

onion2k wrote:Anyone know of something similar for PHP? That'd be handy.
Parsekit
Post Reply