# variable exchange

Discussion in 'Java' started by man4*.*, Oct 3, 2006.

1. ### man4*.*Guest

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....

man4*.*, Oct 3, 2006

2. ### Tim WardGuest

"man4*.*" <> wrote in message news:eftscs\$jm9\$...
> 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

Tim Ward, Oct 3, 2006

3. ### Matt HumphreyGuest

"man4*.*" <> wrote in message news:eftscs\$jm9\$...
> 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.iviz.com/

Matt Humphrey, Oct 3, 2006
4. ### Patricia ShanahanGuest

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

Patricia Shanahan, Oct 3, 2006
5. ### Tim WardGuest

"Patricia Shanahan" <> wrote in message
news:j8vUg.2591\$...
>
> 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

Tim Ward, Oct 3, 2006
6. ### Chris UppalGuest

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

Chris Uppal, Oct 3, 2006
7. ### man4*.*Guest

man4*.*, Oct 3, 2006
8. ### Thomas WeidenfellerGuest

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-hierarchy/comp/comp.lang.java.gui/

Thomas Weidenfeller, Oct 4, 2006
9. ### himaloyGuest

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....

himaloy, Oct 4, 2006
10. ### Patricia ShanahanGuest

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

Patricia Shanahan, Oct 4, 2006
11. ### Lasse Reichstein NielsenGuest

"himaloy" <> writes:

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

What we really need is simultaneous assignment:

(x,y) = (y,x);

All these fancy algorithms should only be something one worried
about at the assembler level. Alas, few languages have this

/L
--
Lasse Reichstein Nielsen -
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'

Lasse Reichstein Nielsen, Oct 6, 2006
12. ### Tim WardGuest

"Lasse Reichstein Nielsen" <> wrote in message
news:...
> "himaloy" <> writes:
>
> > here u go...both values of x & y has been exchanged

>
> What we really need is simultaneous assignment:
>
> (x,y) = (y,x);
>
> All these fancy algorithms should only be something one worried
> about at the assembler level. Alas, few languages have this

IIRC, whilst BCPL didn't have the "simultaneous" bit you could at least
write the exchange all in one statement:

x, y, x ^:= y, x, y;

(I forget the symbol for XOR-becomes, it probably isn't ^:=.)

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

Tim Ward, Oct 9, 2006