面试题58:求下面函数的返回值


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

    假定x=9999。

    答:返回值为8。

    技巧将x转化为二进制,看含有的1的个数。