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.

class Solution {
public:
    int hammingWeight(uint32_t n) {
    int i,res=0;
    for(i=0;i<32;i++)
    {
        if(n%2==1)
            {res++;}
            //return n;
          n = n>>1;//这里要进行赋值,不然不会改变n的值
        //return n;
   
    }
    return res;
    }
};

最新文章

  1. get传中文参数乱码解决方法
  2. 【python游戏编程之旅】第五篇---嗷大喵爱吃鱼小游戏开发实例
  3. Javascript 笔记与总结(1-6)Javascript 面向对象
  4. 页面设计--TextBox
  5. centos 安装 vsftp
  6. Protel99se教程五:protel99se的自动布线
  7. JavaScript 图片轮播入门
  8. pins-模块内的代码及资源隔离方案
  9. bzoj千题计划316:bzoj3173: [Tjoi2013]最长上升子序列(二分+树状数组)
  10. Legend 图例
  11. 【GMT43智能液晶模块】例程二:串口通信实验
  12. django admim后台不转义提交的html
  13. 日期时间类:Date,Calendar,计算类:Math
  14. 前端开发面试题-JavaScript(转载)
  15. PHP 导出简单文本内容(word txt等)
  16. 常见bootloader介绍
  17. Python OS模块操作文件和目录
  18. 【Thinkphp5】封装layer弹窗方法
  19. 前端PHP入门-009-匿名函数
  20. 我的一些简单的shell脚本实例

热门文章

  1. SNMP的应用
  2. Spark Streaming之二:StreamingContext解析
  3. vim vi Ubuntu 设置
  4. Yet Another Number Sequence
  5. 1.13-1.14 Hive Action
  6. 3.19 YARN HA架构及(RM/NM) Restart讲解
  7. 爬虫代码实现六-Queue队列实现循环抓取
  8. C#基础:通过委托给任何对象数组进行排序
  9. 用递归方式在JSON中查找对象
  10. [Xcode 实际操作]八、网络与多线程-(3)使用UIApplication对象拨打电话