如何通过printf的打印二进制数 [英] how to print binary number via printf
本文介绍了如何通过printf的打印二进制数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format\">Is还有一个printf转换器的二进制格式打印?
下面是我的程序
#include<stdio.h>
int main ()
{
int i,a=2;
i=~a;
printf("a=%d\ni=%d\n",a,i);
return 0;
}
的输出是
a=2
i=-3
我想这二进制打印,还有为%x,%0,%d个这是十六进制,八进制,十进制数字,但究竟是什么在printf的打印二进制?
I want this to print in binary,there are %x,%o,%d which are for hexadecimal,octal,decimal number but what is for printing binary in printf?
推荐答案
的printf()不直接支持,相反,你必须使自己的功能。
printf() doesn't directly support that, instead you have to make your own function.
是这样的:
while (n) {
if (n & 1)
printf("1");
else
printf("0");
n >>= 1;
}
printf("\n");
这篇关于如何通过printf的打印二进制数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文