191. Number of 1 Bits (Int; Bit)
2015-12-16 12:37
162 查看
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.
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 ret = 0; for (unsigned int i = 1; i != 0; i <<= 1){ if(n & 1) ret++; n >>= 1; } return ret; } };
相关文章推荐
- Hibernate关联关系映射之一对一(主键关联)
- Spring下载地址
- 贪心算法应用之一:装箱问题
- 职称中的“副高”与“正高”
- FZU Problem 1686 神龙的难题 重复覆盖
- Android编程之ICS式下拉菜单PopupWindow实现方法详解(附源码下载)
- OC-050.常用的结构体
- 使用 hadoop组件 作为 ETL 或 ELT 工具
- wubi安装ubuntu的正确方法
- leetcode -- Basic Calculator I &II --重点,未完全理解
- GPS定位系统的介绍
- 编译器错误消息: CS0016: 未能写入输出文件
- Objective-C_OC中的方法
- 【第四篇章-android平台MediaCodec】推断是否支持硬件解码码
- LeetCode Palindrome Number
- mysql 查询优化
- 栈的顺序表示和实现
- linux select例子
- 设计模式:0. 设计原则
- Unity Networking API文档翻译(二):The High Level API