Number of 1 Bits

1940阅读 0评论2015-03-17 qinchaowhut
分类:C/C++

Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the ).

For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3.


  1. int hammingWeight(uint32_t n) {
  2.     if(n==0)
  3.         return 0;
  4.     return (n&0x01==0x01)? hammingWeight(n>>1)+1:hammingWeight(n>>1); 
  5. }


上一篇:2013年个人微博推荐技术资料汇总
下一篇:链表快排