Swap Two Values in one Command

  • Thread starter Thread starter sort
  • Start date Start date
If Value1 and Value2 are numeric values, then you can swap them without the
need for an intermediate variable...

Value1 = Value1 + Value2
Value2 = Value1 - Value2
Value1 = Value1 - Value2
 
Interesting way of doing the swap and had a look at it, but using an
intermediate variable turns out
to be twice as fast.

RBS


Rick Rothstein said:
If Value1 and Value2 are numeric values, then you can swap them without
the need for an intermediate variable...

Value1 = Value1 + Value2
Value2 = Value1 - Value2
Value1 = Value1 - Value2
 
Hello Mr. Rothstein,
Very nice solution. I usually enjoy your replies and wonder which forum you
are going to "migrate" to?
I am a bit disappointed in MS's phony instructions.

Best Regards,

Gabor Sebo


----- Original Message -----
From: "Rick Rothstein" <[email protected]>
Newsgroups: microsoft.public.excel.programming
Sent: Sunday, May 30, 2010 12:21 AM
Subject: Re: Swap Two Values in one Command

If Value1 and Value2 are numeric values, then you can swap them without
the need for an intermediate variable...

Value1 = Value1 + Value2
Value2 = Value1 - Value2
Value1 = Value1 - Value2

--
Rick (MVP - Excel)


Dave Peterson said:
I think you'll need that intermediate variable.

Rick Rothstein said:
If Value1 and Value2 are numeric values, then you can swap them without
the need for an intermediate variable...

Value1 = Value1 + Value2
Value2 = Value1 - Value2
Value1 = Value1 - Value2
 
Back
Top