Number of 1 Bits
2015-12-11 10:27
274 查看
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
so the function should return 3.
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 result = 0; while(n != 0){ if(n&1 == 1){ result++; } n = n>>1; } return result; } };
相关文章推荐
- CSS-background-position百分比
- win 2012 安装Net35
- Docker镜像和镜像构建
- oracle job interval·相关事例
- setvisibility(view.gone)失效问题
- ibatis如何支持clob 和blob
- 12月初.wang域名总量15强:易名西数前二 地位稳定
- Spring+Hibernate多数据源整合
- 4.shell变量替换技术
- 真假const 指针
- 在IOS中,判断字符A中是否包含字符B
- python读书笔记-《简明python教程》上
- Android ImageView圆形头像 图片完全解析
- Java Base64加密解码实例
- iPhone 6 (iOS 9.2) extractiion failed by XRY
- 3.shell变量的子串应用
- 前出塞:《PHP的魔术符号magic_quotes_gpc》
- 报表模型-格集模型-格集的应用
- iOS 7 中 StoryBoard 整体缩放
- Android数据库更新并保留原来数据的实现