交换价值与XOR [英] Swapping Values with XOR
问题描述
什么是这两个宏之间的区别?
的#define交换(A,B)(((一)^(B))及及((A)^ =(B)^ =(A)^ = (二)))
或者
的#define交换(A,B)(((一)^(B))及及((B)^ =(A)^ =(B),(一个)^ =(b)))
我看到了第二个宏 rel=\"nofollow\">但不明白为什么它未写入像第一个?是否有特殊原因,我错过了?
首先会调用的未定义行为在这两个C99和C11。
在C99,它可以被理解为;他们将未定义行为的由于缺乏序列点的。
C-FAQ :
在previous和下一个序列点之间的对象应具有其存储的值由前pression评价修改最多一次。此外,前一个值是唯一的访问以确定将被存储的值。
块引用>说明:结果
第一个是修改A
双序列点,因此行为是不明确的每条语句之间两次的的previous和下一个顺序点之间对象所其存储的值由前pression评价修改最多一次。的就是这样(没必要去想b
)。C11文件说:
6.5防爆pressions(P2):
如果一个标物体上的副作用是相对于unsequenced 强>任一的使用相同的标量的值相同的标量对象或一个值计算在不同的副作用对象的这种行为是未定义。如果有一个前pression的SUBEX pressions多个允许排序,该行为是未定义如果在任何排序中发生这样的unsequenced副作用。 84)
块引用>在
(一)^ =(B)^ =(A)^ =(B)
,对副作用A
是unsequenced,因此未定义行为。应当注意的是,C 11 6.5 P1说:
...]操作者的操作数的值计算是操作者的结果的值计算之前测序。
块引用>这可以保证在
(一)^ =(B)= ^(一)^ =(B)
| | | |
1 2 3 4所有SUBEX pressions 1,2,3和4保证最左侧的
^ =
运算符的计算结果之前计算。但是,这并不能保证前pression 3最左边^ =
运算的结果的值计算之前保证的那一侧的效果。对><分> 1。重点是我的。
What is the difference between these two macros?
#define swap(a, b) (((a) ^ (b)) && ((a) ^= (b) ^= (a) ^= (b)))
Or
#define swap(a, b) (((a) ^ (b)) && ((b) ^= (a) ^= (b), (a) ^= (b)))
I saw the second macro here but couldn't understand why it wasn't written like the first one? Is there a special reason that I missed?
解决方案First will invoke undefined behavior in both C99 and C11.
In C99, it can be understood as; they will invoke undefined behavior because of the lack of the sequence points.
Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored.
Explanation:
The first one is modifyinga
twice between two sequence points and hence behavior is undefined as per statement: Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. That's it (no need to think aboutb
).C11 documentation says:
6.5 Expressions (p2):
If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings.84)
In
(a) ^= (b) ^= (a) ^= (b)
, side effect ona
is unsequenced and hence invoke undefined behavior. It should be noted that C11 6.5 p1 says that:[...] The value computations of the operands of an operator are sequenced before the value computation of the result of the operator.
This guarantees that in
(a) ^= (b) ^= (a) ^= (b) | | | | 1 2 3 4
all subexpressions 1, 2, 3 and 4 are guaranteed to be computed before the result computation of left most
^=
operator. But, this doesn't guarantee that side effect of expression 3 is guaranteed before the value computation of the result of the left most^=
operator.
1. Emphasis is mine.
这篇关于交换价值与XOR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!