All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars    
perplexus dot info

Home > Algorithms
Swap (Posted on 2004-01-02) Difficulty: 2 of 5
In programming, a common task is to swap the values of two variables. Lines 40-60 of the program below perform the swap using a temporary variable (T).

10 A=123
20 B=456
30 PRINT A,B
40 T=A
50 A=B
60 B=T
70 PRINT A,B

Rewrite the program to swap the values in A and B without using a temporary variable.

Note: The new program will not require sophisticated programming or complex math.

See The Solution Submitted by Brian Smith    
Rating: 3.2857 (7 votes)

Comments: ( Back to comment list | You must be logged in to post comments.)
re(2): other solutions | Comment 6 of 11 |
(In reply to re: other solutions by Tristan)

My understanding of the XOR function is that it takes the contents of two computer words and does their bit-by-bit "exclusive or" (the input bits have to be different to yield a 1 as the output bit). Assuming 4-bit computer words, for example, XOR(1111,1010)=0101 (i.e. bit by bit complement). An example oriented toward the problem: XOR(1001,0010)=1011, and XOR(1011,1001)=0010 while XOR(1011,0010)=1001.
Edited on January 2, 2004, 11:45 pm
  Posted by Richard on 2004-01-02 21:47:58

Please log in:
Login:
Password:
Remember me:
Sign up! | Forgot password


Search:
Search body:
Forums (0)
Newest Problems
Random Problem
FAQ | About This Site
Site Statistics
New Comments (3)
Unsolved Problems
Top Rated Problems
This month's top
Most Commented On

Chatterbox:
Copyright © 2002 - 2024 by Animus Pactum Consulting. All rights reserved. Privacy Information