【急求】c语言程序输入一个整数(int),要求输出其二进制形式的值。

(其各位为:第15 14 13 ... 2 1 0 位)
例如输入:
5
输出:
0000000000000101
输入:
-1
输出:
1111111111111111

第1个回答  2009-10-27
我也发一个自编的,已验证通过。

#include <stdio.h>

main()
{
char binOut[17];
short int i, j; /* 16bit的整数,要用short int型 */
scanf("%d", &i);

for(j=15;j>=0;j--)
{
if(i&(1<<j))
binOut[15-j] = '1';
else
binOut[15-j] = '0';
}
binOut[16] = 0;
printf("DEC(%d)=BIN(%s)\n",i,binOut);
}本回答被提问者采纳
第2个回答  2009-10-27
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int n;
char a[50];
int main(void)
{
scanf("%d",&n);
itoa(n,a,2);
puts(a);
return 0;
}