XOR swap/exchange: nowadays an almost extinct means to exchange two distinct variables of the same size
The Wiert Corner – Almost a year ago, a thread on “premature Delphi optimization” came by on G+ about this code: procedure ExchangeInteger(var AValue1, AValue2: Integer); begin AValue1 := AValue1 xor AValue2; AValue2 := AValue1 xor AValue2; AValue1 := AValue1 xor AValue2; end; I don’t think that was premature optimization, just some code from an old fart that had already been programming in the era where processors had reasons to use it: limited instruction set few registers limited amount of RAM. Back then, the only efficient way to exchange two variables of the same data type was ...
Visits by Source
Users who voted for this posting