📄 swap.homework.html
字号:
<html><!-- Mirrored from c-faq.com/misc/swap.homework.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:02:53 GMT --><head><title>swapping with or without temporaries</title></head><body><p>[This is part of my reply to someone who askedhow to swap two values without using a third object.]<p>From: scs@eskimo.com (Steve Summit)<br>Subject: Re: Swapping two values<br>Date: Fri, 13 Jun 1997 07:27:59 -0700 (PDT)<br>Message-Id: <199706131427.HAA06631@mail.eskimo.com><p>You wrote:<br>> I have a question for you :<br>> I need to swap the value of two scalar's without using a third object.<p>There are two possible reasons you might be asking me this question:<br>1. This requirement is part of some project concerning your employment.<br>2. This is a homework or examination question.<p>If the former, please go back and convince the architect ormanager who has imposed this requirement to reconsider. Thetrick for swapping values without using temporaries is usefulonly in assembly language, when for some reason registers are inshort supply. The trick has no place in a higher-level languagesuch as C, because temporary variables are essentially free.Ifyou're using C, the right way to swap twovalues <TT>a</TT> and <TT>b</TT> is to use a temporary <TT>t</TT>:<p><pre> t = a; a = b; b = t;</pre><p>Using the trick would only make your program slightly larger,slightly slower, and slightly harder to understand.<p>If the latter, I normally do not give people the answers tohomework problems, but since this question isill-advised, I'm willing to make an exception.<p>If <TT>a</TT> and <TT>b</TT> are integers, you can swap them by using this curiousset of exclusive-or operations:<p><pre> a ^= b; b ^= a; a ^= b;</pre><p>But this works only if <TT>a</TT> and <TT>b</TT> are integers, and it has anadditional disadvantage, which is that if for some reason youhave pointers to the integers you want to swap, such that you'retempted to write<p><pre> *p1 ^= *p2; *p2 ^= *p1; *p1 ^= *p2;</pre><p>then in the case where <TT>p1 == p2</TT>, you'll zero out both <TT>*p1</TT>and <TT>*p2</TT>. (The case could arise if you wrote something like<pre> swap(&a[i], &b[j]);</pre>in the context of a sorting routine, and <TT>i==j</TT>.)<p>If <TT>a</TT> and <TT>b</TT> are floating-point variables, I think there aretriple sequences of additions and subtractions which willaccomplish a swap, but they're all subject to overflow.<p>Again, these ``tricks,'' even if they work, are likely to besignificantly slower and bulkier than the obvious three-assignmentswap using a temporary. These tricks are intellectualcuriosities only; they offer no advantages over the more obviousswap; they have virtually no place in modern programming.<p>If, in fact, this was a homework question,please request your instructor not to assign it in the future.It is essentially meaningless and misleading; it serves mostly toinduce students to use these tricks in real programs, where theyhave no place.Also, if your instructor offers as asolution to this question the version of the exclusive-or trickexpressed as<p><pre> a ^= b ^= a ^= b;</pre>please inform your instructor that this expression is undefinedin C.<p><address><a href="http://www.eskimo.com/~scs/">Steve Summit</a><br><a href="mailto:scs@eskimo.com">scs@eskimo.com</a></address></body><!-- Mirrored from c-faq.com/misc/swap.homework.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:02:53 GMT --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -