Standardswappingalgorithmsrequiretheuseofatemporarystoragevariable. UsingtheXORswapalgorithm, however, notemporarystorageisneeded. Thealgorithmisasfollows:X:= XXORYY:= XXORYX:= XXORYThealgorithmtypicallycorrespondstothreemachinecodeinstructions. Forexample, inIBMSystem/370assemblycode:XRR1,R2XRR2,R1XRR1,R2whereR1andR2areregistersandeachXRoperationleavesitsresultintheregisternamedinthefirstargument.
However, theproblemstillremainsthatif "x" and "y" usethesamestoragelocation, thevaluestoredinthatlocationwillbezeroedoutbythefirstXORinstruction, andthenremainzero; itwillnotbe "swappedwithitself". (Notethatthisis "not" thesameasif "x" and "y" havethesamevalues. Thetroubleonlycomeswhen "x" and "y" usethesamestoragelocation.)
SupposethatwehavetworegistersR1andR2, asinthetablebelow, withinitialvalues "A" and "B" respectively. Weperformtheoperationsbelowinsequence, andreduceourresultsusingthepropertieslistedabove.
XOR linked list — XOR linked lists are a data structure used in computer programming. They take advantage of the bitwise exclusive disjunction (XOR) operation, here denoted by ⊕, to decrease storage requirements for doubly linked lists. An ordinary doubly linked… … Wikipedia
Swap (computer science) — For other uses of swap , see swap (disambiguation). In computer programming, the act of swapping two variables refers to mutually exchanging the values of the variables. Usually, this is done with the data in memory. For example, in a program,… … Wikipedia
Échange (informatique) — En programmation informatique, l échange de deux variables (en anglais swap) consiste à intervertir leurs valeurs. Il s agit d une opération de base intégrée à de nombreux langages de programmation et faisant partie du jeu d instructions de… … Wikipédia en Français
Exclusive or — The logical operation exclusive disjunction, also called exclusive or (symbolized XOR or EOR), is a type of logical disjunction on two operands that results in a value of “true” if and only if exactly one of the operands has a value of “true”. [… … Wikipedia
List of algorithms — The following is a list of the algorithms described in Wikipedia. See also the list of data structures, list of algorithm general topics and list of terms relating to algorithms and data structures.If you intend to describe a new algorithm,… … Wikipedia
Bitwise operation — In computer programming, a bitwise operation operates on one or two bit patterns or binary numerals at the level of their individual bits. On most microprocessors, bitwise operations are sometimes slightly faster than addition and subtraction… … Wikipedia
Алгоритм обмена при помощи исключающего ИЛИ — В программировании, обмен при помощи исключающего ИЛИ (англ. Xor swap algorithm, кзор своп алгоритм) это алгоритм, в котором используется операция исключающего ИЛИ (XOR), для обмена значениями между переменными, которые содержат данные … Википедия
Зор-своп алгоритм — В программировании, обмен при помощи исключающего ИЛИ (англ. Xor swap algorithm, кзор своп алгоритм) это алгоритм, который использует операцию исключающего ИЛИ (XOR) для обмена различных значений переменных, имеющих один и тот же тип данных без… … Википедия
Temporary variable — In computer programming, a temporary variable is a variable whose purpose is short lived, usually to hold temporarily data that will soon be discarded, or before it can be placed at a more permanent memory location. Because it is short lived, it… … Wikipedia
Aliasing (computing) — In computing, aliasing describes a situation in which a data location in memory can be accessed through different symbolic names in the program. Thus, modifying the data through one name implicitly modifies the values associated to all aliased… … Wikipedia