<分区>
Possible Duplicate:
Best algorithm to count the number of set bits in a 32-bit integer?
我想找出一个数字的二进制表示中有多少个 1。我有 2 个逻辑。
-
int count =0; int no = 4; while(no!=0){ int d = no%2; if(d==1) count++; no = no/2; str = str+ d; }
现在第二个逻辑是继续用 1,2,4,8,32 迭代屏蔽数字并检查结果是否为 1,2,4,8.....我没有得到应该结束的结果此循环的条件。