Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if the binary representation of an integer is a palindrome?

How to check if the binary representation of an integer is a palindrome?

like image 310
yesraaj Avatar asked May 10 '09 18:05

yesraaj


1 Answers

Hopefully correct:

_Bool is_palindrome(unsigned n)
{
    unsigned m = 0;

    for(unsigned tmp = n; tmp; tmp >>= 1)
        m = (m << 1) | (tmp & 1);

    return m == n;
}
like image 120
Christoph Avatar answered Sep 22 '22 08:09

Christoph