转 -- 求一个二进制数值中的1的个数

求下面函数的返回值(微软)

int func(x) 

    int countx = 0; 
    while(x) 
    { 
          countx ++; 
          x = x&(x-1); 
     } 
    return countx; 

假定x = 9999。 答案:8

思路:将x转化为2进制,看含有的1的个数。

原文地址:https://www.cnblogs.com/chenchenluo/p/5166784.html