置换两个数

交换两个数

int temp;
int n=3,m=8;
temp = n;
n = m;
m = temp;
n = 3, m = 8;

n = n + m;//3+8=11
m = n - m;//11-8=3
n = n - m;//11-3=8

n = n ^ m;
m = n ^ m; //(n ^ m) ^ m;
n = n ^ m; //(n ^ m) ^ n;

你可能感兴趣的:(置换两个数)