/*编程实现将一个十进制数转换为二进制输出(请考虑尽量多的方法)*/
#include <stdio.h>
int main (int argc, char *argv[])
{
int n, i;
scanf("%d",&n);
char bits[33] = {0};
for(i = 31; i >= 0; i--){
bits[i] = (n & 1) + ‘0‘;
n >>= 1;
}
bits[32] = ‘\0‘;
puts(bits);
return 0;
}
#include<stdio.h>
void main()
{
int a = -1;
int b[32] = {};
int i=0;
for(i=0;i<32;i++)
b[i] = (a&(0x01<<i))?1:0;
for(i=31;i>=0;i--)
printf("%d",b[i]);
printf("\n");
}
#include<stdio.h>
void main()
{
int temp = -1;
unsigned int a;
int b[32] = {};
int i=0;
//if(temp<0)
a=temp;
while(a!=0){
b[i] = (a%2)?1:0;
a = a/2;
i++;
}
for(i=31;i>=0;i--)
printf("%d",b[i]);
printf("\n");
}
将一个十进制数转换为二进制输出