交换价值与XOR [英] Swapping Values with XOR

查看:175
本文介绍了交换价值与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.

C-faq:

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 modifying a 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 about b).

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 on a 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆