From: seebs@... (Peter Seebach) Date: 2007-07-28T04:16:28+09:00 Subject: Re: How to pass arguments by reference in a function In message <0514e8b08bc1a44593d7ffe0ddb5117e@ruby-forum.com>, Lloyd Linklater w rites: >That reminds me, in the old days you could do a swap without a third >variable using XOR. e.g. > >a = 13 >b = 17 > >a ^= b >b ^= a >a ^= b > >and they are swapped. > >I know that Ruby might do it as mentioned above. > >My question is, which is more efficient? The xor swap is virtually never more efficient on reasonable hardware. A casual pass through suggests that, even for the special case of numbers where a, b, and a^b are all Fixnum-ranged, the straight swap ought to be noticably faster. More importantly: How could doing it that way ever be idiomatic or easy to understand? I cannot conceive of a program where I have to swap two objects quickly, and the need to swap them quickly is so great that a hack like that would be worth it... and I can't just bypass the swap by improving the algorithm. -s