没有tmp的swap()函数 [英] swap() function without tmp

查看:83
本文介绍了没有tmp的swap()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我发现swap()函数没有临时变量。


void swap(int * x,int * y){

* x ^ = * y;

* y ^ = * x;

* x ^ = * y;

}


所以,我编写了下一个交换双精度的代码。


void dswap(double * x,double * y) {

*(int *)x ^ = *(int *)y;

*((int *)x + 1)^ = *((int *) y + 1);

*(int *)y ^ = *(int *)x;

*((int *)y + 1)^ = *( (int *)x + 1);

*(int *)x ^ = *(int *)y;

*((int *)x + 1) ^ = *((int *)y + 1);

}


但它很脏。是否重写更简单?

---

问候,

OSHIMA

Hi,
I found the swap() function without a temporary variable.

void swap(int *x, int *y){
*x ^= *y;
*y ^= *x;
*x ^= *y;
}

So, I wrote the next code that exchange the double.

void dswap(double *x, double *y){
*(int *)x ^= *(int *)y;
*((int *)x + 1) ^= *((int *)y + 1);
*(int *)y ^= *(int *)x;
*((int *)y + 1) ^= *((int *)x + 1);
*(int *)x ^= *(int *)y;
*((int *)x + 1) ^= *((int *)y + 1);
}

But it is dirty. Does it rewrite more simply?
---
Regards,
OSHIMA

推荐答案

sh *********** **@mail.goo.ne.jp (OSHIMA)写道:
sh*************@mail.goo.ne.jp (OSHIMA) writes:

我发现swap()函数没有临时变量。

void swap(int * x,int * y){
* x ^ = * y;
* y ^ = * x;
* x ^ = * y;
}

所以,我编写了下一个交换双精度的代码。

void dswap(double * x,double * y){
*(int *)x ^ = *(int *)y;
*((int *)x + 1)^ = *((int *)y + 1);
*(int *)y ^ = *(int *)x;
*((int *)y + 1)^ = *((int *)x + 1);
*(int *)x ^ = *(int *)y;
*((int *)x + 1)^ = *((int *)y + 1);
}

但它是脏的。它是否更简单地重写?
Hi,
I found the swap() function without a temporary variable.

void swap(int *x, int *y){
*x ^= *y;
*y ^= *x;
*x ^= *y;
}

So, I wrote the next code that exchange the double.

void dswap(double *x, double *y){
*(int *)x ^= *(int *)y;
*((int *)x + 1) ^= *((int *)y + 1);
*(int *)y ^= *(int *)x;
*((int *)y + 1) ^= *((int *)x + 1);
*(int *)x ^= *(int *)y;
*((int *)x + 1) ^= *((int *)y + 1);
}

But it is dirty. Does it rewrite more simply?




这是不可移植的,缓慢的,适得其反的。暂时使用

,并阅读常见问题解答:


20.15c:如何在不使用临时值的情况下交换两个值?

答:标准古老的汇编语言程序员的诀窍是:


a ^ = b;

b ^ = a;

a ^ = b;


但这种代码在现代HLL
编程中几乎没有什么位置。临时变量基本上是免费的,

和使用三个作业的惯用代码,即


int t = a;

a = b ;

b = t;


不仅对人类读者更清晰,更可能是由b +识别的b $ b编译器并转换为最有效的

代码(例如,使用交换指令,如果可用)。后者

代码显然也适用于指针和

浮点值,与XOR技巧不同。另见问题

3.3b和10.3。


-

为了充分利用这本书,我强烈建议你阅读它。

- 理查德希思菲尔德



This is nonportable, slow, and counterproductive. Use a
temporary, and read the FAQ:

20.15c: How can I swap two values without using a temporary?

A: The standard hoary old assembly language programmer''s trick is:

a ^= b;
b ^= a;
a ^= b;

But this sort of code has little place in modern, HLL
programming. Temporary variables are essentially free,
and the idiomatic code using three assignments, namely

int t = a;
a = b;
b = t;

is not only clearer to the human reader, it is more likely to be
recognized by the compiler and turned into the most-efficient
code (e.g. using a swap instruction, if available). The latter
code is obviously also amenable to use with pointers and
floating-point values, unlike the XOR trick. See also questions
3.3b and 10.3.

--
"To get the best out of this book, I strongly recommend that you read it."
--Richard Heathfield


OSHIMA< sh ********** ***@mail.goo.ne.jp>写道:
OSHIMA <sh*************@mail.goo.ne.jp> wrote:

我发现swap()函数没有临时变量。
Hi,
I found the swap() function without a temporary variable.




我会仍然建议使用临时变量。

-

== Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===

因此,对智能的考虑总是包括

的利益和伤害。 - 孙子

==侮辱,就像暴力一样,是无能的最后避难所... ===



I would still recommend using a temporary variable.
--
== Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like violence, are the last refuge of the incompetent... ===


OSHIMA写道:
OSHIMA wrote:
void swap(int * x,int * y){
* x ^ = * y;
* y ^ = * x;
* x ^ = * y;
}
所以,我编写了下一个交换双精度的代码。

void dswap(double * x,double * y) {
*(int *)x ^ = *(int *)y;
*((int *)x + 1)^ = *((int *)y + 1);
*(int *)y ^ = *(int *)x;
*((int *)y + 1)^ = *((int *)x + 1);
*(int *)x ^ = *(int *)y;
*((int *)x + 1)^ = *((int *)y + 1);
}
void swap(int *x, int *y){
*x ^= *y;
*y ^= *x;
*x ^= *y;
}

So, I wrote the next code that exchange the double.

void dswap(double *x, double *y){
*(int *)x ^= *(int *)y;
*((int *)x + 1) ^= *((int *)y + 1);
*(int *)y ^= *(int *)x;
*((int *)y + 1) ^= *((int *)x + 1);
*(int *)x ^= *(int *)y;
*((int *)x + 1) ^= *((int *)y + 1);
}




嗯,如果有一个memxor()函数可以独占

或一个内存区域与另一个,那么这可能是

做得有点便宜。 memand()和memor()完成

套装。也许它应该被添加到C2009。


- glen



Hmm, if there was a memxor() function that would exclusive
or one region of memory with another, then this could be
done somewhat portably. memand() and memor() to complete
the set. Maybe it should be added to C2009.

-- glen


这篇关于没有tmp的swap()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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