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
Joined: Jan 30, 2002
Posts: 3451
posted
0
By using exclusive or operations:
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
R K Singh
Ranch Hand
Joined: Oct 15, 2001
Posts: 5369
posted
0
a=2; b=3; a = a+b; [a = 5] b = a-b; [b = 5-3 = 2] a = a-b; [a = 5-2 = 3]
"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18652
posted
0
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?
"I'm not back." - Bill Harding, Twister
Michael Morris
Ranch Hand
Joined: Jan 30, 2002
Posts: 3451
posted
0
What if the variables are float or double? Ravish's solution should work for all reals.
John Lee
Ranch Hand
Joined: Aug 05, 2001
Posts: 2545
posted
0
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
Joined: Jan 30, 2000
Posts: 18652
posted
0
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
Joined: Aug 05, 2001
Posts: 2545
posted
0
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
Joined: Jan 30, 2002
Posts: 3451
posted
0
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
Joined: Jan 30, 2002
Posts: 3451
posted
0
Back to XOR for FPs:
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18652
posted
0
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
Joined: Nov 09, 2000
Posts: 6450
posted
0
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. 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
Joined: Jun 02, 2003
Posts: 7
posted
0
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
Joined: Aug 20, 2003
Posts: 20
posted
0
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
Joined: Oct 29, 2001
Posts: 449
posted
0
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 ]
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
Joined: May 18, 2001
Posts: 81
posted
0
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
Joined: Aug 20, 2003
Posts: 20
posted
0
That is very clever!
Jan Liask
Greenhorn
Joined: Aug 31, 2003
Posts: 6
posted
0
wow... impressive... but is that valid??:/
Read my diary<br />http://1981.diaryland.com<br />or go to my homepage (If you want some handy info on gaming):<br />http://www.angeltowns.com/members/casinoheaven
Vivek Kumar
Greenhorn
Joined: Sep 23, 2003
Posts: 9
posted
0
---------------------------- 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
Joined: Nov 09, 2000
Posts: 6450
posted
0
"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
Joined: May 17, 2003
Posts: 33
posted
0
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
Joined: Jan 30, 2000
Posts: 18652
posted
0
I found it to be correct (at least for both false). What am I missing? Nothing; you're correct (as is Jignesh).