Leetcode 191. Number of 1 Bits (Easy) (cpp)
2016-07-21 18:38
483 查看
Leetcode 191. Number of 1 Bits (Easy) (cpp)
Tag: Bit Manipulation
Difficulty: Easy
Tag: Bit Manipulation
Difficulty: Easy
/* 191. Number of 1 Bits (Easy) 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 res; while (n > 0) { res += n & 0x1; n = n >> 1; } return res; } };
相关文章推荐
- C/C++笔记--strcpy和strncpy函数的安全性分析
- Leetcode 342. Power of Four (Easy) (cpp)
- poj2632 Crashing Robots
- PAT乙级1001. 害死人不偿命的(3n+1)猜想 (15) C++
- c语言的一些语句说明和查找问题
- [算法学习笔记]冒泡排序和选择排序
- c++学习笔记(十一):C++类的访问修饰符
- Windows下php扩展开发c++动态库
- C++--单例模式样板
- 高质量C和C++编程 读书笔记 (第一二章)
- C/C++面试题:static(静态)变量的作用
- C语言求矩阵偶数的平均值和程序输出某数的问题
- 《深度探索C++对象模型》2
- C++ STL Adaptor stack、queue和vector的使用
- win7 c/c++ opencv 开发环境搭建
- struct_list_sign_my_name.c
- 【华为OJ平台练习题】求最后一个单词长度
- 一个关于 字符与字符串 的 小问题
- a small test for array
- c++学习笔记(十):C++类和对象