Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Swapping Values

 
Sara Jahan
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
A friend of mine was this in an interview!
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By using exclusive or operations:
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if the variables are float or double?
Ravish's solution should work for all reals.
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Back to XOR for FPs:
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about this method. This is the best I have come up with
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is very clever!
 
Jan Liask
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow... impressive... but is that valid??:/
 
Vivek Kumar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
----------------------------
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Rafael Prado
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found it to be correct (at least for both false). What am I missing?
Nothing; you're correct (as is Jignesh).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic