> public static void swap(Integer x,Integer y)
> {
> x^=y^=x^=y;
> }
That is (I think, I wouldn't write code like that!)
public static void swap(Integer x, Integer y) {
x = Integer.valueOf(x.intValue() ^ y.intValue());
y = Integer.valueOf(y.intValue() ^ x.intValue());
x = Integer.valueOf(x.intValue() ^ y.intValue());
}
> Can anybody tell me why this program is not giving correct o/p i.e
Anyway, Java is always call by value, even when those values are
references (this makes code very much more easy to follow). Your code is
just changing the references (which are copies), not the objects pointed to.
You could write something similar that changes the object themselves.
Integer is immutable, so you will need a different type.
import java.util.concurrent.atomic.AtomicInteger;
...
public static void swap(
final AtomicInteger x, final AtomicInteger y
) {
x.set(x.get() ^ y.get());
y.set(y.get() ^ x.get());
x.set(x.get() ^ y.get());
}
Tom Hawtin
Patricia Shanahan - 19 Aug 2007 06:58 GMT
...
> You could write something similar that changes the object themselves.
> Integer is immutable, so you will need a different type.
[quoted text clipped - 8 lines]
> x.set(x.get() ^ y.get());
> }
Also, many uses of swap really exchange elements of the same array:
/* swap elements i and j of array.*/
public static void intArraySwap(int[] array, int i, int j){
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
Incidentally, in Java code, why do people prefer the xor-swap to using a
temporary? To me, the temporary version seems much more direct and obvious.
Patricia
Thomas Hawtin - 19 Aug 2007 16:49 GMT
> Incidentally, in Java code, why do people prefer the xor-swap to using a
> temporary? To me, the temporary version seems much more direct and obvious.
Because it shows l33t h4x0r ski11z? It made sense in assembler on
pre-RISC processors with few registers. It makes little sense on heavily
optimised processors with large register files. It makes even less sense
in a 3GL.
I suggest to anyone who thinks xor swaps are k3wl, that they work out
their frustrations with a copy of Hacker's Delight and not in their
common or garden production or example code.
http://www.amazon.co.uk/dp/0201914654
Tom Hawtin
Mike Schilling - 19 Aug 2007 18:22 GMT
>> Incidentally, in Java code, why do people prefer the xor-swap to
>> using a temporary? To me, the temporary version seems much more
[quoted text clipped - 4 lines]
> heavily optimised processors with large register files. It makes even
> less sense in a 3GL.
There is a "neat trick" for making a singly-linked list that can be
traversed in both directions by having each "pointer" be the XOR of the
addresses of he previous and next members. It doesn't work in Java, which
is probably a good thing. And it breaks conservative garbage collectors in
C++.
Thomas Hawtin - 19 Aug 2007 18:50 GMT
> There is a "neat trick" for making a singly-linked list that can be
> traversed in both directions by having each "pointer" be the XOR of the
> addresses of he previous and next members. It doesn't work in Java, which
> is probably a good thing. And it breaks conservative garbage collectors in
> C++.
Now that is a neat trick. You could write impure Java (with
sun.misc.Unsafe for instance) to do it. However, you would still confuse
the garbage collector. I guess you could have an array to store
references for GC, but that would still hinder those GC algorithms that
arrange objects close to referrers (as would a linked list algorithm
that replaced nodes with arrays of references and (xor) indexes to next
'node' - but it makes me want to try it).
Tom Hawtin
Patricia Shanahan - 20 Aug 2007 01:07 GMT
>>> Incidentally, in Java code, why do people prefer the xor-swap to
>>> using a temporary? To me, the temporary version seems much more
[quoted text clipped - 9 lines]
> is probably a good thing. And it breaks conservative garbage collectors in
> C++.
I'm aware of several situations in which the xor-swap trick is useful.
For example, it can be used to implement swap as a C expression for use
in a pre-processor macro.
I'm trying to find out why people use it in ordinary Java code. I don't
think any of the special cases apply, but maybe I'm missing something.
The other possibility is that users of xor-swap consider it better style
than temp-swap. If so, why?
Patricia
Twisted - 20 Aug 2007 04:02 GMT
> The other possibility is that users of xor-swap consider it better style
> than temp-swap. If so, why?
At a guess, they simply find the use of a temporary, especially an
explicit temporary, to be ugly.
Roedy Green - 22 Aug 2007 12:07 GMT
>The other possibility is that users of xor-swap consider it better style
>than temp-swap. If so, why?
It is magic. It is cool. It is a way of showing off.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
> public static void swap(Integer x,Integer y)
> {
> x^=y^=x^=y;
> }
you can't write a swap routine in Java.
See http://mindprod.com/jgloss/swap.html
http://mindprod.com/jgloss/callbyreference.html
http://mindprod.com/jgloss/callbyvalue.html
Though it is theoretically allowed, chaining operators like than is
just a way to tick people off. Don't do it. It's only function is
writing unmaintainable code. See
http://mindprod.com/jgloss/unmain.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com