Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (7)


What??? Swapping two variables without pointers or a third one?Format this article printer-friendly!Bookmark function is only available for registered users!
Swapping two values without using pointers or a third variable.
Product:
Delphi all versions
Category:
Algorithm
Skill Level:
Scoring:
Last Update:
07/30/2001
Search Keys:
delphi delphi3000 article borland vcl code-snippet swap temporary integer curiosity
Times Scored:
10
Visits:
5180
Uploader: Rafael Cotta
Company:
Reference: N/A
 
Question/Problem/Abstract:
How to swap two integers without a temporary variable or pointers?
Answer:



Yes, itīs possible to swap the values of two variables without using a third one or swapping pointers.

How??? The answer is: using xor!!!

Place a label and a button on a form, and put this on Buttonīs click event.

//------------------------------

procedure TForm1.Button1Click(Sender: TObject);
var
  var1 : integer;
  var2 : integer;
begin

  var1 := 19;
  var2 := 564;

  var1 := var1 xor var2;
  var2 := var1 xor var2;
  var1 := var1 xor var2;

  // Theyīre swapped!!!

  Label1.Caption := 'Var1 = ' + IntToStr(var1) + '; Var2 = ' + IntToStr(var2);
end;

//------------------------------

Try this!



  





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Hmmm.
    John Mollll (Mar 25 2002 4:20PM)

Oh wow.  I've never seen this before!

(Do a search on Delphi3000 itself to find this exact same code.)
Respond

not (always) worth it....
    anonymus (Jul 30 2001 9:03PM)

I've used the xor trick in the good ol' days when you always ran out of CPU registers and memory. How ever....

Today it's better to leave this to the compiler and write readable code instead...Why? Check this..
a := a xor b
b = a xor b
a = a xor b

is translated into something like this by the compiler:

mov eax,[b]
xor  [a],eax
mov eax,[a]
xor [b],eax
mov eax,[b]
xor [a],eax

and
  tmp = a
  a = b
  b = tmp

is translated into
  mov eax,[a]
  mov [c],eax
  mov eax,[b]
  mov [a],eax
  mov eax,[tmp]
  mov [b],eax

So...Absolutely nothing saved.....
The assembler codes is taken directly from Delphi 5 using global vars - Using local var for the tmp var, Delphi should optimize this...

So, even though it is cute, you don't really save anything in all cases. The asm version showed in another comment is great for swapping large buffers etc, but for standard types... leave it to delphi. Even though I haven't checked it out, you might run into trouble swapping to objects this way - not sure, but there might be cases where it could spoil Delphi's reference counting....

And don't forget... it's always nice to understand your own code in 3 months.... :-)
Respond

RE: not (always) worth it....
Rafael Cotta (Jul 31 2001 3:34AM)

Iīll never use this on my programs. Itīs just an algorithmic curiosity.

Rafael
Respond

RE: a shorter assembler form
Ernesto De Spirito (Aug 20 2001 1:12AM)

  asm
    mov eax, var1;
    xchg var2, eax;
    mov var1, eax;
  end;

Respond

Assembler Version
    Mike Heydon (Jul 30 2001 9:29AM)

// ========================================
// This is a FAST swap routine that swaps  
// the contents of any 2 variables.        
// The variables may be of any type but    
// the sizeof the VARS must be passed in Len
//
// eg.  X1,X2 : integer;
//
//        SwapMem(X1,X2,SizeOf(Integer));
//      
// ======================================== }

procedure SwapMem(var Source,Dest; Len : integer);
   begin
     asm
         push edi
         push esi
         mov esi,Source
         mov edi,Dest
         mov ecx,Len
         cld
     @1:
         mov al,[edi]
         xchg [esi],al
         inc si
         stosb
         loop @1
         pop esi
         pop edi
      end;
   end;



Respond

another one
    Jiang Hong (Jul 30 2001 8:44AM)

var1:=var1+var2;
var2:=var1-var2;
var1:=var1-var2;
Respond

RE: another one
Mat Hobbs (Jul 31 2001 11:07PM)

...but watch out for out of range...
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
R. Lefter
 
   














 







     
  Copyright Đ 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)