71. To reverse binary of a number (Method 1)
#include<stdio.h>
void
main()
{
int num,i,j,n1,n2;
printf("To reverse binary of a
number \n");
printf("Enter a number : ");
scanf("%d",&num);
for(i=31;i>=0;i--)
printf("%d",num>>i&1);
printf("\n");
for(i=0,j=31;i<j;i++,j--)
{
n1=num>>i&1;
n2=num>>j&1;
if(n1!=n2)
{
num=num^(1<<i);
num=num^(1<<j);
}
}
for(i=31;i>=0;i--)
printf("%d",num>>i&1);
printf("\n");
}
Comments
Post a Comment