Swaps the contents of two values given the value of two shaping variables

Source: Internet
Author: User

for a given two variables, the value of the two value of the exchange of the contents of the problem, from two aspects of the design of the solution, the following is the specific algorithm analysis and procedures.

Algorithm one:

First, create two shaping variables and initialize them. At the same time, create a temporary variable, through this custom variable to exchange the address of two shaping variables, and with the aid of pointers, so as to exchange two shaped variable content purposes.

Here are the specific procedures:

#include <stdio. h >

int Main ()

{

int a = Ten;

int b = ;

int *p2=&A;

int *p1=&b;

int temp;

Temp =* P1;

                     * P1 =* P2;

                     * P2 = temp;

a =* P2;

b =* P1;

printf ("%d%d", A, b);

return 0;

}

where temp is a custom variable, the a=20,b=10 can be obtained by exchanging the addresses of two shaping variables with pointers. The program can also customize a swap function swap, which is called in Main, resulting in the same result as above.

The method above is to create a temporary variable, with pointers, to exchange the contents of the two shaping variables. So, what if you can't create a temporary variable?


Algorithm two:

when a temporary variable cannot be created, we can study the binary numbers corresponding to these two shaping variables to discover the three relationships between the corresponding binary numbers:

The first type:

the binary numbers corresponding to the two shaping variables are bitwise-XOR and can be found to have a relationship between them:

a = a^ b;

b = a^ b;

a = a^ b;

Using this relationship, you can write the following program:

#include <stdio. h >

int Main ()

{

int a = Ten;

int b = ;

a = a^ b;

b = a^ b;

a = a^ b;

printf ("%d%d", A, b);

return 0;

}

The second type:

there is an addition and subtraction between the contents of the two shaping variables, namely:

a = a + b;

b = a - b;

a = a - b;

The same result can be obtained by replacing the part of a, B, or relationship of the above program with the plus-minus relationship.

The third type:

The contents of the two shaping variables also exist in the multiplication and addition relationships, i.e.:

a = A * b;

b = A/ b;

a = A/ b;

The result can be obtained by using the multiplication and addition relations.

Analysis:

These three methods, the most accurate is the first kind. When the data of the shaping variable is very large, the addition, subtraction, and multiplication and addition relationships are likely to overflow.

This article from the "C language" blog, declined reprint!

Swaps the contents of two values given the value of two shaping variables

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.