Chinaunix首页 | 论坛 | 博客

AAA

  • 博客访问: 18389
  • 博文数量: 33
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 330
  • 用 户 组: 普通用户
  • 注册时间: 2015-04-17 08:23
个人简介

好记性不如烂笔头,记录学习历程和随想云云,欢迎各位大虾拍砖

文章分类

全部博文(33)

文章存档

2015年(33)

我的朋友
最近访客

分类: C/C++

2015-05-07 23:21:53

【问题描述】

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.     int count;
  3.     
  4.     for(count = 0; n != 0; n >>= 1)
  5.     {
  6.         if( n & 1 != 0)
  7.             count++;
  8.     }
  9.     
  10.     return count;
  11. }

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