使用位移运算符交换数字

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
void swap(int *x,int *y){
         *x^=*y;
         *y^=*x;
         *x^=*y;
}
void main(){
         int a=1,b=3;
         swap(&a,&b);
         cout<<a<<setw(4)<<b<<endl;
}

        另外附上两种不使用第三个变量,交换两个数字的值的方法:ios

        方法一:spa

a += b; 
b = a - b; 
a -= b;

        方法二:code

a -= b;
b += a;
a = b - a;
相关文章
相关标签/搜索