int a, b;
a ^= b;
b ^= a;
a ^= b;
note: if a and b are pointers. this behavior probably will not work well. assume that a and b are pointing to the same varible simultaneously, if we do as the above, the result will be 0. In a conclusion, what if we meet the following cases the kinda method will not work well.
first, the object a and object b have shared part of memory.
second, either a or b is stored in the register or is in bitfall.
swap two values without temporary variable
最新推荐文章于 2022-10-24 08:14:08 发布
博客介绍了通过异或运算交换两个整数变量值的代码,同时指出若a和b为指针,该方法可能失效。还总结了此方法不适用的情况,包括对象a和b有共享内存部分,以及a或b存储在寄存器或处于位域中。
1398

被折叠的 条评论
为什么被折叠?



