# Why am I getting a wrong answer?

• I tried solving this question by reversing the number by using bitwise operation
say for example if the number' binary representation is 00000110 ,what this does is, send 0000011 into the code and the left shift reverse_num 5 times to get 01100000 which obviously is reverse of original,after that I am doing an "&" with this number to original number.if answer is 1 then number is palindrome.Is there some flaw in my logic? If there is ,I think its's in the & part.

``````class Solution {
public:
bool isPalindrome(int x) {
if(x<0)
return false;
int num=x;
int reverse_num=x;
int count=sizeof(x);
num >>= 1;
while(num)
{
reverse_num <<= 1;
reverse_num |= num & 1;
num >>= 1;
count--;
}
reverse_num <<= count;

if(!(reverse_num&&x))
return true;
else return false;
}
};``````

• A palindrome number in decimal is not a palindrome in binary.
E.g. 0110,0000(96) is not reverse of 0000,0110(3)

• Thinking of overflow possibility , reversing would not be a good idea .
My code is that convert the digits from begin with the end one ,just like two pointer and it stops when they two meet in the middle . I hope this would help.

`````` bool isPalindrome(int x) {
int tmp=x;
int index=0;
if(x<0)
return false;
while(tmp>0)     //count how many digits the num have
{
tmp/=10;
index++;
}
int tmp_last=x;
int tmp_first=x;
int times=index/2;
while(times>0)   // compare the digit from the begin with the end . And stop when these two meet
{
int first_digit=tmp_first/(int)pow(10,index-1);
tmp_first=tmp_first%((int)pow(10,index-1));
int last_digit=tmp_last%10;
tmp_last/=10;
if(first_digit!=last_digit)
return false;
index--;
times--;
}
return true;
}``````

• This post is deleted!

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.