Toepopper wrote
Very very common, alas. I once interviewed a candidate for a VBA job (yes, you can stop booing for the peanut gallery) whom I asked to swap two variable contents without using a temp variable. It’s the standard a=a+b, b=a-b, a=a-b problem. His answer? Well, I can’t do it in VBA, but if you let me use Excel I can put the values in two cells and swap the cells’ contents using a third cell.
We hired the guy who said, well, “if they’re integers, then I’d do it by a=a|b, b=a^b, a=a^b. But I don’t know how to do it if they’re strings.”
Toepopper on February 27, 2007 01:49 AM
I think Toepopper hired the wrong fellow, because his "swap program"
is logically flawed. After looking up the meaning of the | ^ operators (i.e. | = bitwise OR and ^ = bitwise XOR), I attempted a formal proof. I could not get it to work out. So I tried a simple case. Suppose we have 1-bit words and
a = 1
b = 1
After a = a|b:
a = 1
After b = a^b
b = 0
After a = a^b
a = 1
And you can see, a swap has not occurred.