{"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=&#8221;banner&#8221;]\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 &amp; (1 &lt;&lt; (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<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\/>\/\/ This function returns true if k&#039;th bit in x is set (or 1).<br\/>\/\/ For example if x (0010) is 2 and k is 2, then it returns true<br\/>bool isKthBitSet(unsigned int x, unsigned int k)<br\/>{<br\/>    return (x &amp; (1 &lt;&lt; (k-1)))? true: false;<br\/>}<br\/> <br\/>\/\/ This function returns true if binary representation of x is<br\/>\/\/ palindrome. For example (1000...001) is paldindrome<br\/>bool isPalindrome(unsigned int x)<br\/>{<br\/>    int l = 1; \/\/ Initialize left position<br\/>    int r = sizeof(unsigned int)*8; \/\/ initialize right position<br\/> <br\/>    \/\/ One by one compare bits<br\/>    while (l &lt; r)<br\/>    {<br\/>        if (isKthBitSet(x, l) != isKthBitSet(x, r))<br\/>            return false;<br\/>        l++;     r--;<br\/>    }<br\/>    return true;<br\/>}<br\/> <br\/>\/\/ Driver program to test above function<br\/>int main()<br\/>{<br\/>    unsigned int x = 1&lt;&lt;15 + 1&lt;&lt;16;<br\/>    cout &lt;&lt; isPalindrome(x) &lt;&lt; endl;<br\/>    x = 1&lt;&lt;31 + 1;<br\/>    cout &lt;&lt; isPalindrome(x) &lt;&lt; endl;<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\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}]}}