GeeksforGeeks » Interview Questions

Qualcomm Interview Question for Software Engineer/Developer (Fresher) about Bit Magic

(4 posts)
  • Started 4 months ago by rahul
  • Latest reply from Guddu Sharma

Tags:

  1. rahul
    guest
    Posted 4 months ago #

    Bit palindrome:
    Write a function to check if the bit-wise representation of an integer is a palindrome.

    eg. 5, 9, etc. are palindromes.

  2. jitendra
    guest
    Posted 4 months ago #

    #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;
    }

  3. jitendra
    guest
    Posted 4 months ago #

    create a new number "tmp" which is just reverse of num and check if they are equal.

  4. Guddu Sharma
    Member
    Posted 1 month ago #

    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.

RSS feed for this topic