An in-depth understanding of the auto-increment and auto-Subtraction Algorithm

Source: Internet
Author: User

Example 2:

Int I = 3;
Int J = 4;
Int A = I ++;
Int B = ++ J;
Printf ("% d, % d \ n", a, B );

The problem is that I ++ is to add a value first, add a value, then add a value, and then assign a value, or add a value first and then add a value twice. In addition, what will ++ J do?

Result: 6, 12

Now I understand that I ++ is supposed to perform other operations of the complete expression before auto-increment. Therefore, in the example, A = 3 + 3 = 6; then I auto-increment 2 times, I = 5; on the contrary, ++ J First Auto-increment and then participate in other operations, so B = 6 + 6 = 12.

So far, is it completely clear? Then return to the question in the introduction:

Example 3:

Int I = 3;
Int J = 4;
Int A = I ++;
Int B = ++ J;
Printf ("% d, % d \ n", a, B );

Someone may say that this is very simple. I understand it all: a = 3 + 3 + 3 = 9, I = 6, B = 5 + 5 + 5 = 15, j = 5. Is that true?

The result is: 9, 19

This is good and confused. We have no doubt about a = I ++; the plus side is to execute other operations of the complete expression before auto-increment, the above example has also been verified, but B = ++ J; how should we understand it?

In addition to the priority of the pre-algorithm, the principle expression also has a binding problem. In ++ J;, because there are two + operations at the same level, according to the left combination of the + operator, during compilation, in fact, the preceding section (++ J) is processed first, and then the result is added with ++ J. For detailed procedures, see the assembly code:

Int B = ++ J;
0040b7dd mov ECx, dword ptr [ebp-8]
0040b7e0 add ECx, 1
0040b7e3 mov dword ptr [ebp-8], ECx // first + + J
0040b7e6 mov edX, dword ptr [ebp-8]
0040b7e9 add edX, 1
0040b7ec mov dword ptr [ebp-8], EDX // second + + J
0040b7ef mov eax, dword ptr [ebp-8]
0040b7f2 add eax, dword ptr [ebp-8] // ++ J
0040b7f5 mov ECx, dword ptr [ebp-8]
0040b7f8 add ECx, 1
0040b7fb mov dword ptr [ebp-8], ECx // third + + J
0040b7fe add eax, dword ptr [ebp-8] // ++ J ++
0040b801 mov dword ptr [ebp-10h], eax // assign to B

In addition, let's look at the assembly code of a = I ++:

Int A = I ++;
0040b7b6 mov eax, dword ptr [ebp-4]
0040b7b9 add eax, dword ptr [ebp-4] // I + I
0040b7bc add eax, dword ptr [ebp-4] // I + I
0040b7bf mov dword ptr [ebp-0Ch], eax // assign a value to
0040b7c2 mov ECx, dword ptr [ebp-4]
0040b7c5 add ECx, 1
0040b7c8 mov dword ptr [ebp-4], ECx // first I ++
0040b7cb mov edX, dword ptr [ebp-4]
0040b7ce add edX, 1
0040b7d1 mov dword ptr [ebp-4], EDX // second I ++
0040b7d4 mov eax, dword ptr [ebp-4]
0040b7d7 add eax, 1
0040b7da mov dword ptr [ebp-4], eax // third I ++

As expected. At this point, the problem of the front and back of the ++ operator should be completely solved.

 

Example 4:

Int I = 1;
Int J = 1;
Int A = I ++; // seven
Int B = ++ J;
Printf ("% d, % d \ n", a, B );
Printf ("% d, % d \ n", I, j );

Rules are rules. Our computer is not the mother of the matrix of the hacker empire. We always need to follow the rules.

A = 1 + 1 + 1 + 1 + 1 + 1 + 1 = 7, I = 8
B = 3 + 3 + 4 + 5 + 6 + 7 + 8 = 36, j = 8

Transferred from:
Http://www.cnblogs.com/E-star/archive/2012/09/18/2691507.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.