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.

计算一个数字的二进制表示中含有的bit1的个数,简单的右移而已,代码如下所示:

 class Solution {
public:
int hammingWeight(uint32_t n) {
if(n == ) return ;
int ret = ;
while(n != ){
if(n & == ){
ret++;
}
n >>= ;
}
return ret;
}
};

LeetCode OJ:Number of 1 Bits(比特1的位数)的更多相关文章

  1. LeetCode 191. Number of 1 bits (位1的数量)

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

  2. [LeetCode] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数

    Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...

  3. [LeetCode] Binary Number with Alternating Bits 有交替位的二进制数

    Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...

  4. [LeetCode] 191. Number of 1 Bits 二进制数1的个数

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

  5. 【leetcode】Number of 1 Bits

    题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...

  6. LeetCode 191 Number of 1 Bits

    Problem: Write a function that takes an unsigned integer and returns the number of '1' bits it has ( ...

  7. Java for LeetCode 191 Number of 1 Bits

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

  8. (easy)LeetCode 191.Number of 1 Bits

    Number of 1 Bits Write a function that takes an unsigned integer and returns the number of ’1' bits ...

  9. leetcode:Number of 1 Bits

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

  10. Java [Leetcode 191]Number of 1 Bits

    题目描述: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (als ...

随机推荐

  1. spring cloud 转

    http://blog.csdn.net/forezp/article/details/70148833 服务的注册与发现(Eureka) 服务注册(consul) 服务消费者(rest+ribbon ...

  2. 20145303刘俊谦 《Java程序设计》第三周学习总结

    20145303刘俊谦 <Java程序设计>第三周学习总结 教材学习内容总结 1.类与对象: 类:对现实生活中事物的描述,定义类时用关键词class 对象:这类事物实实在在存在的个体,利用 ...

  3. 20145335《java程序设计》第三次实验报告

    20145335郝昊<java程序设计>第三次实验报告 实验目的与要求 以结对编程的方式编写一个软件,Blog中要给出结对同学的Blog网址,可以拍照展现结对编程 情况,可以参考一下其他学 ...

  4. openssl 编译

    不要费事编译了,直接下载吧! https://www.npcglib.org/~stathis/blog/precompiled-openssl/ 下载 openssl https://www.ope ...

  5. Qt大小端

    转:http://blog.csdn.net/usownh/article/details/42614185 大端模式和小端模式是计算机中经常涉及到的两种字节序,也有大端对齐.小端对齐.大尾.小尾等叫 ...

  6. 安装完kali需要做的一些事情

    1. 没有声音的问题[ kali ] 参考:http://tieba.baidu.com/p/4343219808 用pulseaudio --start会看到一些信息,提示类似root用户之类的 我 ...

  7. 终极之shell-zsh全解析

    什么是Zsh Zsh是一款强大的虚拟终端,既是一个系统的虚拟终端,也可以作为一个脚本语言的交互解析器. Zsh的一些特性 兼容bash,原来使用bash的兄弟切换过来毫无压力. 强大的历史纪录功能,在 ...

  8. mvn设置

    mvn仓库网址: https://mvnrepository.com 安装好maven后,一定要确认安装路径下的setting.xml与本地仓库中的setting.xml一致. 坐标: 什么是坐标? ...

  9. [译]JavaScript需要类吗?

    [译]JavaScript需要类吗?   原文:http://www.nczonline.net/blog/2012/10/16/does-javascript-need-classes/ 译者注:在 ...

  10. 【cs231n】最优化笔记

    ): W = np.random.randn(10, 3073) * 0.0001 # generate random parameters loss = L(X_train, Y_train, W) ...