Binary representation palindrome or not

Suggest an optimized algorithm to check if binary representation of a number is palindrome or not.
eg. 5 = 101 so it is palindrome.

Comments

  1. int 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";

    ReplyDelete
  2. @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

Post a Comment

Popular posts from this blog