{"id":26637,"date":"2017-12-21T21:06:58","date_gmt":"2017-12-21T15:36:58","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26637"},"modified":"2017-12-21T21:06:58","modified_gmt":"2017-12-21T15:36:58","slug":"swap-two-numbers-without-using-temporary-variable","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/swap-two-numbers-without-using-temporary-variable\/","title":{"rendered":"swap two numbers without using a temporary variable"},"content":{"rendered":"<p>Given two variables, x and y, swap two variables without using a third variable.<span id=\"more-612\"><\/span><\/p>\n<p><strong>Method 1 (Using Arithmetic Operators)<\/strong><br \/>\nThe idea is to get sum in one of the two given numbers. The numbers can then be swapped using the sum and subtraction from sum.<\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%23include%20%3Cstdio.h%3E%0Aint%20main()%0A%7B%0A%20%20int%20x%20%3D%2010%2C%20y%20%3D%205%3B%0A%20%0A%20%20%2F%2F%20Code%20to%20swap%20\u2019x\u2019%20and%20\u2019y\u2019%0A%20%20x%20%3D%20x%20%2B%20y%3B%20%20%2F%2F%20x%20now%20becomes%2015%0A%20%20y%20%3D%20x%20-%20y%3B%20%20%2F%2F%20y%20becomes%2010%0A%20%20x%20%3D%20x%20-%20y%3B%20%20%2F%2F%20x%20becomes%205%0A%20%0A%20%20printf(%22After%20Swapping%3A%20x%20%3D%20%25d%2C%20y%20%3D%20%25d%22%2C%20x%2C%20y)%3B%0A%20%0A%20%20return%200%3B%0A%7D\u201d message=\u201dC Programming\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<pre>After Swapping: x = 5, y = 10<\/pre>\n<p>Multiplication and division can also be used for swapping.<\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%23include%20%3Cstdio.h%3E%0Aint%20main()%0A%7B%0A%20%20int%20x%20%3D%2010%2C%20y%20%3D%205%3B%0A%20%0A%20%20%2F%2F%20Code%20to%20swap%20\u2019x\u2019%20and%20\u2019y\u2019%0A%20%20x%20%3D%20x%20*%20y%3B%20%20%2F%2F%20x%20now%20becomes%2050%0A%20%20y%20%3D%20x%20%2F%20y%3B%20%20%2F%2F%20y%20becomes%2010%0A%20%20x%20%3D%20x%20%2F%20y%3B%20%20%2F%2F%20x%20becomes%205%0A%20%0A%20%20printf(%22After%20Swapping%3A%20x%20%3D%20%25d%2C%20y%20%3D%20%25d%22%2C%20x%2C%20y)%3B%0A%20%0A%20%20return%200%3B%0A%7D\u201d message=\u201dC Programming\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<pre>After Swapping: x = 5, y = 10<\/pre>\n[ad type=\u201dbanner\u201d]\n<p><strong>Method 2 (Using Bitwise XOR)<\/strong><br \/>\nThe bitwise XOR operator can be used to swap two variables. The XOR of two numbers x and y returns a number which has all the bits as 1 wherever bits of x and y differ. For example XOR of 10 (In Binary 1010) and 5 (In Binary 0101) is 1111 and XOR of 7 (0111) and 5 (0101) is (0010).<\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%23include%20%3Cstdio.h%3E%0Aint%20main()%0A%7B%0A%20%20int%20x%20%3D%2010%2C%20y%20%3D%205%3B%0A%20%0A%20%20%2F%2F%20Code%20to%20swap%20\u2019x\u2019%20(1010)%20and%20\u2019y\u2019%20(0101)%0A%20%20x%20%3D%20x%20%5E%20y%3B%20%20%2F%2F%20x%20now%20becomes%2015%20(1111)%0A%20%20y%20%3D%20x%20%5E%20y%3B%20%20%2F%2F%20y%20becomes%2010%20(1010)%0A%20%20x%20%3D%20x%20%5E%20y%3B%20%20%2F%2F%20x%20becomes%205%20(0101)%0A%20%0A%20%20printf(%22After%20Swapping%3A%20x%20%3D%20%25d%2C%20y%20%3D%20%25d%22%2C%20x%2C%20y)%3B%0A%20%0A%20%20return%200%3B%0A%7D\u201d message=\u201dC Programming\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<pre>After Swapping: x = 5, y = 10<\/pre>\n<p><strong>Problems with above methods<\/strong><br \/>\n<strong>1)<\/strong> The multiplication and division based approach doesn\u2019 work if one of the numbers is 0 as the product becomes 0 irrespective of the other number.<\/p>\n<p><strong>2)<\/strong> Both Arithmetic solutions may cause arithmetic overflow. If x and y are too large, addition and multiplication may go out of integer range.<\/p>\n<p><strong>3)<\/strong> When we use pointers to variable and make a function swap, all of the above methods fail when both pointers point to the same variable. Let\u2019s take a look what will happen in this case if both are pointing to the same variable.<\/p>\n<p>\/\/ Bitwise XOR based method<br \/>\nx = x ^ x; \/\/ x becomes 0<br \/>\nx = x ^ x; \/\/ x remains 0<br \/>\nx = x ^ x; \/\/ x remains 0<\/p>\n<p>\/\/ Arithmetic based method<br \/>\nx = x + x; \/\/ x becomes 2x<br \/>\nx = x \u2013 x; \/\/ x becomes 0<br \/>\nx = x \u2013 x; \/\/ x remains 0<\/p>\n[ad type=\u201dbanner\u201d]\n<p>Let us see the following program.<\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%23include%20%3Cstdio.h%3E%0Avoid%20swap(int%20*xp%2C%20int%20*yp)%0A%7B%0A%20%20%20%20*xp%20%3D%20*xp%20%5E%20*yp%3B%0A%20%20%20%20*yp%20%3D%20*xp%20%5E%20*yp%3B%0A%20%20%20%20*xp%20%3D%20*xp%20%5E%20*yp%3B%0A%7D%0A%20%0Aint%20main()%0A%7B%0A%20%20int%20x%20%3D%2010%3B%0A%20%20swap(%26x%2C%20%26x)%3B%0A%20%20printf(%22After%20swap(%26x%2C%20%26x)%3A%20x%20%3D%20%25d%22%2C%20x)%3B%0A%20%20return%200%3B%0A%7D\u201d message=\u201dC Programming\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<pre>After swap(&x, &x): x = 0<\/pre>\n<p>Swapping a variable with itself may needed in many standard algorithms. For example see <a href=\"http:\/\/geeksquiz.com\/quick-sort\/\" target=\"_blank\" rel=\"noopener noreferrer\">this <\/a>implementation of <a href=\"http:\/\/geeksquiz.com\/quick-sort\/\" target=\"_blank\" rel=\"noopener noreferrer\">QuickSort <\/a>where we may swap a variable with itself. The above problem can be avoided by putting a condition before the swapping.<\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%23include%20%3Cstdio.h%3E%0Avoid%20swap(int%20*xp%2C%20int%20*yp)%0A%7B%0A%20%20%20%20if%20(xp%20%3D%3D%20yp)%20%2F%2F%20Check%20if%20the%20two%20addresses%20are%20same%0A%20%20%20%20%20%20return%3B%0A%20%20%20%20*xp%20%3D%20*xp%20%2B%20*yp%3B%0A%20%20%20%20*yp%20%3D%20*xp%20-%20*yp%3B%0A%20%20%20%20*xp%20%3D%20*xp%20-%20*yp%3B%0A%7D%0Aint%20main()%0A%7B%0A%20%20int%20x%20%3D%2010%3B%0A%20%20swap(%26x%2C%20%26x)%3B%0A%20%20printf(%22After%20swap(%26x%2C%20%26x)%3A%20x%20%3D%20%25d%22%2C%20x)%3B%0A%20%20return%200%3B%0A%7D\u201d message=\u201dC Programming\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<pre>After swap(&x, &x): x = 10<\/pre>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>swap two numbers without using a temporary variable-Bit Algorithm-Given two variables, x and y, swap two variables without using a third variable.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,74852],"tags":[79522,71923,79544,79534,71906,79537,79520,75109,79512,71924,79516,79529,74875,79523,79538,79527,79513,71909,79515,71911,79526,71925,75118,75086],"class_list":["post-26637","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-bit-algorithms","tag-algorithm-for-swapping-of-two-numbers-in-c","tag-c-program-for-swapping","tag-c-program-for-swapping-of-two-numbers-using-pointers","tag-c-program-swap-two-numbers","tag-c-program-to-swap-two-numbers","tag-c-program-to-swap-two-numbers-using-call-by-value","tag-c-programming-swap-function","tag-c-xor","tag-call-by-reference-in-c","tag-flowchart-for-swapping-of-two-numbers","tag-temporary-number","tag-valuation-of-swaps","tag-variables-in-java","tag-wap-swap","tag-write-a-java-program-to-swap-two-numbers","tag-write-a-pl-sql-program-to-swap-two-numbers","tag-write-a-program-in-c-to-swap-two-numbers","tag-write-a-program-to-swap-two-numbers","tag-write-a-program-to-swap-two-numbers-using-function","tag-write-ac-program-to-swap-two-numbers","tag-write-ac-program-to-swap-two-numbers-using-function","tag-write-ac-program-to-swap-two-numbers-using-pointers","tag-xor-operation-in-c","tag-xor-operator-in-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26637"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26637\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}