Two variable exchange and principle analysis without the help of the third variable

Source: Internet
Author: User


The usual procedure for exchanging two variables, A/b, is
var temp;
Temp=a;
A=b;
B=temp;
The 3rd temporary variable temp above is required.

There is no need to exchange two variables, A/d, with the 3rd temporary variable:

Logical operation (XOR)

The following XOR operation enables A/b interchange without the need for a 3rd temporary variable:
A = a ^ b;
b = a ^ b;
A = a ^ b;
This exchange of two variables without the help of the 3rd temporary variable process, the implementation is mainly based on the following properties of the XOR operation:
1. Any one of the variable X and its own XOR operation, the result is 0, that is, x^x=0
2. Any one of the variables X and 0 is an XOR operation, the result is unchanged, that is, x^0=x
3. The XOR operation has the binding, namely a^b^c= (a^b) ^c=a^ (b^c)
4. The XOR operation is exchangeable, i.e. A^b=b^a

Analysis:
First step: a = a ^ b; The result of a variable after completion is a ^ b
Step Two: b = a ^ b; At this time the assignment number to the right of a to save the value of a ^ B, then the assignment number to the right of a with a ^ B to replace, get (a ^ b) ^ B=a ^ (b ^ b) =a ^0=a, that is, after the second operation, the value of B is a, that is B=a, a to B
Step three: a = a ^ b; At this time the assignment number to the right of a is still the value of a ^ B, unchanged, and the assignment number to the right of the B is a, the assignment number to the right of a, a ^ b= (a ^ b) ^ A=a ^ b^ a=a ^ a^ b=0^ b=b, the value assigned to a, that is a=b That is, after the third operation, the value in A is B, which is a=b, and B is changed to a.
This completes the exchange of two variables, a, B, as in the three steps above, without the need for a 3rd temporary variable procedure.
function swap5 (x, y) {
if (x==y) {
Return
}else{
X^=y;
Y^=x;
X^=y;
}
}
Arithmetic operations (plus and minus)

Easy overflow with x and y in the same number of cases
function Swap1 (x, y) {
X=x+y;
Y=x-y;
X=x-y;
}
Easy overflow in cases where x and Y are different
function Swap2 (x, y) {
X=x-y;
Y=x+y;
X=y-x;
}
A rigorous approach
function Swap4 (x, y) {
if (x==y) {
return;
}else if ((x>0&&y>0) | | (x<0&&y<0)) {
X=x-y;
Y=x+y;
X=y-x;
}else{
X=x+y;
Y=x-y;
X=x-y;
}
}

If you think carefully, you can see that there are actually many ways. Why do I need a third variable? Two variables, there must be two information (in the unit), if the direct Exchange, A=b, then a original information is lost, so introduce a temporary variable to save the information of a to ensure the integrity of information. That is, the role of temp is to preserve the amount of information that can be lost in the exchange process, so as long as this amount of information is not lost, then temp is not required. Then, it is perfectly possible to pass additional information during the interchange, that is, the coupling information between A and B, without the loss of information and the need for a third variable. This coupling information can be a-B, a^b, or a*b. such as:
A=a*b;
a=a/b;
b=a/b;
It is also possible to complete the exchange (providing ideas only, without considering issues such as 0 overflow). Extrapolate, there can be many ways, but the principle is the same.

Two variable exchange and principle analysis without the help of the third variable

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.