#include <stdio.h>
#include <string.h>
#define SIZE 8 * sizeof(int)
void print(char *str)
{
int i = 0;
while(str[i] != '\0'){
putchar(str[i]);
if(++i % 4 ==0 && str[i])
putchar(' ');
}
puts("");
}
void itob(int n, char *str)
{
int i;
for(i = SIZE - 1; i >= 0; i --, n >>= 1 )
str[i] = (n & 1) + '0';
str[SIZE] = '\0';
print(str);
}
int main(int argc, char **argv)
{
int num;
char str[SIZE];
printf("Please input the num:");
while(scanf("%d", &num) == 1){
itob(num, str);
printf("Please input the num:");
}
return 0;
}
利用位操作移位实现打印二进制
最新推荐文章于 2024-10-15 16:59:13 发布