Binary representation palindrome or not Get link Facebook X Pinterest Email Other Apps By Admin - January 12, 2011 Suggest an optimized algorithm to check if binary representation of a number is palindrome or not. eg. 5 = 101 so it is palindrome. Get link Facebook X Pinterest Email Other Apps Comments JTJanuary 13, 2011 at 8:29 PMint l=(int)(log(n)/log2)+1;int i=0,j=l;while(i < j){if(( n & (1 << i)) == ( n & (1 << j))){i++;j--;}else{break;}}if(i >= j)cout << "no. is a palindrome";else cout << "no. is not a palindrome";ReplyDeleteRepliesReplyAdminMay 26, 2011 at 9:45 AM@jainendra according to your understanding do you consider 1010 as palindrome or not.If yes then your program doesn't check for even length number.ReplyDeleteRepliesReplyAdd commentLoad more... Post a Comment
int l=(int)(log(n)/log2)+1;
ReplyDeleteint i=0,j=l;
while(i < j){
if(( n & (1 << i)) == ( n & (1 << j))){
i++;
j--;
}
else{
break;
}
}
if(i >= j)cout << "no. is a palindrome";
else cout << "no. is not a palindrome";
@jainendra according to your understanding do you consider 1010 as palindrome or not.If yes then your program doesn't check for even length number.
ReplyDelete