{"id":25793,"date":"2017-10-25T20:39:27","date_gmt":"2017-10-25T15:09:27","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25793"},"modified":"2017-10-25T20:39:27","modified_gmt":"2017-10-25T15:09:27","slug":"add-two-numbers-without-using-arithmetic-operators","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/add-two-numbers-without-using-arithmetic-operators\/","title":{"rendered":"Add two numbers without using arithmetic operators"},"content":{"rendered":"<p>Write a function Add() that returns sum of two integers. The function should not use any of the arithmetic operators (+, ++, \u2013, -, .. etc).<\/p>\n<p>Sum of two bits can be obtained by performing XOR (^) of the two bits. Carry bit can be obtained by performing AND (&amp;) of two bits.<br \/>\nAbove is simple Half Adder logic that can be used to add 2 single bits. We can extend this logic for integers. If x and y don\u2019t have set bits at same position(s), then bitwise XOR (^) of x and y gives the sum of x and y. To incorporate common set bits also, bitwise AND (&amp;) is used. Bitwise AND of x and y gives all carry bits. We calculate (x &amp; y) &lt;&lt; 1 and add it to x ^ y to get the required result.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">c<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include&lt;stdio.h&gt;<br\/> <br\/>int Add(int x, int y)<br\/>{<br\/>    \/\/ Iterate till there is no carry  <br\/>    while (y != 0)<br\/>    {<br\/>        \/\/ carry now contains common set bits of x and y<br\/>        int carry = x &amp; y;  <br\/> <br\/>        \/\/ Sum of bits of x and y where at least one of the bits is not set<br\/>        x = x ^ y; <br\/> <br\/>        \/\/ Carry is shifted by one so that adding it to x gives the required sum<br\/>        y = carry &lt;&lt; 1;<br\/>    }<br\/>    return x;<br\/>}<br\/> <br\/>int main()<br\/>{<br\/>    printf(&quot;%d&quot;, Add(15, 32));<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Following is recursive implementation for the same approach.<\/p>\n<div class=\"line number1 index0 alt2 highlighted\"><code class=\"c color1 bold\">int<\/code> <code class=\"c plain\">Add(<\/code><code class=\"c color1 bold\">int<\/code> <code class=\"c plain\">x, <\/code><code class=\"c color1 bold\">int<\/code> <code class=\"c plain\">y)<\/code><\/div>\n<div class=\"line number2 index1 alt1 highlighted\"><code class=\"c plain\">{<\/code><\/div>\n<div class=\"line number3 index2 alt2 highlighted\"><code class=\"c spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"c keyword bold\">if<\/code> <code class=\"c plain\">(y == 0)<\/code><\/div>\n<div class=\"line number4 index3 alt1 highlighted\"><code class=\"c spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"c keyword bold\">return<\/code> <code class=\"c plain\">x;<\/code><\/div>\n<div class=\"line number5 index4 alt2 highlighted\"><code class=\"c spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"c keyword bold\">else<\/code><\/div>\n<div class=\"line number6 index5 alt1 highlighted\"><code class=\"c spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"c keyword bold\">return<\/code> <code class=\"c plain\">Add( x ^ y, (x &amp; y) &lt;&lt; 1);<\/code><\/div>\n<div class=\"line number7 index6 alt2 highlighted\"><code class=\"c plain\">}<\/code><\/div>\n<div>[ad type=&#8221;banner&#8221;]<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Add two numbers without using arithmetic operators &#8211; Bit Algorithm &#8211; Sum of two bits can be obtained by performing XOR (^) of the two bits.<\/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":[75445,75451,75444,75448,75434,70840,75453,75438,75462,75449,75452,75446,75443,74843,75435,75429,75450,75441,75456,75437,74369,74379,75465,70857,75436,75454,75442,75428,75461,75458,74896,75447,75459,75425,56754,75431,75426,75440,75427,75439,75433,75432,75464,75455,75460,75430,75463,75424,75457,75466],"class_list":["post-25793","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-bit-algorithms","tag-3-result","tag-addition-in-java","tag-addition-of-two-numbers-in-c-language","tag-addition-of-two-numbers-in-javascript","tag-addition-of-two-numbers-in-python","tag-addition-program-in-c","tag-addition-program-in-java","tag-algebraic-operations","tag-algorithm-for-adding-two-numbers","tag-arithmetic-calculation","tag-arithmetic-examples","tag-arithmetic-operations","tag-arithmetic-operators","tag-arithmetic-operators-in-c","tag-arithmetic-operators-in-java","tag-arithmetic-symbols","tag-basic-arithmetic-operations","tag-basic-math-formulas","tag-basic-mathematical-operations","tag-basic-mathematical-operations-example","tag-binary-subtraction","tag-binary-subtraction-examples","tag-c-program-for-arithmetic-operations","tag-c-program-to-add-two-numbers","tag-computer-arithmetic","tag-example-of-arithmetic","tag-how-to-add-numbers","tag-how-to-add-two-numbers-in-java","tag-java-add","tag-java-addition-program","tag-java-math-operators","tag-java-program-for-addition","tag-java-program-to-add-two-numbers","tag-java-program-to-add-two-numbers-taking-input-from-user","tag-math-calculator","tag-math-operations","tag-mathematical-operators-in-c","tag-maths-sums","tag-mod-math","tag-modulus-operator","tag-operation-math","tag-php-program-to-add-two-numbers","tag-program-for-addition-of-two-numbers-in-java","tag-program-in-java-to-add-two-numbers","tag-put-any-mathematical-sign","tag-simple-addition-program-in-c","tag-simple-java-programs-for-addition-of-two-numbers","tag-simple-javascript-program-to-add-two-numbers","tag-square-root-multiplication-calculator","tag-write-a-program-to-add-two-numbers-without-using-operator"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25793","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=25793"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25793\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}