home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 3557 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: cs.ruu.nl!usenet
  2. From: wsldanke@cs.ruu.nl (Wessel Dankers)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: x ^= y ^= x ^= y;
  5. Date: 20 Feb 96 23:59:04 +0100
  6. Organization: Dept of Computer Science, Utrecht University, The Netherlands
  7. Message-ID: <1286.6624T1439T237@cs.ruu.nl>
  8. NNTP-Posting-Host: anx1p8.cc.ruu.nl
  9. X-Newsreader: THOR 2.22 (Amiga TCP/IP)
  10.  
  11. In one of my college-books I found this transition:
  12.  
  13. <x := x+y ; y := x-y ; x := x-y, s> -> s{s(y)/x}{s(x)/y}
  14.  
  15. proven correct.
  16.  
  17. In plain C:
  18.  
  19.  x = x + y;
  20.  y = x - y;
  21.  x = x - y;
  22.  
  23. will swap the integers x and y. Of course I immediately started `hacking'
  24. on it, resulting in:
  25.  
  26.  x = x ^ y;
  27.  y = x ^ y;
  28.  x = x ^ y;
  29.  
  30. with the C bitwise-or operator.
  31.  
  32. Which can of course be rewritten as:
  33.  
  34.  x ^= y;
  35.  y ^= x;
  36.  x ^= y;
  37.  
  38. or if you want it real fancy:
  39.  
  40.  x ^= y ^= x ^= y;
  41.  
  42. Note that these algorithms do not use temporary storage and can be
  43. generalized for large memory areas with a for-loop.
  44.  
  45. My questions are:
  46.  
  47.  a) are these routines efficient for integers? (probably not)
  48.  
  49.  b) is this x ^= y ^= x ^= y; method usable with the blitter?
  50.     It would be possible to swap two memory locations with no
  51.     extra memory required in three Blit calls. Is the blitter
  52.     equally fast with copying as with XOR'ing?
  53.  
  54. --
  55. Wessel Dankers                 _\\|//_            <wsldanke@cs.ruu.nl>
  56.                                ///|\\\
  57. ----------------------------oOO--(_)---OOo----------------------------
  58. `Never imagine yourself not to be otherwise than what it might appear
  59. to others that what you were or might have been was not otherwise than
  60. what you had been would have appeared to them to be otherwise.'
  61.  
  62.