{"id":25932,"date":"2017-10-25T21:52:29","date_gmt":"2017-10-25T16:22:29","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25932"},"modified":"2017-10-25T21:52:29","modified_gmt":"2017-10-25T16:22:29","slug":"add-1-given-number","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/add-1-given-number\/","title":{"rendered":"Add 1 to a given number"},"content":{"rendered":"<p>Write a program to add one to a given number. You are not allowed to use operators like \u2018+\u2019, \u2018-\u2018, \u2018*\u2019, \u2018\/\u2019, \u2018++\u2019, \u2018\u2013\u2018 \u2026etc.<\/p>\n<p>Examples:<br \/>\nInput: 12<br \/>\nOutput: 13<\/p>\n<p>Input: 6<br \/>\nOutput: 7<\/p>\n<p>Yes, you guessed it right, we can use bitwise operators to achieve this. Following are different methods to achieve same using bitwise operators.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Method 1<\/strong><br \/>\nTo add 1 to a number x (say 0011000111), we need to flip all the bits after the rightmost 0 bit (we get 0011000000). Finally, flip the rightmost 0 bit also (we get 0011001000) and we are done.<\/p>\n<p>&nbsp;<\/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 addOne(int x)<br\/>{<br\/>  int m = 1;<br\/> <br\/>  \/* Flip all the set bits until we find a 0 *\/<br\/>  while( x &amp; m )<br\/>  {<br\/>    x = x^m;<br\/>    m &lt;&lt;= 1;<br\/>  }<br\/> <br\/>  \/* flip the rightmost 0 bit *\/<br\/>  x = x^m;<br\/>  return x;<br\/>}<br\/> <br\/>\/* Driver program to test above functions*\/<br\/>int main()<br\/>{<br\/>  printf(&quot;%d&quot;, addOne(13));<br\/>  getchar();<br\/>  return 0;<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Method 2<\/strong><\/p>\n<p>We know that the negative number is represented in 2\u2019s complement form on most of the architectures. We have the following lemma hold for 2\u2019s complement representation of signed numbers.<\/p>\n<p>Say, x is numerical value of a number, then<\/p>\n<p>~x = -(x+1) [ ~ is for bitwise complement ]\n<p>(x + 1) is due to addition of 1 in 2\u2019s complement conversion<\/p>\n<p>To get (x + 1) apply negation once again. So, the final expression becomes (-(~x)).<\/p>\n<p>&nbsp;<\/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\">int addOne(int x)<br\/>{<br\/>   return (-(~x));<br\/>}<br\/> <br\/>\/* Driver program to test above functions*\/<br\/>int main()<br\/>{<br\/>  printf(&quot;%d&quot;, addOne(13));<br\/>  getchar();<br\/>  return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Example, assume the machine word length is one *nibble* for simplicity.<br \/>\nAnd x = 2 (0010),<br \/>\n~x = ~2 = 1101 (13 numerical)<br \/>\n-~x = -1101<br \/>\nInterpreting bits 1101 in 2\u2019s complement form yields numerical value as -(2^4 \u2013 13) = -3. Applying \u2018-\u2018 on the result leaves 3. Same analogy holds for decrement. See this comment for implementation of decrement.<br \/>\nNote that this method works only if the numbers are stored in 2\u2019s complement form.<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Add 1 to a given number &#8211; Bit Algorithm &#8211; Add 1 to a given number write a program to add 1 to a given number. You are not allowed to use operators like \u2018+\u2019,<\/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,83576],"tags":[70190,73242,70212,74370,74354,74363,74367,74347,74342,74371,74375,70194,74355,74357,74341,74345,74366,70187,74372,76250,76260,76263,76265,76249,76251,74352,74374,74361,74349,76259,76255,76248,74377,74348],"class_list":["post-25932","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-bit-algorithms","category-number","tag-binary","tag-binary-addition","tag-binary-code","tag-binary-computer-systems","tag-binary-digits","tag-binary-language","tag-binary-math","tag-binary-number-system-in-computer","tag-binary-numbers-in-computer","tag-binary-numbers-table","tag-binary-of-10","tag-binary-system","tag-binary-system-in-computer","tag-binary-system-uses-power-of","tag-computer-binary-numbers","tag-computer-number-system","tag-conversion-of-number-system","tag-decimal-to-binary","tag-explain-binary-system","tag-gauss-formula","tag-math-addition","tag-math-addition-games","tag-math-numbers","tag-maths-starters-ks1","tag-number-addition","tag-number-conversion-system","tag-number-system","tag-number-system-conversion","tag-number-system-in-computer","tag-simple-addition-and-subtraction","tag-simple-addition-games","tag-simple-addition-worksheets","tag-what-is-a-binary-number","tag-what-is-binary-system"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25932","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=25932"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25932\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}