8 Replies - 8761 Views - Last Post: 11 October 2009 - 04:35 PM Rate Topic: -----

#1 rahul_ait  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-October 09

how to swap 2 numbers without using temporary variable

Post icon  Posted 10 October 2009 - 07:30 AM

how to swap 2 numbers without using temporary variable
Is This A Good Question/Topic? 0
  • +

Replies To: how to swap 2 numbers without using temporary variable

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,033
  • Joined: 15-July 08

Re: how to swap 2 numbers without using temporary variable

Posted 10 October 2009 - 07:47 AM

View Postrahul_ait, on 10 Oct, 2009 - 06:30 AM, said:

how to swap 2 numbers without using temporary variable


Can this even be done? I mean, even pre-prepared Java swappers use a temporary variables... Why is this so important?
Was This Post Helpful? 0
  • +
  • -

#3 LaFayette  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 326
  • Joined: 24-November 08

Re: how to swap 2 numbers without using temporary variable

Posted 10 October 2009 - 07:48 AM

Can be done with simple arithmetics!
a = a + b;
b = a - b;
a = a - b;


Kinda stupid/unecessary to not use a temp variable though...

edited for stupidity!

This post has been edited by LaFayette: 10 October 2009 - 07:56 AM

Was This Post Helpful? 1
  • +
  • -

#4 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: how to swap 2 numbers without using temporary variable

Posted 10 October 2009 - 08:19 AM

Please don't tell me that your lecturer asked you to write a method that swaps two values without using a temporary variable...@.@
I don't think it's possible.....right?
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,259
  • Joined: 27-December 08

Re: how to swap 2 numbers without using temporary variable

Posted 10 October 2009 - 08:38 AM

This sounds like a homework assignment?

@OP: What do you think? Give us some of your ideas. We can help you more once you show us some effort. Thanks!
Was This Post Helpful? 0
  • +
  • -

#6 Guest_Neumann*


Reputation:

Re: how to swap 2 numbers without using temporary variable

Posted 10 October 2009 - 09:32 AM

Nothing stupid about this assignment. Good job LaFayette, that's a neat solution.
Was This Post Helpful? 0

#7 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: how to swap 2 numbers without using temporary variable

Posted 10 October 2009 - 09:36 AM

This isn't the only way. There is also the xor swap

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

Edit: Looking again. It looks a lot like LaFayette's answer

This post has been edited by skaoth: 10 October 2009 - 09:37 AM

Was This Post Helpful? 0
  • +
  • -

#8 LaFayette  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 326
  • Joined: 24-November 08

Re: how to swap 2 numbers without using temporary variable

Posted 10 October 2009 - 10:45 AM

Quote

Nothing stupid about this assignment. Good job LaFayette, that's a neat solution.


Thanks!

Quote

There is also the xor swap

I had almost forgotten about that one. It's actual pretty likely (I think...) that the compiler (javac in this case) can recognise simple swapping and translates it into somethings similar..

This post has been edited by LaFayette: 10 October 2009 - 10:45 AM

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: how to swap 2 numbers without using temporary variable

Posted 11 October 2009 - 04:35 PM

Will also work with an XOR

A XOR B
B XOR A
A XOR B

fastest way to swap two register in assembler
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1