I was practicing basic programs of C and tried to do a binary to decimal conversion.
Below is the code I tried but it did not produce correct results. I couldn't understand where my logic goes wrong.
/* binary to decimal */
#include<stdio.h>
int main()
{
int a,i,p,sum=0;
printf("enter binary number\n");
scanf("%u",&a);
for(i = 0 ; i < 5; i++) /* Taking only 5 digits in binary */
{
if((a & 1<<i))
{
p = 1<<i;
sum = sum +p;
}
}
printf("%d\n",sum);
return 0;
}
I entered 1001 and was expecting 9 to be the output, but output was 1001?