Sunday, February 14, 2010

Showing Binary Equivalent of an Integer

main()
{
int n;
char ch;
while(1)
{
clrscr();
printf("
Enter an integer to see its Binary Equivalent");
printf("

Enter ");
scanf("%d",&n);
showbits(n);

printf("
continue ? ");
ch=getch();
if(ch==27)
break;
}
}

showbits(int n)
{
int i,bit,mask,count=0;
printf("
");
for(i=15;i>=0;i--)
{
mask=1<

bit=n & mask;

if(bit==mask)
printf("1");
else
printf("0");
count++;
if(count%4==0)
printf(" ");
}
}

No comments: