{"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&(n-1) would give us the required result.<\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%23include%3Cstdio.h%3E%0A%20%0A%2F*%20unsets%20the%20rightmost%20set%20bit%20of%20n%20and%20returns%20the%20result%20*%2F%0Aint%20fun(unsigned%20int%20n)%0A%7B%0A%20%20return%20n%26(n-1)%3B%0A%7D%20%20%20%20%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20function%20*%2F%0Aint%20main()%0A%7B%0A%20%20int%20n%20%3D%207%3B%0A%20%20printf(%22The%20number%20after%20unsetting%20the%20rightmost%20set%20bit%20%25d%22%2C%20fun(n))%3B%0A%20%0A%20%20getchar()%3B%0A%20%20return%200%3B%0A%7D\u201d message=\u201dc\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\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}]}}