d*******n 发帖数: 369 | 1 swap a and b using XOR (no the third variable). The following function is
correct? Explain
swap(int *a, int *b)
{
*a ^= *b ^= *a ^= *b;
}
I found that if I write it as follows, it's correct
*a ^= *b;
*b ^= *a;
*a ^= *b;
but *a ^= *b ^= *a ^= *b is wrong but why? explain in detail | t****t 发帖数: 6806 | 2 peng u to death
http://c-faq.com/expr/xorswapexpr.html
【在 d*******n 的大作中提到】 : swap a and b using XOR (no the third variable). The following function is : correct? Explain : swap(int *a, int *b) : { : *a ^= *b ^= *a ^= *b; : } : I found that if I write it as follows, it's correct : *a ^= *b; : *b ^= *a; : *a ^= *b;
| a****l 发帖数: 8211 | 3 这种东西有什么用处?你调用都是有开销的,与其浪费在这些东西上还不如直接用一个中
间变量做个简单的替换.最简单的说,你现在搞个*a,*b这就是至少两个int,还没算别的
出入堆的东西.
【在 d*******n 的大作中提到】 : swap a and b using XOR (no the third variable). The following function is : correct? Explain : swap(int *a, int *b) : { : *a ^= *b ^= *a ^= *b; : } : I found that if I write it as follows, it's correct : *a ^= *b; : *b ^= *a; : *a ^= *b;
| N***m 发帖数: 4460 | 4 想起当年朗道嘲笑哥德巴赫猜想:难道素数不是用来相乘的吗?
【在 a****l 的大作中提到】 : 这种东西有什么用处?你调用都是有开销的,与其浪费在这些东西上还不如直接用一个中 : 间变量做个简单的替换.最简单的说,你现在搞个*a,*b这就是至少两个int,还没算别的 : 出入堆的东西.
| z****e 发帖数: 2024 | |
|