#include<stdio.h>
void printBinary( int x)
{
int i;
int j =0;
int a[32]={0};
// if x is an integer
if(x>0)
{
while(x)
{
a[j++] = x % 2;
x = x / 2 ;
}
}
// else if x is a negative integer
else
{
unsigned int X = (unsigned int)x;
while(X)
{
a[j++] = X % 2;
X /= 2 ;
}
}
for(i=31; i>=0; i--)
{
printf("%d", a[i]);
if(i % 8 == 0 && i != 0)
printf(",");
}
puts("");
}
void main()
{
printBinary(-15);
}
本文介绍了一段使用C语言实现的函数,用于将整数转换为二进制并打印出来,支持正负整数。


被折叠的 条评论
为什么被折叠?



