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. HTTP服务器(3)

    功能完整的HTTP服务器 导语 这个一个功能完备的HTTP服务器.它可以提供一个完整的文档输,包括图像,applet,HTML文件,文本文件.它与SingleFileHttpServer非常相似,只不 ...

  2. oracle修改某字段不是必输性

    执行sql: ALTER TABLE table_name MODIFY 要修改的字段名字 NULL;

  3. js中获取时间戳

    function conver(){ var date = new Date(); var year = date.getFullYear() var month=date.getMonth()+1; ...

  4. NodeJS中间层搭建

    前言 最近碰了个壁,公司开发的一个新项目里我大胆地提出要前后端完全分离,用JavaScript模板引擎.ajax.路由等技术替代繁琐的前后端混合的业务逻辑,项目进行到一半前辈提出来仅仅靠前端的力量无法 ...

  5. 仿netty线程池简化版本

    package com.hcxy.car.threadpools; import java.io.IOException; import java.nio.channels.Selector; imp ...

  6. Stitching模块中对特征提取的封装解析(以ORB特性为例)

    titching模块中对特征提取的封装解析(以ORB特性为例)     OpenCV中Stitching模块(图像拼接模块)的拼接过程可以用PipeLine来进行描述,是一个比较复杂的过程.在这个过程 ...

  7. 20145307第十周JAVA学习报告

    教材学习内容总结 Java的网络编程 1.计算机网络概述 (1)路由器和交换机组成了核心的计算机网络,计算机只是这个网络上的节点以及控制等,通过光纤.网线等连接将设备连接起来,从而形成了一张巨大的计算 ...

  8. 201453131《Java程序设计》实验三实验报告

    实验三 敏捷开发与XP实践 实验内容 •下载并学会使用git上传代码: •与同学结对,相互下载并更改对方代码,并上传: 实验步骤 下载并用git上传代码: •1.下载并安装好git,在cmd中输入gi ...

  9. 20145335郝昊 Java学习心得 密码学代码复写

    20145335郝昊 Java学习心得 密码学代码复写 本学期我们学习了现代密码学这门课程,在上课的时候接触到了很多种类型的密码体制,对于一些典型很通用的密码体制有自己的学习和设计.不论是从密码体制还 ...

  10. 学Git,用Git ③

    不知道我前面是否将git讲清楚了,这里再稍微总结一下git的一个重要功能用法,同时增加两个很实用的git使用技巧. 1.git"读档"与git"回退" 我发现我 ...