Siemens Interview Question Software Engineer in Tests
0of 0 votesWrite a function to determine whether the binary representation of a specified 32-bit integer is a palindrome. For example, the 32-bit integer 0xFCA8153F is a palindrome, but 0xFCA88ACF is not. Show how you would test this function.
This can be done by checking the starting nibbles and end nibbles and the sum of both should be 15(F). This should be true for all the nibbles till you cover all the nibbles.
#define IS_PALINDROME (num) !(num ^ ((num & (1 << 0 ) ) << 31 \
| (num & (1 << 1 ) ) << 29 \
| (num & (1 << 2 ) ) << 27 \
| (num & (1 << 3 ) ) << 25 \
| (num & (1 << 4 ) ) << 23 \
| (num & (1 << 5 ) ) << 21 \
| (num & (1 << 6 ) ) << 19 \
| (num & (1 << 7 ) ) << 17 \
| (num & (1 << 8 ) ) << 15 \
| (num & (1 << 9 ) ) << 13 \
| (num & (1 << 10 ) ) << 11 \
| (num & (1 << 11 ) ) << 09 \
| (num & (1 << 12 ) ) << 07 \
| (num & (1 << 13 ) ) << 05 \
| (num & (1 << 14 ) ) << 03 \
| (num & (1 << 15 ) ) << 01 \
| (num & (1 << 16 ) ) >> 1 \
| (num & (1 << 17 ) ) >> 3 \
| (num & (1 << 18 ) ) >> 5 \
| (num & (1 << 19 ) ) >> 7 \
| (num & (1 << 20 ) ) >> 9 \
| (num & (1 << 21 ) ) >> 11 \
| (num & (1 << 22 ) ) >> 13 \
| (num & (1 << 23 ) ) >> 15 \
| (num & (1 << 24 ) ) >> 17 \
| (num & (1 << 25 ) ) >> 19 \
| (num & (1 << 26 ) ) >> 21 \
| (num & (1 << 27 ) ) >> 23 \
| (num & (1 << 28 ) ) >> 25 \
| (num & (1 << 29 ) ) >> 27 \
| (num & (1 << 30 ) ) >> 29 \
| (num & (1 << 31 ) ) >> 31))
int reverse(unsigned int num)
{
unsigned int rev=0;
int s = sizeof(num) * 8;
while(num)
{
rev <<= 1;
rev |= num & 1;
num>>=1;
--s;
}
rev <<= s;
return rev;
}
void checkPalindrome(unsigned int number)
{
if(reverse(number)==number)
printf("Palindrome\n");
else
printf("Not Palindrome\n");
}
void palindrome_32bit_no()
{
unsigned long int x=0xFCA88ACF,A,B;
int i=0,flag=0;
for(i=0;i<16;i++)
{
A=(x & (unsigned long)pow(2.0, 32-i-1) );
B=(x & (unsigned long)pow(2.0, i) );
if((A && B)|| (!A && !B))
flag=1;
else
{
flag=0;
cout<<"\nThe no is not palindrome";
break;
}
}
if(flag==1)
cout<<"\nPalindrome";
}

You could do this bit by bit or nibble by nibble...or even byte by byte (look up table)
- EveryoneLovesMicrosoft on November 03, 2009 Edit | Flag Reply