N
NM
I am given an int and have to tell whether the bit representation of that
int is a palindrome or not.
Here is what I have come up with
bool isIntPalindrome(int num)
{
unsigned int temp = num;
int reversed = 0;
while (num != 0) {
reveresed <<= 1;
reversed |= (temp & 1);
temp >>= 1;
}
return reversed == num;
}
My question is this doing what it is supposed to do? In particular I want to
know if the signed to unsigned
conversion is changing the bit pattern or not? It works on my system, but
don't know if it will work on
every machine.
Thanks
NM
int is a palindrome or not.
Here is what I have come up with
bool isIntPalindrome(int num)
{
unsigned int temp = num;
int reversed = 0;
while (num != 0) {
reveresed <<= 1;
reversed |= (temp & 1);
temp >>= 1;
}
return reversed == num;
}
My question is this doing what it is supposed to do? In particular I want to
know if the signed to unsigned
conversion is changing the bit pattern or not? It works on my system, but
don't know if it will work on
every machine.
Thanks
NM