Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!

# Swapping Values

Sara Jahan
Ranch Hand
Posts: 120
Yeah - I seem to be the lucky one! I get to put the first topic here.
How can you swap the values of two variables without using a third, temp variable?

Michael Morris
Ranch Hand
Posts: 3451
By using exclusive or operations:

R K Singh
Ranch Hand
Posts: 5384
a=2;
b=3;
a = a+b; [a = 5]
b = a-b; [b = 5-3 = 2]
a = a-b; [a = 5-2 = 3]

Jim Yingst
Wanderer
Sheriff
Posts: 18671
What if the variables are float or double?
What if the variables are reference type? (I think this one's impossible in Java).
I'll skip the boolean question; too boring.
Why would these techniques be useful in some other languages?
Why are they much less useful in Java?

Michael Morris
Ranch Hand
Posts: 3451
What if the variables are float or double?
Ravish's solution should work for all reals.

John Lee
Ranch Hand
Posts: 2545
x = a ;
y = b ;
x = x + y = a + b ;
y = x - y = a + b - b = a ;
x = x - y = a + b - a = b ;
x = b ;
y = a ;

Jim Yingst
Wanderer
Sheriff
Posts: 18671
Ravish's solution should work for all reals.
Not if you want exact values. Try
double a = .1;
double b = .9;
and see what happens. And there are a few values for a and b which can completely screw things up...

John Lee
Ranch Hand
Posts: 2545
x = a ;
y = b ;
x = x * y = a * b ;
y = x / y = a * b / b = a ;
x = x / y = a * b / a = b ;
x = b ;
y = a ;
there is still pitfall.

Michael Morris
Ranch Hand
Posts: 3451
Not if you want exact values.
I see your point. I was thinking purely algebraically. You know how many times in my career that rounding errors have bit me? You'd think after so many years I'd learn. Just yesterday one of the guys came a told me, "You know that new filter program you wrote, all the elevations seem to be off a 1/16 inch after using it."

Michael Morris
Ranch Hand
Posts: 3451
Back to XOR for FPs:

Jim Yingst
Wanderer
Sheriff
Posts: 18671
Yup, that's the one I was thinking of. Though it's rather perverse - at this point, I'm thinking let's just grab eight bytes off the stack to allocate another local variable, why all these function calls? As a puzzle, it's kind of fun; but if a customer tried to make "no local variables" a requirement, they should probably just be shot.
Incidentally, I lied about the boolean case being "too boring" - it's actually not as straightforward as I thought. But, like the solution for doubles and floats, it falls under the category of "things you'd only do in a puzzle, not the real world".
[ June 01, 2003: Message edited by: Jim Yingst ]

Jason Menard
Sheriff
Posts: 6450
Originally posted by Michael Morris:
Not if you want exact values.
I see your point. I was thinking purely algebraically. You know how many times in my career that rounding errors have bit me? You'd think after so many years I'd learn. Just yesterday one of the guys came a told me, "You know that new filter program you wrote, all the elevations seem to be off a 1/16 inch after using it."

Check out the book Java Number Cruncher.

Michael Morris
Ranch Hand
Posts: 3451
Check out the book Java Number Cruncher.
Thanks for the info Jason. I got myself into cbd.booksonline, a Computer Book club, and that is one of the selections currently available. I'll probably pick it up.

Why would these techniques be useful in some other languages?
Bit flipping (assembly) languages because it reduces the number of registers used, the number of instructions used and the number of machine cycles used.

Kao-Wei Wan
Greenhorn
Posts: 7
Bit flipping (assembly) languages because it reduces the number of registers used, the number of instructions used and the number of machine cycles used.
well, I don't think so...
I don't know if it reduces the registers used(I think so but not sure),
but I don't think that it reduces the number of instructions used or the number of machine cycles used.
(1)
int a=0,b=100;
a^=b^=a^=b;
(2)
int a=0,b=100,c;
c=a; a=b; b=c;
which one is faster?? I don't think the answer is (1).
I think code(2) will be faster.
[ July 20, 2003: Message edited by: Kao-Wei Wan ]

Spike Spiegel
Greenhorn
Posts: 20
Hi,
I've been thinking about how to swap boolean values without using helper variables but I think it is impossible to do it in a more clever way. The only way I could come up with is this:

Who can think of another way of swapping 2 booleans?

Regards,
Hans
[ August 20, 2003: Message edited by: Hans Dorst ]

Vijay S. Rathore
Ranch Hand
Posts: 449
public void swapBoolean(boolean A, boolean B) {
System.out.println(A);
System.out.println(B);
if (A != B) {
A = A ? false : true;
B = !A;
}
System.out.println(A);
System.out.println(B);
}
[ August 20, 2003: Message edited by: Vijay Rathore ]

Vijay S. Rathore
Ranch Hand
Posts: 449
One more way to do it.
public void swapBoolean(boolean A, boolean B) {
System.out.println(A);
System.out.println(B);
if (A != B) {
A = B;
B = !A;
}
System.out.println(A);
System.out.println(B);
}
[ August 20, 2003: Message edited by: Vijay Rathore ]

Jignesh Malavia
Author
Ranch Hand
Posts: 81
Originally posted by Hans Dorst:
Hi,
I've been thinking about how to swap boolean values without using helper variables

How's this?
a = (a==b);
b = (a==b);
a = (a==b);

Spike Spiegel
Greenhorn
Posts: 20
That is very clever!

Greenhorn
Posts: 6
wow... impressive... but is that valid??:/

Vivek Kumar
Greenhorn
Posts: 9
----------------------------
Jignesh Malavia
How's this?
a = (a==b);
b = (a==b);
a = (a==b);
----------------------------
This is not valid. If both a and b are false, then
this sets both a and b to true.

Jason Menard
Sheriff
Posts: 6450
"young indus",
Welcome to JavaRanch! We don't have many rules here at the Ranch, but one that we do have is our naming policy. Please change your display name to comply with this policy.
Thanks in advance, and we look forward to seeing you around the Ranch.

Ranch Hand
Posts: 33
Originally posted by young indus:
----------------------------
Jignesh Malavia
How's this?
a = (a==b);
b = (a==b);
a = (a==b);
----------------------------
This is not valid. If both a and b are false, then
this sets both a and b to true.

I found it to be correct (at least for both false). What am I missing?
a == false
b == false
a = (false == false) => a == true
b = (true == false) => b == false
a = (true == false) => a = false
Final result:
a == false
b == false

Jim Yingst
Wanderer
Sheriff
Posts: 18671
I found it to be correct (at least for both false). What am I missing?
Nothing; you're correct (as is Jignesh).