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 , two variables may be defined thus (in pseudocode ):
17-494: (Redirected from SWAP ) [REDACTED] Look up swap in Wiktionary, the free dictionary. Swap or SWAP may refer to: Finance [ edit ] Swap (finance) , a derivative in which two parties agree to exchange one stream of cash flows against another Barter Science and technology [ edit ] Swap (computer programming) , exchanging two variables in
34-419: A 2015 Philippine crime drama film See also [ edit ] Swaps (horse) (1952–1972), a California-bred American Thoroughbred racehorse Swapping (disambiguation) The Swap Topics referred to by the same term [REDACTED] This disambiguation page lists articles associated with the title Swap . If an internal link led you here, you may wish to change the link to point directly to
51-419: A 2015 Philippine crime drama film See also [ edit ] Swaps (horse) (1952–1972), a California-bred American Thoroughbred racehorse Swapping (disambiguation) The Swap Topics referred to by the same term [REDACTED] This disambiguation page lists articles associated with the title Swap . If an internal link led you here, you may wish to change the link to point directly to
68-436: A problem in most applications, the sizes of the values being swapped may be huge (which means the temporary variable may occupy a lot of memory as well), or the swap operation may need to be performed many times, as in sorting algorithms. In addition, swapping two variables in object-oriented languages such as C++ may involve one call to the class constructor and destructor for the temporary variable, and three calls to
85-540: A single operation, by swapping the pointers alone. This is usually found in programming languages supporting pointers, like C or C++ . The Standard Template Library overloads its built-in swap function to exchange the contents of containers efficiently this way. As pointer variables are usually of a fixed size (e.g., most desktop computers have pointers 64 bits long), and they are numeric, they can be swapped quickly using XOR swap . Some languages, like Ruby or Python support parallel assignments , which simplifies
102-471: A space instrument aboard the PROBA2 satellite SWAP (New Horizons) (Solar Wind At Pluto), a science instrument aboard the uncrewed New Horizons space probe SWAP protein domain , in molecular biology Size, weight and power (SWaP), see DO-297 Other [ edit ] Swåp , an Anglo-Swedish folk music band Sector-Wide Approach (SWAp), an approach to international development Swap (film) ,
119-406: Is generally used to swap low-level data types, like integers . However, it is, in theory, capable of swapping any two values which can be represented by fixed-length bitstrings . This method swaps two variables by adding and subtracting their values. This is rarely used in practical applications, mainly because: Containers which allocate memory from the heap using pointers may be swapped in
136-508: The copy constructor . Some classes may allocate memory in the constructor and deallocate it in the destructor, thus creating expensive calls to the system. Copy constructors for classes containing a lot of data, e.g. in an array , may even need to copy the data manually. XOR swap uses the XOR operation to swap two numeric variables. It is generally touted to be faster than the naive method mentioned above; however it does have disadvantages . XOR swap
153-568: The ability to swap variables directly through built-in instructions. x86 processors, for example, include an XCHG instruction to swap two registers directly without requiring that a third temporary register is used. A compare-and-swap instruction is even provided in some processor architectures, which compares and conditionally swaps two registers. This is used to support mutual exclusion techniques. XCHG may not be as efficient as it seems. For example, in x86 processors, XCHG will implicitly lock access to any operands in memory to ensure
170-518: The free dictionary. Swap or SWAP may refer to: Finance [ edit ] Swap (finance) , a derivative in which two parties agree to exchange one stream of cash flows against another Barter Science and technology [ edit ] Swap (computer programming) , exchanging two variables in the memory of a computer Swap partition , a partition of a computer data storage used for paging SWAP (instrument) (Sun Watcher using Active Pixel System Detector and Image Processing),
187-409: The intended article. Retrieved from " https://en.wikipedia.org/w/index.php?title=Swap&oldid=1259698048 " Category : Disambiguation pages Hidden categories: Short description is different from Wikidata All article disambiguation pages All disambiguation pages swap [REDACTED] Look up swap in Wiktionary,
SECTION 10
#1732863335375204-602: The intended article. Retrieved from " https://en.wikipedia.org/w/index.php?title=Swap&oldid=1259698048 " Category : Disambiguation pages Hidden categories: Short description is different from Wikidata All article disambiguation pages All disambiguation pages Swap (computer programming) After swap() is performed, x will contain the value 0 and y will contain 1; their values have been exchanged. This operation may be generalized to other types of values, such as strings and aggregated data types . Comparison sorts use swaps to change
221-618: The memory of a computer Swap partition , a partition of a computer data storage used for paging SWAP (instrument) (Sun Watcher using Active Pixel System Detector and Image Processing), a space instrument aboard the PROBA2 satellite SWAP (New Horizons) (Solar Wind At Pluto), a science instrument aboard the uncrewed New Horizons space probe SWAP protein domain , in molecular biology Size, weight and power (SWaP), see DO-297 Other [ edit ] Swåp , an Anglo-Swedish folk music band Sector-Wide Approach (SWAp), an approach to international development Swap (film) ,
238-557: The notation for swapping two variables: This is shorthand for an operation involving an intermediate data structure: in Python, a tuple; in Ruby, an array. Javascript 6+ supports destructuring operators which do the same thing: Here, two globally scoped variables are passed by value through a function, eliminating the need for a temporary storage variable. Because of the many applications of swapping data in computers, most processors now provide
255-543: The operation is atomic , and so may not be efficient when swapping memory. Such locking is important when it is used to implement thread-safe synchronization, as in mutexes . However, an XCHG is usually the fastest way to swap two machine-size words residing in registers . Register renaming may also be used to swap registers efficiently. With the advent of instruction pipelining in modern computers and multi-core processors facilitating parallel computing , two or more operations can be performed at once. This can speed up
272-502: The positions of data. In many programming languages the swap function is built-in. In C++ , overloads are provided allowing std::swap to exchange some large structures in O(1) time. The simplest and probably most widely used method to swap two variables is to use a third temporary variable : While this is conceptually simple and in many cases the only convenient way to swap two variables, it uses extra memory. Although this should not be
289-549: The swap using temporary variables and give it an edge over other algorithms. For example, the XOR swap algorithm requires sequential execution of three instructions. However, using two temporary registers, two processors executing in parallel can swap two variables in two clock cycles: More temporary registers are used, and four instructions are needed instead of three. In any case, in practice this could not be implemented in separate processors, as it violates Bernstein's conditions for parallel computing. It would be infeasible to keep
#374625