{"id":26441,"date":"2017-12-20T21:05:29","date_gmt":"2017-12-20T15:35:29","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26441"},"modified":"2017-12-20T21:05:29","modified_gmt":"2017-12-20T15:35:29","slug":"position-rightmost-set-bit","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/position-rightmost-set-bit\/","title":{"rendered":"Position of rightmost set bit"},"content":{"rendered":"<p>Write a one line C function to return position of first 1 from right to left, in binary representation of an Integer.<\/p>\n<pre>I\/P    18,   Binary Representation 010010\r\nO\/P   2\r\nI\/P    19,   Binary Representation 010011\r\nO\/P   1\r\n<\/pre>\n<p>Let I\/P be 12 (1100)<\/p>\n<pre><strong>Algorithm: <\/strong>(Example 18(010010))\r\n\r\n1. Take two's complement of the given no as all bits are reverted\r\nexcept the first '1' from right to left (10111)\r\n\r\n2  Do an bit-wise & with original no, this will return no with the\r\nrequired one only (00010)\r\n\r\n3  Take the log2 of the no, you will get position -1 (1)\r\n\r\n4  Add 1 (2)<\/pre>\n[ad type=\u201dbanner\u201d]\n<p><strong>Program:<\/strong><\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%23include%3Cstdio.h%3E%0A%23include%3Cmath.h%3E%0A%20%0Aunsigned%20int%20getFirstSetBitPos(int%20n)%0A%7B%0A%20%20%20return%20log2(n%26-n)%2B1%3B%0A%7D%0A%20%0Aint%20main()%0A%7B%0A%20%20%20%20int%20n%20%3D%2012%3B%0A%20%20%20%20printf(%22%25u%22%2C%20getFirstSetBitPos(n))%3B%0A%20%20%20%20getchar()%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dC Programming\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>Write a one line C function to return position of first 1 from right to left, in binary representation of an Integer.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[74852],"tags":[78762,75277,78763,75107,75266,75880,75082,75115,75102,75114,78761,75099,77970,75110,75889,78758,75085,78759,75289,75083,75079,75088,75269,75263,75879,75112,75111,75109,75884,75078,75084,78760,75994,75270,75892,78757,75098,75886,75106,76387,75281,75089,75096,75992,75104,75291,75293,75100,75118,75086],"class_list":["post-26441","post","type-post","status-publish","format-standard","hentry","category-bit-algorithms","tag-2-power-32","tag-2-power-32-value","tag-2-to-the-power-32","tag-32-bit-word","tag-binary-operation","tag-bit","tag-bit-bit-bit","tag-bit-manipulation","tag-bit-number","tag-bit-reversal","tag-bit-set-test","tag-bit-table","tag-bit-test","tag-bitcount","tag-bits-power","tag-bitscan","tag-bitset","tag-bitsets","tag-bittricks","tag-bitwise-operations-in-c","tag-bitwise-operator-in-c","tag-bitwise-operator-in-c-example-programs","tag-bitwise-operators","tag-bitwise-operators-in-c","tag-bs-it","tag-c-bit","tag-c-byte","tag-c-xor","tag-chacker","tag-clearbit","tag-counting-bits","tag-ffs3","tag-find-1","tag-first-in-math-hack","tag-first-set-bit","tag-fitbit","tag-for-bit","tag-html-graphics","tag-java-binary-operators","tag-low-bit","tag-magic-bit","tag-nextbit","tag-python-integer-to-binary","tag-set-a-bit","tag-set-bit","tag-twiddle","tag-what-is-bitwise-operator","tag-word-bit","tag-xor-operation-in-c","tag-xor-operator-in-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26441","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=26441"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26441\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}