• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
美丽的时光机器
博客园    首页    新随笔    联系   管理    订阅  订阅
将一个十进制整数转换为二进制并输出
 1  #include <stdio.h>
 2  #include <string.h>
 3  
 4  int main(int argc, char *argv[])
 5  {
 6          char bin[8 * sizeof(int)];
 7          int n = 0;
 8          int index;
 9  
10          scanf("%d", &n);
11  
12          for(index = (8 * sizeof(int) - 1); index >= 0; index--)
13          {
14                  bin[index] = (n & 1) + '0';
15                  n >>= 1;
16          }
17 
18         bin[8 * sizeof(int)] = '\0';
19          int i = 0;
20 
21         for(index = 0; index <= (8 * sizeof(int) - 1); index++)
22         {
23             ++i;
24             putchar(bin[index]);
25 
26             if(i % 4 == 0)
27             {
28                 putchar(' ');
29             }
30         }
31            putchar('\n');
32   
33            return 0;
34 }

 

点滴珍贵,重在积累,愿时光慢一点,再慢一点。
posted on 2018-01-29 17:20  美丽的时光机器  阅读(577)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3