|
-
June 13th, 2004, 09:55 AM
#1
interchange of variables
Hi all, this is a pretty simple question but has been haunting me for quite sometime now so here goes....How is it possible to interchange the values of two variables without having to store one of their value temporarily? Need an answer quick!
Thanx in advance
Awais
Life is what happens to you while you're busy making other plans!
-
June 13th, 2004, 10:05 AM
#2
Re: interchange of variables
Use pointers and swap them instead of the variable values
-
June 13th, 2004, 10:36 AM
#3
consider:
A = 2;
B = 8;
A=A+B; ==> now A=10, B=8
B=A-B; ==> now A=10, B=2
A=A-B; ==> now A=8, B=2
Regards,
Guy
**** **** **** **** **/**
-
June 13th, 2004, 11:13 AM
#4
Re: Re: interchange of variables
Originally posted by Oliver Twesten
Use pointers and swap them instead of the variable values
... and furthermore, use only those pointers, because each variable remains in its initially place.
-
June 13th, 2004, 12:27 PM
#5
What kind of variables do you want to swap?
If these are numeric variables, you can use the xor-swap method:
Code:
// swap x and y
x ^= y;
y ^= x;
x ^= y;
or directly use the XCHG i386 opcode
-
June 13th, 2004, 02:43 PM
#6
-
June 13th, 2004, 05:22 PM
#7
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|