Standard swap

From Wikipedia, the free encyclopedia

In computer science, the standard swap or three point turn[citation needed] is a programming paradigm that exchanges the values of two variables.

By using a third variable, whose value is otherwise of no concern, to hold the value of the first, and then assigning the second's to the first, and the third's back to the second, the values of the first two are swapped.

For example:

  1. let temp = b
  2. let b = a
  3. let a = temp

In modern[chronology citation needed] CPUs this is accomplished on the processor itself, in a single machine instruction, rather than having to go through RAM. In many programming languages this can be achieved using a simple swap instruction.


Wikiwand in your browser!

Seamless Wikipedia browsing. On steroids.

Every time you click a link to Wikipedia, Wiktionary or Wikiquote in your browser's search results, it will show the modern Wikiwand interface.

Wikiwand extension is a five stars, simple, with minimum permission required to keep your browsing private, safe and transparent.