{"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 >= 5\r\nOutput: 15<\/pre>\n<p>The idea is to use bitwise <<, & and ~ operators. Using expression \u201c~(1 << (k \u2013 1))\u201c, we get a number which has all bits set, except the k\u2019th bit. If we do bitwise & 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=\u201dbanner\u201d]\n<p>Following is C++ implementation of this.<\/p>\n[pastacode lang=\u201dcpp\u201d manual=\u201d%23include%20%3Ciostream%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20Returns%20a%20number%20that%20has%20all%20bits%20same%20as%20n%0A%2F%2F%20except%20the%20k\u2019th%20bit%20which%20is%20made%200%0Aint%20turnOffK(int%20n%2C%20int%20k)%0A%7B%0A%20%20%20%20%2F%2F%20k%20must%20be%20greater%20than%200%0A%20%20%20%20if%20(k%20%3C%3D%200)%20return%20n%3B%0A%20%0A%20%20%20%20%2F%2F%20Do%20%26%20of%20n%20with%20a%20number%20with%20all%20set%20bits%20except%0A%20%20%20%20%2F%2F%20the%20k\u2019th%20bit%0A%20%20%20%20return%20(n%20%26%20~(1%20%3C%3C%20(k%20-%201)))%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20int%20n%20%3D%2015%3B%0A%20%20%20%20int%20k%20%3D%204%3B%0A%20%20%20%20cout%20%3C%3C%20turnOffK(n%2C%20k)%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dC++ Programming\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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}]}}