Chinaunix首页 | 论坛 | 博客
  • 博客访问: 358233
  • 博文数量: 60
  • 博客积分: 15
  • 博客等级: 民兵
  • 技术积分: 1138
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-20 16:18
个人简介

最多140个字

文章分类

全部博文(60)

文章存档

2016年(1)

2015年(34)

2014年(25)

分类: C/C++

2015-03-17 19:05:41

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. }


阅读(1871) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~