Multiple methods to implement the value of the swap 2 int variable

Source: Internet
Author: User

The most common method is to save the backup value with a temporary variable

void swap(intint &y){    int temp = x;    x = y;    y = temp;}

Do not use temporary variables, as follows: Bitwise XOR and arithmetic implementation

#include <iostream>#include <limits>using namespace STD;voidSwapint&x,int&y) {x ^= y;    y = x ^ y; x = x ^ y;}voidSWAP1 (int&x,int&y) {x = x + y;//Even if overflow, the result is still correcty = x-y; x = XY;}voidSWAP2 (int&x,int&y) {x = XY;//x-y data lossy = x + y; x = x + y;}voidSWAP3 (int&x,int&y) {x = x * y;//x*y may overflowy = x/y; × = x/y;}voidSWAP4 (int&x,int&y) {x = y/x; y = y/x;//x = 0 Errorx = x * y;}intMainvoid){intx = numeric_limits<int>::max ();inty = numeric_limits<int>::max ()-1;//x = 1, y = 2;    cout<<"Original value"<< x <<" "<< y << Endl;    Swap (x, y); Swap (x, y);cout<<"Swap xor:";cout<< x <<" "<< y << Endl;    Swap1 (x, y); Swap1 (x, y);cout<<"swap plus:";cout<< x <<" "<< y << Endl;    SWAP2 (x, y); SWAP2 (x, y);cout<<"swap minus:";cout<< x <<" "<< y << Endl;//SWAP3 (x, y);    //cout << "swap by:";    //cout << x << "" << y << Endl;    //SWAP4 (x, y);    //cout << "swap except:";    //cout << x << "" << y << Endl;    cout<<"==========="<< Endl; x = numeric_limits<int>::min (); y = numeric_limits<int>::min () +1;cout<<"Original value"<< x <<" "<< y << Endl;    Swap (x, y); Swap (x, y);cout<<"Swap xor:";cout<< x <<" "<< y << Endl;    Swap1 (x, y); Swap1 (x, y);cout<<"swap plus:";cout<< x <<" "<< y << Endl;    SWAP2 (x, y); SWAP2 (x, y);cout<<"swap minus:";cout<< x <<" "<< y << Endl;}

The result of the operation is:

原值2147483647 2147483646以下交换使用一种方法均进行交换2次,如仍输出原值,结果正确swap 异或:2147483647 2147483646swap 加:2147483647 2147483646swap 减:-2147483647 -2147483648===========原值-2147483648 -2147483647swap 异或:-2147483648 -2147483647swap 加:-2147483648 -2147483647swap 减:2147483646 2147483647[Finished in 0.4s]

From the above results analysis, the Exchange can be implemented correctly only by bitwise XOR and by means of a variable and (although overflow but the result is correct) .

// 异或void swap(intint &y){    x ^= y;    y = x ^ y;    x = x ^ y;}// 用 x 保存 x+y 的和void swap1(intint &y){    x = x + y;    y = x - y;    x = x - y;}

Other ways, such as by saving 2 variables of the difference/product/quotient , for some data may output the correct results, but for potentially overflow data, can not achieve the correct Exchange .

Conclusion

In addition to using temporary variables to implement the interchange method, you can also Exchange 2 integer variables in the form of bitwise XOR and with one of the variables saved and .

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Multiple methods to implement the value of the swap 2 int 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.