如何在C中以二进制格式打印十进制数?

2020-09-08 09:50:36 浏览数 (1)

回答:

代码语言:javascript复制
#define CHAR_BITS  8  // size of character

#define INT_BITS  ( sizeof(int) * CHAR_BITS) //bits in integer

void PrintInBinary(unsigned n)

{

char Pos = (INT_BITS -1);

for (; Pos >= 0 ; --Pos)

{

  (n & (1 << Pos))? printf("1"): printf("0");

}

}

0 人点赞