笔试题2. 求一个数的二进制中1的个数

来源:互联网 发布:javascript 书 知乎 编辑:程序博客网 时间:2024/05/16 07:33

例如 : 整数25   <  0000 0000 0000 0000 0000 0000 00011001>   1 的个数为 3



#include <stdio.h>int GetBinaryNum(int value){int count = 0;while (value){value &= (value - 1);count++;}return count;}int main(){printf("%d\n",GetBinaryNum(25));return 0;}


0 0
原创粉丝点击