Velocity Reviews > Java > variable exchange

# variable exchange

man4*.*
Guest
Posts: n/a

 10-03-2006
hello...
as a beginer in Java I've got a few workbooks for practice, so
here's one example.
-how to exchange content of 2 int variables x and y using only those 2
variables without using
any functions and any other variable?
temp=x;
x=y;
y=temp;
this would be easier...but they have to complicate....

Tim Ward
Guest
Posts: n/a

 10-03-2006
"man4*.*" <(E-Mail Removed)> wrote in message news:eftscs\$jm9\$(E-Mail Removed)...
> hello...
> as a beginer in Java I've got a few workbooks for practice, so
> here's one example.
> -how to exchange content of 2 int variables x and y using only those 2
> variables without using
> any functions and any other variable?
> temp=x;
> x=y;
> y=temp;
> this would be easier...but they have to complicate....

Ah, now that one's fair enough - this is something you have to be told, I
don't think anyone works it out for themselves!

XOR

--
Tim Ward
Brett Ward Limited - www.brettward.co.uk

Matt Humphrey
Guest
Posts: n/a

 10-03-2006

"man4*.*" <(E-Mail Removed)> wrote in message news:eftscs\$jm9\$(E-Mail Removed)...
> hello...
> as a beginer in Java I've got a few workbooks for practice, so
> here's one example.
> -how to exchange content of 2 int variables x and y using only those 2
> variables without using
> any functions and any other variable?
> temp=x;
> x=y;
> y=temp;
> this would be easier...but they have to complicate....

This is an old trick that makes you reason about the value of a variable
over time. You have to combine the numbers and then uncombine them.
Pretend you're the computer and write out X= something and Y=something and
then step through combinations of what you can do with ints and cross out
the old values and write in the new ones. Don't overlook the bitwise
operators.

Matt Humphrey http://www.velocityreviews.com/forums/(E-Mail Removed) http://www.iviz.com/

Patricia Shanahan
Guest
Posts: n/a

 10-03-2006
man4*.* wrote:
> hello...
> as a beginer in Java I've got a few workbooks for practice, so
> here's one example.
> -how to exchange content of 2 int variables x and y using only those 2
> variables without using
> any functions and any other variable?
> temp=x;
> x=y;
> y=temp;
> this would be easier...but they have to complicate....

Get a different book. You already know the simple, clear way of doing
the job. Why use a book that wants to waste your time on a very
specialized trick?

See http://www.caliberdt.com/tips/Apr2003.htm for a description of how
to swap without the temporary variable.

In addition to the limitations they describe, note that it temporarily
assigns one of the variables a value that is neither its old value nor
its new value. That may have implications in some multi-threaded code.

I've known the technique for about 20 years. I have yet to encounter a
situation in which I've needed to use it.

Patricia

Tim Ward
Guest
Posts: n/a

 10-03-2006
"Patricia Shanahan" <(E-Mail Removed)> wrote in message
news:j8vUg.2591\$(E-Mail Removed) ink.net...
>
> I've known the technique for about 20 years. I have yet to encounter a
> situation in which I've needed to use it.

Not for swapping two variables, no. But I have used it in other
circumstances. Can't recall what, offhand, because memory has been cheap for
a long time now ... something like using only half the space for the
pointers in a double-linked list perhaps, back in the days when 4k was a lot
of RAM?

--
Tim Ward
Brett Ward Limited - www.brettward.co.uk

Chris Uppal
Guest
Posts: n/a

 10-03-2006
Tim Ward wrote:

> > I've known the technique for about 20 years. I have yet to encounter a
> > situation in which I've needed to use it.

>
> Not for swapping two variables, no. But I have used it in other
> circumstances. Can't recall what, offhand, because memory has been cheap
> for a long time now ... something like using only half the space for the
> pointers in a double-linked list perhaps, back in the days when 4k was a
> lot of RAM?

That's a known use, yes. I've seen it given a valid use for fast exchange of
graphical areas -- if the underlying hardware can do a fast bulk XOR, then
three such bulk XORs can be a lot faster than iterating over each pixel using
"normal" code and doesn't consume extra (then scarce) video memory.

But I find it hard to imagine seeing the technique ever given a valid use
again, so I agree with Patricia's advice -- get a different book...

-- chris

man4*.*
Guest
Posts: n/a

 10-03-2006
> See http://www.caliberdt.com/tips/Apr2003.htm for a description of how
> to swap without the temporary variable.

Thank you a LOT!!
that helped me...

Thomas Weidenfeller
Guest
Posts: n/a

 10-04-2006
Patricia Shanahan wrote:
> I've known the technique for about 20 years. I have yet to encounter a
> situation in which I've needed to use it.

I think I used it once. In Z80 assembler. There, and maybe in the IOCCC
it might have its place.

Oh, and a variant is useful in interactive computer graphics. To draw a
cursor on top of some graphics and to erase the cursor later, without
having to remember the graphics contents under the cursor.

/Thomas
--
The comp.lang.java.gui FAQ:
http://gd.tuwien.ac.at/faqs/faqs-hie...lang.java.gui/

himaloy
Guest
Posts: n/a

 10-04-2006
my professor taught us a way of swaping variables without using any
temp variable

let suppose x = 3 & y = 2

then x = x + y = 3+2 = 5

y = x - y = 5 - 2 = 3
x = x - y = 5 - 3 = 2

here u go...both values of x & y has been exchanged

man4*.* wrote:

> hello...
> as a beginer in Java I've got a few workbooks for practice, so
> here's one example.
> -how to exchange content of 2 int variables x and y using only those 2
> variables without using
> any functions and any other variable?
> temp=x;
> x=y;
> y=temp;
> this would be easier...but they have to complicate....

Patricia Shanahan
Guest
Posts: n/a

 10-04-2006
himaloy wrote:
> my professor taught us a way of swaping variables without using any
> temp variable
>
> let suppose x = 3 & y = 2
>
> then x = x + y = 3+2 = 5
>
> y = x - y = 5 - 2 = 3
> x = x - y = 5 - 3 = 2
>
> here u go...both values of x & y has been exchanged

Be careful with this version. Although addition and subtraction are
available for float and double, they don't have the properties that make
it work for integers. If you really can't get hold of a temporary,
unlikely in Java, it is better to use the xor method which makes it
obvious it is only applicable to integer types.

This program:

public static void main(String[] args) {
double x = 1e30;
double y = 1.0;
System.out.printf("Before: x=%g, y=%g%n",x,y);
x = x+y;
y = x-y;
x = x-y;
System.out.printf("After: x=%g, y=%g%n",x,y);
}
}

outputs:

Before: x=1.00000e+30, y=1.00000
After: x=0.00000, y=1.00000e+30

Patricia