Check if binary representation of a number is palindrome


#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
bool check(int n)
{
int x=0;
int m=0;
f
for(m=0;!((pow(2,m)<=n&&n<pow(2,m+1)));m++);
m++;
cout<<"m="<<m<<endl;
for(int i=m,j=1;i>0;i--,j++)
{
if(!(n & 1<<i-1)==0 )
{
x=x^(1<<j-1);
}
}
cout<<"x="<<x;
if(n==x) return true;
else return false;
}
int main()
{
int n;
cin>>n;
if(check(n))
{
cout<<"yes";
}
else
{
cout<<"no";
}

Comments

Popular posts from this blog