GeeksforGeeks » Interview Questions
Qualcomm Interview Question for Software Engineer/Developer (Fresher) about Bit Magic
(4 posts)-
Bit palindrome:
Write a function to check if the bit-wise representation of an integer is a palindrome.eg. 5, 9, etc. are palindromes.
-
#include<stdio.h>
int main()
{
int num,ntmp,tmp;
scanf("%d",&num);
tmp=0;
ntmp=num;
while(ntmp)
{
tmp=tmp|(ntmp&1);
ntmp=ntmp>>1;
tmp=tmp<<1;
printf("%d %d\n",ntmp,tmp);
}
tmp=tmp>>1;
if(tmp==num)
printf("palindrome\n");
else
printf("not a palindrome\n");
return 0;
} -
create a new number "tmp" which is just reverse of num and check if they are equal.
-
int main() { int n,k,rev; printf("Enter a number "); scanf("%d",&n); rev=0; k=n; while(n) { rev=rev|(n&1); rev<<=1; n>>=1; } rev>>=1; if(k==rev) printf("Palindrome"); else printf("Not palindrome"); return 0; }Let me know if a better approach exists.
Reply
You must log in to post.