Algorithm learning-do not request a third number to exchange values of two integers

Source: Internet
Author: User

Exchange two numbers

Normally we need to apply one more variable to exchange two number values (assuming the variable is a, b). Examples are as follows:

int temp = A;
A = b;
b = temp;

That would be all right. But this is ultimately a way to apply temporary variables, always think about the name of the temporary variable. Here are two main approaches.

Direct operation

This approach is simple and I like to use it, because even floating-point can be used in this way, continue to assume that two variables are a and b .

A = A+b;
b = a A;
A = a-B;

This is the exchange! is not very simple. If you don't understand, you can find an example to try.

Logical operations

This approach is not difficult to understand, that is, the need for a small explanation, the first to see the solution is to use the logical operator of the XOR operator, the symbol is ^ .

A = A^b;
b = a^b;
A = A^b;

Is this a little bit of an exchange? (PS: If you understand the next method directly). In fact, give an example to understand. Suppose a = 3; b= 2; , and then we'll see how to convert both variables into binary. An int is 4 bytes, 32bit, those 0 I will not write, I only write the first 4 bit good.
a = a^b;

 A=3 = 0 0 1 1 b=2 = 0 0 1 0----------------0 0 0 1  

Now the binary of a is 0001 , the decimal is 1 . Now execute
b = a^b;

 a=1= 0 0 0 1 b=2= 0 0 1 0---------------0 0 1 1 

Find it, now B's binary is 0011 , decimal is 3 , then execute, a = a^b, then a becomes 2.

Other methods

In fact, here is a very similar, that is, we are on the value of a B is different or, then the Exchange is the value, in fact, we can be the address of a B xor, so you can directly exchange a B's address, of course a B represents the value of the change.
The detailed method I did not write. Same as above, but instead of an address.

PS: If there is a better way, you can comment to me, we learn a piece.

Algorithm learning-do not request a third number to exchange values of two integers

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.