exchange 2 varibles withoutb using a third variable

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2008
Posts: 5
Reputation: abcd_nima is an unknown quantity at this point 
Solved Threads: 0
abcd_nima's Avatar
abcd_nima abcd_nima is offline Offline
Newbie Poster

exchange 2 varibles withoutb using a third variable

 
0
  #1
Apr 18th, 2008
iam a 1st yr b.e stucent.i was attending my c.p lab viva.i was asked to write a program that would exchange 2 variables without using a third variable.i could'nt do it.i know how to exchange the values by using a third variable.someone please answer this question.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,968
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: exchange 2 varibles withoutb using a third variable

 
1
  #2
Apr 18th, 2008
It's fairly simple to write:

  1. int a=5;
  2. int b=3;
  3.  
  4. a = a ^ b; // a will contain a xor b = 6
  5. b = a ^ b; // b will receive 6 xor 3 = 5
  6. a = a ^ b; // a will become 6 xor 5 = 3
  7.  
  8. printf("a=%d b=%d", a,b);

But personally I don't like it. I don't like the assignment and I don't like teachers who come up with this stuff.

The reason is that this can easily cause undefined behavior. Look at this line:
b = a ^ b; What value does the right b have? You would say it has it's old value, but perhaps 'a' was already assigned to 'b' before the xor operation was carried out. What would happen then??

You should tell your teacher that this is a worthless assignment and that this horror of a code will only teach bad coding habits and major problems if ever used in a real project.
Last edited by niek_e; Apr 18th, 2008 at 7:21 am.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 248
Reputation: hammerhead is an unknown quantity at this point 
Solved Threads: 24
hammerhead's Avatar
hammerhead hammerhead is offline Offline
Posting Whiz in Training

Re: exchange 2 varibles withoutb using a third variable

 
0
  #3
Apr 18th, 2008
Another alternative.

a=a-b
b=b+a
a=b-a
There are 10 types of people in the world, those who understand binary and those who don't.

All generalizations are wrong. Even this one.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC