{"id":26781,"date":"2017-12-24T15:42:07","date_gmt":"2017-12-24T10:12:07","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26781"},"modified":"2017-12-24T15:43:09","modified_gmt":"2017-12-24T10:13:09","slug":"check-binary-representation-number-palindrome","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/check-binary-representation-number-palindrome\/","title":{"rendered":"C++ Program Check if binary representation of a number is palindrome"},"content":{"rendered":"<p>Given an integer \u2018x\u2019, write a C function that returns true if binary representation of x is palindrome else return false.<\/p>\n<p>For example a numbers with binary representation as 10..01 is palindrome and number with binary representation as 10..00 is not palindrome.<\/p>\n<p>The idea is similar to checking a string is palindrome or not. We start from leftmost and rightmost bits and compare bits one by one. If we find a mismatch, then return false.<\/p>\n<p><strong>We strongly recommend that you click here and practice it, before moving on to the solution.<\/strong><\/p>\n[ad type=\u201dbanner\u201d]\n<p><strong>Algorithm:<\/strong><br \/>\nisPalindrome(x)<br \/>\n1) Find number of bits in x using sizeof() operator.<br \/>\n2) Initialize left and right positions as 1 and n respectively.<br \/>\n3) Do following while left \u2018l\u2019 is smaller than right \u2018r\u2019.<br \/>\n..\u2026..a) If bit at position \u2018l\u2019 is not same as bit at position \u2018r\u2019, then return false.<br \/>\n..\u2026..b) Increment \u2018l\u2019 and decrement \u2018r\u2019, i.e., do l++ and r\u2013-.<br \/>\n4) If we reach here, it means we didn\u2019t find a mismatching bit.<\/p>\n<p>To find the bit at a given position, we can use the idea similar to this post. The expression \u201c<strong>x & (1 << (k-1))<\/strong>\u201d gives us non-zero value if bit at k\u2019th position from right is set and gives a zero value if if k\u2019th bit is not set.<\/p>\n<p>Following is C++ implementation of the above algorithm.<\/p>\n[pastacode lang=\u201dcpp\u201d manual=\u201d%23include%3Ciostream%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20This%20function%20returns%20true%20if%20k\u2019th%20bit%20in%20x%20is%20set%20(or%201).%0A%2F%2F%20For%20example%20if%20x%20(0010)%20is%202%20and%20k%20is%202%2C%20then%20it%20returns%20true%0Abool%20isKthBitSet(unsigned%20int%20x%2C%20unsigned%20int%20k)%0A%7B%0A%20%20%20%20return%20(x%20%26%20(1%20%3C%3C%20(k-1)))%3F%20true%3A%20false%3B%0A%7D%0A%20%0A%2F%2F%20This%20function%20returns%20true%20if%20binary%20representation%20of%20x%20is%0A%2F%2F%20palindrome.%20For%20example%20(1000\u2026001)%20is%20paldindrome%0Abool%20isPalindrome(unsigned%20int%20x)%0A%7B%0A%20%20%20%20int%20l%20%3D%201%3B%20%2F%2F%20Initialize%20left%20position%0A%20%20%20%20int%20r%20%3D%20sizeof(unsigned%20int)*8%3B%20%2F%2F%20initialize%20right%20position%0A%20%0A%20%20%20%20%2F%2F%20One%20by%20one%20compare%20bits%0A%20%20%20%20while%20(l%20%3C%20r)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(isKthBitSet(x%2C%20l)%20!%3D%20isKthBitSet(x%2C%20r))%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20false%3B%0A%20%20%20%20%20%20%20%20l%2B%2B%3B%20%20%20%20%20r\u2013%3B%0A%20%20%20%20%7D%0A%20%20%20%20return%20true%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20unsigned%20int%20x%20%3D%201%3C%3C15%20%2B%201%3C%3C16%3B%0A%20%20%20%20cout%20%3C%3C%20isPalindrome(x)%20%3C%3C%20endl%3B%0A%20%20%20%20x%20%3D%201%3C%3C31%20%2B%201%3B%0A%20%20%20%20cout%20%3C%3C%20isPalindrome(x)%20%3C%3C%20endl%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<p><strong>Output:<\/strong><\/p>\n<pre>1\r\n1<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Check if binary representation of a number is palindrome &#8211; Bit Algorithm &#8211; Given an integer \u2018x\u2019, write a C function that returns true if binary .<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[74852,1],"tags":[80030,80028,80032,80033,80025,80026,80024,80029,80031,72024,80037,80034,80036,80035,80027],"class_list":["post-26781","post","type-post","status-publish","format-standard","hentry","category-bit-algorithms","category-coding","tag-binary-palindrome-c","tag-binary-palindrome-hackerearth","tag-binary-palindrome-java","tag-bitwise-palindrome-8086-explanation","tag-check-if-a-binary-number-is-a-palindrome-java","tag-find-nth-binary-palindrome","tag-list-of-binary-palindromes","tag-nth-binary-palindrome-in-java","tag-nth-binary-palindrome-number","tag-palindrome-names","tag-palindrome-numbers","tag-palindrome-sentence","tag-string-is-palindrome-or-not-in-c","tag-string-is-palindrome-or-not-in-java","tag-what-is-bitwise-palindrome"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26781","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=26781"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26781\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}