Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Fun with Operators

Fun with Operators

Scheduled Pinned Locked Moved Clever Code
10 Posts 7 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jake86954
    wrote on last edited by
    #1

    Fun with Operators : int I = 6; I += I++; System.out.println("Result : " + I); // I is now 12 int I = 6; I += ++I; System.out.println("Result : " + I); // I is now 13

    J M 2 Replies Last reply
    0
    • J Jake86954

      Fun with Operators : int I = 6; I += I++; System.out.println("Result : " + I); // I is now 12 int I = 6; I += ++I; System.out.println("Result : " + I); // I is now 13

      J Offline
      J Offline
      Jorgen Sigvardsson
      wrote on last edited by
      #2

      You've just described the semantics of the postfix and prefix version of '++'. Do you claim it is a subtle bug in the Java language? :~

      -- Mit viel Oktan und frei von Blei, eine Kraftstoff wie Benziiiiiiin!

      J S 2 Replies Last reply
      0
      • J Jake86954

        Fun with Operators : int I = 6; I += I++; System.out.println("Result : " + I); // I is now 12 int I = 6; I += ++I; System.out.println("Result : " + I); // I is now 13

        M Offline
        M Offline
        Mindflow
        wrote on last edited by
        #3

        Wrong! They're both 13 I had a look and knew something wasn't right, so I ran the conspicuous one in PHP. This is right... int I = 6; I += I++; System.out.println("Result : " + I); // I is now 13 int I = 6; I += ++I; System.out.println("Result : " + I); // I is now 13 However... // PHP $i = 6; echo ($i = $i + $i++); echo "Result : " . $i; Displays 12 twice :omg: Not good. I think the first line should display 12 and second 13. I wonder how it would be displayed when compiled in other languages. // PHP $i = 6; echo ($i = $i + ++$i); echo "Result : " . $i; Displays 13 twice, this one makes sense; :sigh: any one with something to say about PHP must come through me :suss: heheh ;P

        E 1 Reply Last reply
        0
        • J Jorgen Sigvardsson

          You've just described the semantics of the postfix and prefix version of '++'. Do you claim it is a subtle bug in the Java language? :~

          -- Mit viel Oktan und frei von Blei, eine Kraftstoff wie Benziiiiiiin!

          J Offline
          J Offline
          Jake86954
          wrote on last edited by
          #4

          This is indeed perfectly correct Java-behaviour. I just added this example because less experienced users (who may not even know the difference between i++ and ++i) can be easily tricked by this.

          1 Reply Last reply
          0
          • M Mindflow

            Wrong! They're both 13 I had a look and knew something wasn't right, so I ran the conspicuous one in PHP. This is right... int I = 6; I += I++; System.out.println("Result : " + I); // I is now 13 int I = 6; I += ++I; System.out.println("Result : " + I); // I is now 13 However... // PHP $i = 6; echo ($i = $i + $i++); echo "Result : " . $i; Displays 12 twice :omg: Not good. I think the first line should display 12 and second 13. I wonder how it would be displayed when compiled in other languages. // PHP $i = 6; echo ($i = $i + ++$i); echo "Result : " . $i; Displays 13 twice, this one makes sense; :sigh: any one with something to say about PHP must come through me :suss: heheh ;P

            E Offline
            E Offline
            Egon_Freeman
            wrote on last edited by
            #5

            "// PHP $i = 6; echo ($i = $i + $i++); echo "Result : " . $i; Displays 12 twice Not good. I think the first line should display 12 and second 13. I wonder how it would be displayed when compiled in other languages." I have Your bug by the balls, so to say :-) Here's how it works: a) "$i + $i" evaluates to 12 b) $i++ evaluates to 7 c) $i evaluates to 12 in other words, the post-incremenation comes BEFORE the assignment, so $i becomes 12 and not 13 (it's 6->7 then 12, not 12 then 12->13). The second example works, because ++$i evaluates to 7 first. The difference between pre- and post-incrementation is that the former increments before reading the variable, and the latter increments AFTER the value is taken into consideration in whatever computation. It's the same with pre- and post-decrementation. Actually, this is a textbook example of "not getting" these operators. :)

            R M 2 Replies Last reply
            0
            • E Egon_Freeman

              "// PHP $i = 6; echo ($i = $i + $i++); echo "Result : " . $i; Displays 12 twice Not good. I think the first line should display 12 and second 13. I wonder how it would be displayed when compiled in other languages." I have Your bug by the balls, so to say :-) Here's how it works: a) "$i + $i" evaluates to 12 b) $i++ evaluates to 7 c) $i evaluates to 12 in other words, the post-incremenation comes BEFORE the assignment, so $i becomes 12 and not 13 (it's 6->7 then 12, not 12 then 12->13). The second example works, because ++$i evaluates to 7 first. The difference between pre- and post-incrementation is that the former increments before reading the variable, and the latter increments AFTER the value is taken into consideration in whatever computation. It's the same with pre- and post-decrementation. Actually, this is a textbook example of "not getting" these operators. :)

              R Offline
              R Offline
              Ray Kelm
              wrote on last edited by
              #6

              The C specification (and C++, I believe) state that this the result is undefined. So the compiler can produce any result and still be "correct". I've never seen the spec for PHP, assuming there is one.

              E 1 Reply Last reply
              0
              • R Ray Kelm

                The C specification (and C++, I believe) state that this the result is undefined. So the compiler can produce any result and still be "correct". I've never seen the spec for PHP, assuming there is one.

                E Offline
                E Offline
                Egon_Freeman
                wrote on last edited by
                #7

                I'm not sure either if there is a spec for PHP - I just read the manual and learn as I go. ;) As for the inc/dec operators, I've heard a discussion about which one was more efficient when used as a standalone operation, ie. ... i++; doSomething(); ... or ... ++i; doSomething(); ... and from what I remember, someone said that pre-inc/dec was more efficient, because it doesn't copy the value somewhere... I'm not really that good with C/C++, but with PHP no matter how many times I run what I've posted before it ALWAYS evaluates the way I expect it to. I guess that's what the whole hype's about with deterministic computing, etc. ... ;) But You are right in that we should be more explicit about what we want to do, and what we expect to get in return. I mean, ie. the strict-comparison (===) operator exists for a reason... :) Besides, that whole line ( $i = $i + $i++; ) is just plain wrong, as in 'close to obfuscated'... not that I've never done a similar thing, one way or the other... :-O -- modified at 23:06 Tuesday 3rd October, 2006

                S 1 Reply Last reply
                0
                • E Egon_Freeman

                  "// PHP $i = 6; echo ($i = $i + $i++); echo "Result : " . $i; Displays 12 twice Not good. I think the first line should display 12 and second 13. I wonder how it would be displayed when compiled in other languages." I have Your bug by the balls, so to say :-) Here's how it works: a) "$i + $i" evaluates to 12 b) $i++ evaluates to 7 c) $i evaluates to 12 in other words, the post-incremenation comes BEFORE the assignment, so $i becomes 12 and not 13 (it's 6->7 then 12, not 12 then 12->13). The second example works, because ++$i evaluates to 7 first. The difference between pre- and post-incrementation is that the former increments before reading the variable, and the latter increments AFTER the value is taken into consideration in whatever computation. It's the same with pre- and post-decrementation. Actually, this is a textbook example of "not getting" these operators. :)

                  M Offline
                  M Offline
                  Mindflow
                  wrote on last edited by
                  #8

                  Egon_Freeman wrote:

                  the post-incremenation comes BEFORE the assignment

                  So strange anyway, what is the compiler outputing to machine code? We have 2 operations in one line, A and B. B can be ($i++) Low Level Code: 1. Evaluate i+i to a new memory address A2 2. Evaluate i++ to a new memory address B2 being 7 3. Assign i the value of B2 4. Assign i the value of A2 :confused: isn't this backwards Ahh I get it, ;P 1. Evaluate i+i to a new memory address A2 (i+i) 2. Do i++ (but NOT to a new memory address) actually increment the contents of that memory location. (This is probably possible with one line of assembly) 3. Assign i the value of A2 (i =) :-> Still dumb if you ask me heh. Definitely a subtle bug I think. Best to always keep things on sepatate lines, that's what I do.:cool:

                  1 Reply Last reply
                  0
                  • J Jorgen Sigvardsson

                    You've just described the semantics of the postfix and prefix version of '++'. Do you claim it is a subtle bug in the Java language? :~

                    -- Mit viel Oktan und frei von Blei, eine Kraftstoff wie Benziiiiiiin!

                    S Offline
                    S Offline
                    S Senthil Kumar
                    wrote on last edited by
                    #9

                    Well, atleast in C++, the semantics are undefined if the prefixed or postfixed variable is used elsewhere in the same expression.

                    Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                    1 Reply Last reply
                    0
                    • E Egon_Freeman

                      I'm not sure either if there is a spec for PHP - I just read the manual and learn as I go. ;) As for the inc/dec operators, I've heard a discussion about which one was more efficient when used as a standalone operation, ie. ... i++; doSomething(); ... or ... ++i; doSomething(); ... and from what I remember, someone said that pre-inc/dec was more efficient, because it doesn't copy the value somewhere... I'm not really that good with C/C++, but with PHP no matter how many times I run what I've posted before it ALWAYS evaluates the way I expect it to. I guess that's what the whole hype's about with deterministic computing, etc. ... ;) But You are right in that we should be more explicit about what we want to do, and what we expect to get in return. I mean, ie. the strict-comparison (===) operator exists for a reason... :) Besides, that whole line ( $i = $i + $i++; ) is just plain wrong, as in 'close to obfuscated'... not that I've never done a similar thing, one way or the other... :-O -- modified at 23:06 Tuesday 3rd October, 2006

                      S Offline
                      S Offline
                      Suzetta
                      wrote on last edited by
                      #10

                      I believe at one point in the murky past of C++ one of the intel compilers actually had a problem where i++ was less efficient than i=i+1 due to some pipelining issue, but rumour at the time had it that this was only a problem on intel chips!

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups