Sinjiufai 2021-05-09 11:34 采纳率: 33.3%
浏览 32
已采纳

c语言计算二进制有多少个1时 我想到移位操作符 但为什么运行结果与我想的不同

两个的都是用了移位操作符 为什么结果不一样 难道a在&的时候没有变成2进制计算???

 

  • 写回答

3条回答 默认 最新

  • benbenli 2021-05-09 12:13
    关注

    第一各图是对的。第二各图里,你把if (a & (1 << i) == 1) 改为if (a & (1 << i) ) 就对了。因为每次移位后1 << i不是1,而是2的i次方。

    下面代码演示给你看计算过程:

    
    #include <stdio.h>
    
    int main()
    {
        int count =0;
        int a = 13;
        for (int i = 0; i < 32; ++i)
        {
            printf("i=%d, 1 << i = %d, a & (1 << i) = %d\n", i, 1 << i, a & (1 << i));
            if (a & (1 << i) == 1)
                ++count;
        }
        
        printf("%d\n", count);
        return 0;
    }
    
    //Output
    i=0, 1 << i = 1, a & (1 << i) = 1                                                                    
    i=1, 1 << i = 2, a & (1 << i) = 0                                                                    
    i=2, 1 << i = 4, a & (1 << i) = 4                                                                    
    i=3, 1 << i = 8, a & (1 << i) = 8                                                                    
    i=4, 1 << i = 16, a & (1 << i) = 0                                                                   
    i=5, 1 << i = 32, a & (1 << i) = 0                                                                   
    i=6, 1 << i = 64, a & (1 << i) = 0                                                                   
    i=7, 1 << i = 128, a & (1 << i) = 0                                                                  
    i=8, 1 << i = 256, a & (1 << i) = 0                                                                  
    i=9, 1 << i = 512, a & (1 << i) = 0                                                                  
    i=10, 1 << i = 1024, a & (1 << i) = 0                                                                
    i=11, 1 << i = 2048, a & (1 << i) = 0                                                                
    i=12, 1 << i = 4096, a & (1 << i) = 0                                                                
    i=13, 1 << i = 8192, a & (1 << i) = 0                                                                
    i=14, 1 << i = 16384, a & (1 << i) = 0                                                               
    i=15, 1 << i = 32768, a & (1 << i) = 0                                                               
    i=16, 1 << i = 65536, a & (1 << i) = 0                                                               
    i=17, 1 << i = 131072, a & (1 << i) = 0                                                              
    i=18, 1 << i = 262144, a & (1 << i) = 0                                                              
    i=19, 1 << i = 524288, a & (1 << i) = 0                                                              
    i=20, 1 << i = 1048576, a & (1 << i) = 0                                                             
    i=21, 1 << i = 2097152, a & (1 << i) = 0                                                             
    i=22, 1 << i = 4194304, a & (1 << i) = 0                                                             
    i=23, 1 << i = 8388608, a & (1 << i) = 0                                                             
    i=24, 1 << i = 16777216, a & (1 << i) = 0                                                            
    i=25, 1 << i = 33554432, a & (1 << i) = 0                                                            
    i=26, 1 << i = 67108864, a & (1 << i) = 0                                                            
    i=27, 1 << i = 134217728, a & (1 << i) = 0                                                           
    i=28, 1 << i = 268435456, a & (1 << i) = 0                                                           
    i=29, 1 << i = 536870912, a & (1 << i) = 0                                                           
    i=30, 1 << i = 1073741824, a & (1 << i) = 0                                                          
    i=31, 1 << i = -2147483648, a & (1 << i) = 0                                                         
    3          
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注