题目:
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).
For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011
, so the function should return 3.
int res=0;
/*while(n){
n&=n-1;
res++;
}*/
for(;n!=0;n&=n-1)
++res;
以上是同一种方法,但是运行时间相差4ms,for循环为4ms,while循环为8ms, 难道for循环更快????以后要注意尝试一下。。
//另一种方法统计二进制中1的个数,本质一样
int res=0;
for(;x|=0;x>>=1)
if(x&01)
res++;