1,105,534 Community Members

exchange 2 variables values without using 3rd variable.

Member Avatar
Somali Rathore
Newbie Poster
7 posts since Jan 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-2
 

How can I exchange 2 variables values without using any 3rd temporary variable.

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
0
 

>How can I exchange 2 variables values without using any 3rd temporary variable.
Most likely your teacher is expecting this:

a ^= b;
b ^= a;
a ^= b;

But I'd recommend you find a new teacher because your current one is likely too focused on archaic trivialities and bad practices to teach you anything worthwhile.

Member Avatar
iyercb
Newbie Poster
10 posts since Jan 2009
Reputation Points: 5 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
1
 

How can I exchange 2 variables values without using any 3rd temporary variable.

Let's say you have A=0x10100000 and B=0x00001010 then this is what you do to swap the two variables without using a third variable.

Step 1
_____

(A) Exclusive OR (B), when you do this here's what happens

(A) 10100000
(B) 00001010
------------------
(A) 10101010 XOR Result - contents of A
(B) 00001010 B is unchanged

Step 2
_____

Now (B) Exclusive OR (A), when you do this here's what happens

(B) 00001010
(A) 10101010
------------------
(B) 10100000 XOR Result - contents of B
(A) 10101010 A is unchanged - same as after Step 1

Step 3
_____

Now (A Exclusive OR (B), when you do this here's what happens

(A) 10101010
(B) 10100000
------------------
(A) 00001010 XOR Result - contents of A
(B) 10100000 B is unchanged - same as after Step 2

Finally A has 00001010 and B has 10100000 (what you wanted, you have swapped them) and you did not use an itermediate variable.

Question Answered as of 5 Years Ago by Narue and iyercb
Member Avatar
Somali Rathore
Newbie Poster
7 posts since Jan 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you very much

Member Avatar
Salem
Posting Sage
7,177 posts since Dec 2005
Reputation Points: 5,138 [?]
Q&As Helped to Solve: 970 [?]
Skill Endorsements: 41 [?]
Team Colleague
 
0
 

Very good, now try it with something else, like a float or a struct or a pointer.

It's a cheap trick, not programming knowledge.

Member Avatar
ajay.krish123
Junior Poster in Training
90 posts since Nov 2008
Reputation Points: -4 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 0 [?]
 
-1
 

good method for swapping the numbers.

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
1
 

>good method for swapping the numbers.
No, it's not. It's brittle, often slower than the conventional method, and due to compiler optimizations the chances that it saves memory over the equivalent swap with a temporary are actually quite slim. This trick may have been useful twenty or thirty years ago, but now it's just a stupid way for stupid people to show of their knowledge of stupid trivia.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article