in reply to Flying Obfu

This was done for speed. XOR is a very fast operation on some processor types (e.g., 8086). Along similar lines, we were taught to use XOR to zero a register, because XORing it with itself was much faster than assigning a constant value to the register. Whether it's actually any faster in Perl than the equivalent swap, I don't know, but it would be faster in some assembly languages. So, three XORs would accomplish the swap faster than the usual three assignments, too. How many times a second does this need to run? You might want to leave it alone, if it's a flight control system, or at least do a benchmark before you change it.

Replies are listed 'Best First'.
Re: Re: Flying Obfu
by awkmonk (Monk) on Apr 14, 2003 at 16:02 UTC

    Not much chance of this one being changed - the paperwork would be horrendous (it's part of the flight control of a Tornado).

    The person that wrote it clearly didn't do it for speed as they didn't recognise that 2 if's are quicker than an and.

    It appears to have been done primarily to avoid introducing a third variable. No ($a, $b) = ($b, $a) functionality available. Even so, I still believe it to be a prime example of 'how not to code' in a high risk, real time environment when it should count as 'quite important' that maintenance staff can clearly follow the code.

    We live and learn I suppose.


    'I think the problem lies in the fact that your data doesn't fit my program'.