Computing.Net > Forums > Programming > SWAP without temp variable

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

SWAP without temp variable

Reply to Message Icon

Name: Nirav
Date: November 5, 2002 at 09:55:37 Pacific
OS: win98
CPU/Ram: p3
Comment:

Hi:
How can i swap values in 2 variables without using temp varibale.

Thanx
Nirav



Sponsored Link
Ads by Google

Response Number 1
Name: Jeff J
Date: November 5, 2002 at 12:58:33 Pacific
Reply:

The same way one swaps the liquid in two cups without any intermediate storage space.

Quite the brain teaser, and an age-old question. If I remember my physics correctly, one crux is that "two objects cannot occupy the same space at the same time".

The only ways I know of, rely on a twist on the words, like storing in registers rather than allocated variables (if it fits), or swapping pointers to values (if applicable). Yet a temp container still needs to be used for one value of some sort. I'd love to hear more creative solutions...

Cheers


0

Response Number 2
Name: Guy
Date: November 5, 2002 at 13:33:26 Pacific
Reply:

Hi - In programming, this is typically done using XOR functions/transformations.

It can be done with data in registers or variables if the language you use supports this.

It usually cannot be done with user defined objects in an OO language (tho' there are some exceptions here).

For example (in Java):

int i = 2;
int j = 3;
System.out.println("i= "+i+" j= "+j);
i = i ^ j;
j = j ^ i;
i = i ^ j;
System.out.println("i= "+i+" j= "+j);

HTH, Guy



0

Response Number 3
Name: dtech10
Date: November 5, 2002 at 15:53:39 Pacific
Reply:

There are two ways you can swap two variables

Method #1 this works integers and floats
a=12 :b=4
a=a+b
b=a-b
a=a-b

method #2 Integers only
a=12 : b=4
a=a xor b
b=a xor b
a=a xor b

if your computer doe's not have an XOR function use (a or b) and not (a and b)
as below.
a=(a or b)and not (a and b)
b=(a or b) and not (a and b)
a=(a or b)and not (a and b)


0

Response Number 4
Name: Jim
Date: November 5, 2002 at 17:00:55 Pacific
Reply:

If you're programming in assembly, there is actually an xchg instruction. At least one of your operands has to be in a register, but it can be done in one instruction.


0

Response Number 5
Name: Guy
Date: November 5, 2002 at 18:07:41 Pacific
Reply:

Ah yes - I had forgotten that languages like Java (and others) forbid XOR on primitives like floats and doubles (although this is not usually a consideration in assembly language).

If XOR is forbidden, then the method#1 above works fine.

The 'xchg' instruction is of course OK on Intel architecture machines. Machines from other vendors may or may not have an equivalent instruction. Most will allow the XOR technique, for example to swap the values in two registers on a IBM mainframe:

XR R1,R2
XR R2,R1
XR R1,R2

Much more difficult on for example a PDP-8, so named because there are only 8 instructions in the whole set, and if I recall correctly a single 8-bit data register.

Regards, Guy


0

Related Posts

See More



Response Number 6
Name: nisam
Date: November 6, 2002 at 05:50:53 Pacific
Reply:

what is macro and how it works please reaplay thanks


0

Response Number 7
Name: Jeff J
Date: November 6, 2002 at 21:27:20 Pacific
Reply:

Please ignore my initial theoretical blather, Nirav. I should have realised you simply meant how to avoid allocating your own integers, rather than hypothesise whether temp storage could be avoided at all (e.g., registers, whether instruction-loaded or not). Everyone else addressed your question quite nicely.

Cheers


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: SWAP without temp variable

Add two strings to form variable www.computing.net/answers/programming/add-two-strings-to-form-variable/6014.html

ASP without session variables www.computing.net/answers/programming/asp-without-session-variables/7870.html

basic calculater www.computing.net/answers/programming/basic-calculater-/7321.html