• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

swap without using temp variable

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to swap the values without using temp variable

please can you tell me ?

Thanks & Regards,
seetharaman.v
[ May 28, 2008: Message edited by: David O'Meara ]
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Google says
http://prabhagovind.wordpress.com/2007/02/09/3-ways-to-swap-variables-without-temp-variable/

Personally I'd just use a temp variable...
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks David ...i got the tricky
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can do it in a single step:
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can swap as shown below without using temp

let a and b are two variables to swap for example let a=5 b= 3

step 1)a=a+b ------------ > a=5+3=8

step 2)b=a-b ------------ > b=8-3=5

step 3)a=a-b ------------ > a=8-5=3

finally a=3 ,b=5 hence swaped
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

you can swap like that also,

let a=5 b= 3

a=a*b ------------ > a=5*3=15

b=a/b ------------ > b=15/3=5

a=a/b ------------ > a=15/5=3

result a=3,b=5

-----------------------
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Sandy

I like Karthikeyan Ramaswamy's method! There is supposed to be a way to do it with the bitwise XOR operator ^ but I can't remember how to do it.
 
Karthikeyan Ramaswamy
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, Campbell

Using bitwise XOR operator

int a=10,b=20;

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

result a=20,b=10

-----------------
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're writing a serious piece of software, please do not use tricks like the above to swap the values of variables. They are clever tricks, but nothing more than that. You gain nothing by using these tricks in any piece of software - it's not faster and doesn't use more memory than just using a temp variable.

Note that the above methods by adding / subtracting or multiplying / dividing don't work for all values. Integers can overflow, and dividing by zero leads to errors as well.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, this stuff is all sorta kinda fun, but pointless.

Perhaps it was an interview question? If so, it doesn't say much for the interviewer. There are a small set of tricks like this, which people can easily learn for interviews, without actually knowing much about programming at all. Whereas many good honest candidates wouldn't know the answer, because they've been too busy working on real projects!

Interviewers should ask searching questions with longer answers, where it's much harder to fake a good answer. Of course, if the interviewer's own technical knowledge is lacking, and they're only looking for the "right" answer they've got on their crib-sheet, there's trouble there, too.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"sandy th",

Will you please check your private messages regarding an important message?

Thank you,

Rob
 
Die Fledermaus does not fear such a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic