tobold.org

correct • elegant • free

△ Comp.lang.c △

◅ loop terminaters and efficiency/style

File truncation ▻

Where would I use ^ ?

In article <3psv7u$ijt@paperboy.wellfleet.com>,
Todd Short  <tshort@baynetworks.com> wrote:
>void swap(int *a, int *b)
>{
>   a ^= b;
>   b ^= a;
>   a ^= b;
>}
>
>Also note that in PC assembly, a register can be quickly and efficiently
>cleared by XOR'ing itself (it's like 1 byte, w/o any extra memory accesses):

Which is why your swap function is dangerous.  What does this code do,
if i == j?

    swap(&a[i], &a[j]);

See the FAQ, question 6.1.

Except in very unusual circumstances, you should write code in the most
straightforward style possible, rather than resorting to tricks like the
"xor swap" in the hope of saving a word of memory.

Tim.
--
Tim Goodwin        | "X.400(84)... conformance can reduce
Public IP Exchange | real interoperability." -- Steve Kille

Original headers:

From: tim@pipex.net (Tim Goodwin)
Newsgroups: comp.lang.c
Subject: Re: Where would I use ^ ?
Date: 23 May 1995 16:48:37 +0100
Organization: PIPEX, 216 Science Park, Cambridge, England
Message-ID: <3pt04l$seb@pipe.pipex.net>
References: <3psu5g$dbo@inet-nntp-gw-1.us.oracle.com>
  <3psv7u$ijt@paperboy.wellfleet.com>

△ Comp.lang.c △

◅ loop terminaters and efficiency/style

File truncation ▻