c语言中将一个十进制数按照二进制输出
1、
#include <stdio.h> int main(void) { int bits = 0; unsigned tmp = ~0U; while(tmp) { if(tmp & 1U) bits++; tmp >>= 1; } int i; unsigned x; puts("please input a decimal number."); printf("x = "); scanf("%u", &x); for(i = bits - 1; i >= 0; i--) { putchar(x >> i & 1U ? '1' : '0'); } putchar('\n'); return 0; }