Number of 1 Bits
2015-07-29 23:28
330 查看
题目:
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
unsigned integer: http://baike.baidu.com/view/6052699.htm
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.
unsigned integer: http://baike.baidu.com/view/6052699.htm
public class Solution { // you need to treat n as an unsigned value public int hammingWeight(int n) { int res=0; int k=0; for(int i=1;i<=32;i++) { res=n&1; if(res==1) k++; n=n>>1; } return k; } }
相关文章推荐
- sqlite3 数据库
- CodeIgniter辅助函数
- Unique Binary Search Tree
- 工具--- base64网页解码
- android services
- 基于Ofbiz的Web框架
- eclipse 快捷键的使用
- 关于泛型的自我学习以及笔记
- 【模拟+递归+位运算】POJ1753-Flip Game
- IEnumerable.Select和SelectMany的区别
- 自动化运维工具Ansible之Playbooks基础应用
- 1020. Tree Traversals (25)
- HDU_1789_DoingHomeworkAgain
- inputclean插件的用法
- Scala深入浅出实战经典:29,Case class和Case object代码实战解析
- myslq-5.6基于GTID的主从复制实现
- libuv 使用点滴
- [LeetCode]Kth Largest Element in an Array
- Android沉浸式状态栏设计(安卓4.4以上才能支持)
- lua 文件读写