{"id":25972,"date":"2017-10-25T22:04:49","date_gmt":"2017-10-25T16:34:49","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25972"},"modified":"2017-10-25T22:04:49","modified_gmt":"2017-10-25T16:34:49","slug":"turn-off-rightmost-set-bit","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/turn-off-rightmost-set-bit\/","title":{"rendered":"Turn off the rightmost set bit"},"content":{"rendered":"<p>Write a C function that unsets the rightmost set bit of an integer.<\/p>\n<p><strong>Examples:<\/strong><\/p>\n<pre>Input:  12 (00...01100)\r\nOutput: 8 (00...01000)\r\n\r\nInput:  7 (00...00111)\r\nOutput: 6 (00...00110)<\/pre>\n<p>Let the input number be n. n-1 would have all the bits flipped after the rightmost set bit (including the set bit). So, doing n&amp;(n-1) would give us 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\/>\/* unsets the rightmost set bit of n and returns the result *\/<br\/>int fun(unsigned int n)<br\/>{<br\/>  return n&amp;(n-1);<br\/>}    <br\/> <br\/>\/* Driver program to test above function *\/<br\/>int main()<br\/>{<br\/>  int n = 7;<br\/>  printf(&quot;The number after unsetting the rightmost set bit %d&quot;, fun(n));<br\/> <br\/>  getchar();<br\/>  return 0;<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Turn off the rightmost set bit &#8211; Bit Algorithm &#8211; Let the input number be n. n-1 would have all the bits flipped after the rightmost set bit .<\/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":[75090,76297,75106,76300,76306,76390,76277,76383,76380,76289,76381,76386,75264,75271,75086],"class_list":["post-25972","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-bit-algorithms","tag-how-many-bits-in-a-byte","tag-java-binary-operator","tag-java-binary-operators","tag-java-bit-shift-example","tag-java-bitwise","tag-java-shift-operator-example","tag-logical-operators-in-c","tag-low-order-bit","tag-meaning-of-least","tag-operator-c-programming","tag-shift-operator-in-java","tag-the-least-bit","tag-unsigned-int-in-c","tag-xor-0","tag-xor-operator-in-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25972","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=25972"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25972\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}