Introduction To Programming

Why programs ? Programming is nothing but making the computers think the same way a human thinks. Programs are made for obtaining a specific solution which can even be solved by humans which when solved by us may consume time..

Friday, March 26, 2010

Display bits of signed numbers

int main()
{
int a=-3,i=0,j;
int b[32];
clrscr();
printf("%d\n",a);
for(j=0;j<32;j++)
b[j]=0;
i=0;
while(a!=0)
{
if(a&01)
{
b[i]=1;
}
else
{
b[i]=0;
}
i++;
a>>=1;
}
for(j=31;j>=0;j--)
{
printf("%d",b[j]);
}
getch();
return 0;
}

Binary to Decimal

int main()
{
int a[8],i,j=1,b=0;
clrscr();
for(i=0;i<8;i++)
scanf("%d",&a[i]);
for(i=7;i>=0;i--)
{
b+=(a[i]*j);
j=j*2;

}
printf("%d",b);
getch();
return 0;
}