implement isPalindrome(int n)

1,441.7K Views
Implement the function boolean isPalindrome (int n);

Which will return true if the bit-wise representation of the integer 
is a palindrome and false otherwise.
Share
Mehar Starter Asked on 31st July 2015 in Amazon Interview Puzzles.
Add Comment

  • 1 Answer(s)
    bool isNthBitSet(unsigned int x, unsigned int n)
    {
        return (x & (1 << (n-1)))? true: false;
    }
    bool isPalindrome(unsigned int x)
    {
        int l = 1; // Initialize left position
        int r = sizeof(unsigned int)*8; // initialize right position
        // One by one compare bits
        while (l < r)
        {
            if (isNthBitSet(x, l) != isNthBitSet(x, r))
                return false;
            l++;
    r--;
        }
        return true;
    }
    Pranav Jain Scholar Answered on 27th April 2016.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.
  • More puzzles to try-