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.... :-)
> hello...
> as a beginer in Java I've got a few workbooks for practice, so
[quoted text clipped - 6 lines]
> 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

Signature
Tim Ward
Brett Ward Limited - www.brettward.co.uk
> hello...
> as a beginer in Java I've got a few workbooks for practice, so
[quoted text clipped - 6 lines]
> 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 matth@ivizNOSPAM.com http://www.iviz.com/
> hello...
> as a beginer in Java I've got a few workbooks for practice, so
[quoted text clipped - 6 lines]
> 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 - 03 Oct 2006 16:29 GMT
> 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?

Signature
Tim Ward
Brett Ward Limited - www.brettward.co.uk
Chris Uppal - 03 Oct 2006 16:54 GMT
> > I've known the technique for about 20 years. I have yet to encounter a
> > situation in which I've needed to use it.
[quoted text clipped - 4 lines]
> 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*.* - 03 Oct 2006 18:13 GMT
> 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 - 04 Oct 2006 08:05 GMT
> 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

Signature
The comp.lang.java.gui FAQ:
http://gd.tuwien.ac.at/faqs/faqs-hierarchy/comp/comp.lang.java.gui/
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
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
> hello...
> as a beginer in Java I've got a few workbooks for practice, so
[quoted text clipped - 6 lines]
> y=temp;
> this would be easier...but they have to complicate.... :-)
Patricia Shanahan - 04 Oct 2006 19:24 GMT
> my professor taught us a way of swaping variables without using any
> temp variable
[quoted text clipped - 7 lines]
>
> 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 class BadDoubleSwap {
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
Lasse Reichstein Nielsen - 06 Oct 2006 19:31 GMT
> 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
very helpful feature :(
/L

Signature
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Tim Ward - 09 Oct 2006 09:43 GMT
> > here u go...both values of x & y has been exchanged
>
[quoted text clipped - 5 lines]
> about at the assembler level. Alas, few languages have this
> very helpful feature :(
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 ^:=.)

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