Number of 1 BitsWrite a function that takes an unsigned integer and returns the number of ’1' bits i
2024-10-21 08:26:57
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;
}
};
最新文章
- get传中文参数乱码解决方法
- 【python游戏编程之旅】第五篇---嗷大喵爱吃鱼小游戏开发实例
- Javascript 笔记与总结(1-6)Javascript 面向对象
- 页面设计--TextBox
- centos 安装 vsftp
- Protel99se教程五:protel99se的自动布线
- JavaScript 图片轮播入门
- pins-模块内的代码及资源隔离方案
- bzoj千题计划316:bzoj3173: [Tjoi2013]最长上升子序列(二分+树状数组)
- Legend 图例
- 【GMT43智能液晶模块】例程二:串口通信实验
- django admim后台不转义提交的html
- 日期时间类:Date,Calendar,计算类:Math
- 前端开发面试题-JavaScript(转载)
- PHP 导出简单文本内容(word txt等)
- 常见bootloader介绍
- Python OS模块操作文件和目录
- 【Thinkphp5】封装layer弹窗方法
- 前端PHP入门-009-匿名函数
- 我的一些简单的shell脚本实例
热门文章
- SNMP的应用
- Spark Streaming之二:StreamingContext解析
- vim vi Ubuntu 设置
- Yet Another Number Sequence
- 1.13-1.14 Hive Action
- 3.19 YARN HA架构及(RM/NM) Restart讲解
- 爬虫代码实现六-Queue队列实现循环抓取
- C#基础:通过委托给任何对象数组进行排序
- 用递归方式在JSON中查找对象
- [Xcode 实际操作]八、网络与多线程-(3)使用UIApplication对象拨打电话