Check if binary representation of a number is palindrome
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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
Post a Comment