{"id":436825,"date":"2018-07-25T04:36:05","date_gmt":"2018-07-25T04:36:05","guid":{"rendered":"https:\/\/essaypaper.org\/an-example-of-swap-without-temp\/"},"modified":"2018-10-24T09:10:37","modified_gmt":"2018-10-24T09:10:37","slug":"an-example-of-swap-without-temp","status":"publish","type":"post","link":"https:\/\/www.benedictsol.com\/blogs\/an-example-of-swap-without-temp\/","title":{"rendered":"An Example of Swap Without Temp"},"content":{"rendered":"<div>\n<blockquote>\n<p><i><span style=\"font-weight: 400;\">Here is a solution to the problem of<\/span><\/i><i> swap without temp<\/i><i><span style=\"font-weight: 400;\">. Now a similar problem that a student may have can be solved as follows in our example. If our sample has made a good impression on you then you should consider getting help from Assignment.EssayShark.com. Our expert will follow all of your requirements and academic standards while dealing with your order. Next, we should mention that we are available 24\/7, so you can get our help any time you need.<\/span><\/i><\/p>\n<p><i><span style=\"font-weight: 400;\">The purpose of our service is to provide customers with qualified technical help with their homework. Even the most difficult task can be solved by our expert. Many students have already used our service and remain satisfied. Feedback that they have left can be easily found on our site. So, using our service, you can get a lot of benefits. It is clear that our samples can help you as well, so read the presented one to understand what we are talking about.<\/span><\/i><\/p>\n<\/blockquote>\n<p><span style=\"font-weight: 400;\">Swap the Values of Variables Without Use of the Temporary Variables <\/span><\/p>\n<p><span style=\"font-weight: 400;\">This is a classic problem. It is usually offered in interviews, and it is quite simple. Assume x0 is the original value of x,<\/span> <span style=\"font-weight: 400;\">and y0 is the original value of y. Assume dif as a difference between x and y (x \u2013 b). Let\u2019s show the relative position of all of these values on the real axis in the case when x &gt; y. <\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8148\" src=\"https:\/\/assignment.essayshark.com\/blog\/wp-content\/uploads\/2018\/03\/swap.png\" alt=\"\" width=\"253\" height=\"72\"\/><\/p>\n<p><span style=\"font-weight: 400;\">Assign x the value of dif. If we add the value of y and dif, we will get x0 and save the result to y. Now we have y = x0 and x = dif. Now all we have to do is assign y the value of x0 \u2013 dif, which is equal to y \u2013 x.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The following code implements this algorithm:<\/span><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">&#13;\npublic static void swap(int x, int y) {&#13;\n    \/\/for example, let x = 10, y = 6;&#13;\n    x = x - y; \/\/ x = 10 - 4 = 6&#13;\n    y = x + y; \/\/ y = 6 + 4 = 10&#13;\n    x = y - x; \/\/ x = 10 - 6&#13;\n &#13;\n    System.out.println(\"x: \" + x);&#13;\n    System.out.println(\"y: \" + y);&#13;\n}&#13;\n<\/pre>\n<p>We can also solve this problem using a bit manipulation. This approach will allow us to work with different types of data, not just integers.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">&#13;\npublic static void swap(int x, int y) {&#13;\n    \/\/for example, x = 101 (binary), y = 110&#13;\n    x = x ^ y; \/\/ x = 101^110 = 011&#13;\n    y = x ^ y; \/\/ y = 011^110 = 101&#13;\n    x = x ^ y; \/\/ x = 011^101 = 110&#13;\n &#13;\n    System.out.println(\"x: \" + x);&#13;\n    System.out.println(\"y: \" + y);&#13;\n}&#13;\n<\/pre>\n<p>This code uses the operation XOR. The easiest way to understand the code is to look at the two bits a and b. Let\u2019s denote the initial values as a, a0, and b0.<br \/>If we will be able to swap a couple of bits, the algorithm will work correctly. Let\u2019s look at the work of the algorithm step by step:<\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">a = a0 ^ b0 (0 if a0 = b0, 1 if a0 ! = b0)<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">b = a ^ b0 (equal to the value of a0)<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">a = a ^ b (equal to b0)<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">In line 1 we perform a = a0^b0 operation, which will be equal to 0 if a0 = b0, and equal to 1 if a0 != b0.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In line 2 we perform the b = a ^ b0 operation. Let\u2019s analyze both possible values of a. Since we want to swap the values of a and b, we want to get 0 as a result:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">a = 0: in this case a0 = b0 because we want to return a0 and b0. Applying XOR for any value will return the original value, so the result of this operation will be b0 (or a0).<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">a = 1: in this case a0 != b0. As a result, we get the inversion of a0. This is exactly what we want because a0 != b0.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\"> at the moment is equal to a0; so, in fact, we do a^a0.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">a = 0: Since a0 = b0, we want to return a0 or b0. Performing 0^a0 we will return a0(b0).<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">a = 1: We perform 1^a0. As a result, we will get an inversion of a0, which is exactly what we need, because a0 != b0.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Now we should assign a the value of b0, and assign b the value of a0. Now we are sure that our algorithm reverses each bit correctly, which means that the result is correct.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Thanks for your attention!<\/span><\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Here is a solution to the problem of swap without temp. Now a similar problem that a student may have can be solved as follows in our example. If our sample has made a good impression on you then you <a href=\"https:\/\/www.benedictsol.com\/blogs\/an-example-of-swap-without-temp\/\" class=\"read-more\">Read More &#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,25],"tags":[],"class_list":["post-436825","post","type-post","status-publish","format-standard","hentry","category-essay-paper-writing","category-samples"],"_links":{"self":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts\/436825","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/comments?post=436825"}],"version-history":[{"count":0,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/posts\/436825\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/media?parent=436825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/categories?post=436825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.benedictsol.com\/blogs\/wp-json\/wp\/v2\/tags?post=436825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}