{"id":26754,"date":"2017-12-22T20:53:15","date_gmt":"2017-12-22T15:23:15","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26754"},"modified":"2017-12-22T20:53:15","modified_gmt":"2017-12-22T15:23:15","slug":"turn-off-particular-bit-number","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/turn-off-particular-bit-number\/","title":{"rendered":"How to turn off a particular bit in a number"},"content":{"rendered":"<p><strong>Difficulty Level:<\/strong> Rookie<\/p>\n<p>Given a number n and a value k, turn of the k\u2019th bit in n.<\/p>\n<p><strong>Examples:<\/strong><\/p>\n<pre>Input:  n = 15, k = 1\r\nOutput: 14\r\n\r\nInput:  n = 15, k = 2\r\nOutput: 13\r\n\r\nInput:  n = 15, k = 3\r\nOutput: 11\r\n\r\nInput:  n = 15, k = 4\r\nOutput: 7\r\n\r\nInput:  n = 15, k &gt;= 5\r\nOutput: 15<\/pre>\n<p>The idea is to use bitwise &lt;&lt;, &amp; and ~ operators. Using expression &#8220;~(1 &lt;&lt; (k &#8211; 1))\u201c, we get a number which has all bits set, except the k\u2019th bit. If we do bitwise &amp; of this expression with n, we get a number which has all bits same as n except the k\u2019th bit which is 0.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>Following is C++ implementation of this.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C++ Programming<\/span> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">#include &lt;iostream&gt;<br\/>using namespace std;<br\/> <br\/>\/\/ Returns a number that has all bits same as n<br\/>\/\/ except the k&#039;th bit which is made 0<br\/>int turnOffK(int n, int k)<br\/>{<br\/>    \/\/ k must be greater than 0<br\/>    if (k &lt;= 0) return n;<br\/> <br\/>    \/\/ Do &amp; of n with a number with all set bits except<br\/>    \/\/ the k&#039;th bit<br\/>    return (n &amp; ~(1 &lt;&lt; (k - 1)));<br\/>}<br\/> <br\/>\/\/ Driver program to test above function<br\/>int main()<br\/>{<br\/>    int n = 15;<br\/>    int k = 4;<br\/>    cout &lt;&lt; turnOffK(n, k);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>7<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>C++ program How to turn off a particular bit in a number-Bit Algorithm-Given a number n and a value k, turn of the k\u2019th bit in n.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[74852,83515,1],"tags":[79805,79811,79815,79810,79816,79814,79812,79807,79809,79808,79802,79813,79804,79806,79803,79801],"class_list":["post-26754","post","type-post","status-publish","format-standard","hentry","category-bit-algorithms","category-c-programming-3","category-coding","tag-c-program-to-set-a-bit-clear-a-bit-and-check-a-bit","tag-c-reverse-bits","tag-flip-bits-geeksforgeeks","tag-flip-bits-java","tag-flipping-a-bit-in-java","tag-flipping-bits-hackerrank","tag-flipping-bits-program-in-c","tag-how-to-check-if-a-particular-bit-is-set-in-c","tag-how-to-flip-a-bit-in-c","tag-how-to-set-a-particular-bit-in-c","tag-how-to-toggle-a-bit-in-c","tag-invert-bits-to-obtain-a-number-in-java","tag-left-shifting-a-number-by-1-is-always-equivalent-to-multiplying-it-by-2","tag-toggle-all-bits-in-c","tag-which-bitwise-operator-is-suitable-for-checking-whether-a-particular-bit-is-on-or-off","tag-write-a-c-program-to-set-a-particular-bit-in-a-given-number"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26754","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=26754"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26754\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}