lc 191 Number of 1 Bits


191 Number of 1 Bits

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.

analysation##

如果当前的数据为有符号数,在进行右移的时候,根据符号位决定左边补0还是补1。如果符号位为0,则左边补0;但是如果符号位为1,则根据不同的计算机系统,可能有不同的处理方式。

将所有对2的整除运算转换为位移运算,可提高程序的运行效率。

soliution

int hammingWeight(uint32_t n) {
int t, sum = 0;
for (t = 31; t >= 0; t--) {
if ((n >> t & 1) == 1)
sum++;
}
return sum;
}

LN : leetcode 191 Number of 1 Bits的更多相关文章

  1. Leetcode#191. Number of 1 Bits(位1的个数)

    题目描述 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量). 示例 : 输入: 11 输出: 3 解释: 整数 11 的二进制表示为 000000 ...

  2. 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 ...

  3. [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 ...

  4. LeetCode 191 Number of 1 Bits

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

  5. 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 ...

  6. (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 ...

  7. 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 ...

  8. [LeetCode] 191. Number of 1 Bits ☆(位 1 的个数)

    描述 Write a function that takes an unsigned integer and return the number of '1' bits it has (also kn ...

  9. 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 ...

随机推荐

  1. codevs——1275 有鱼的声音

    1275 有鱼的声音 2012年CCC加拿大高中生信息学奥赛  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 青铜 Bronze 题解  查看运行结果     题目描述 Des ...

  2. python可变參数调用函数问题

    一直使用python实现一些想法,近期在使用python的过程中出现这样一个需求,定义了一个函数.第一个是普通參数.第二个是默认參数,后面还有可变參数,在最初学习python的时候,都知道非keywo ...

  3. protobuf入门

    ,step1:准备.proto文件 syntax = "proto3"; message gps_data { int64 id = 1; string terminalId = ...

  4. java7新特性之Try-with-resources (TWR)

    java7新特性之Try-with-resources (TWR) This change is easy to explain, but it has proved to have hidden s ...

  5. Windows命令实现匿名邮件发送

    在日常工具开发中,常常会有发送邮件的需求.在一些高级语言中,如Python.C#中,都有专门的邮件发送模块,如Python 中的 smtplib 模块.那么.一封邮件究竟是怎样发送到一个特定的邮箱呢? ...

  6. Message: unknown error: Element is not clickable at point

    Message: unknown error: Element is not clickable at point google chrome - Debugging "Element is ...

  7. ios29--多线程

    进程是指在系统中正在运行的一个应用程序(一个程序可以对应多个进程).每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内.比如同时打开迅雷.Xcode,系统就会分别启动2个进程.1个进程要 ...

  8. 2015-2016 ACM-ICPC Pacific Northwest Regional Contest (Div. 2)V - Gears

    Problem V | limit 4 secondsGearsA set of gears is installed on the plane. You are given the center c ...

  9. FreeMarker:

    ylbtech-FreeMarker: 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http://ylbt ...

  10. Python---scikit-learn(sklearn)模块

    Python在机器学习方面一个非常强力的模块---scikit-learn模块,它作为数据挖掘和数据分析方面的一个简单而有效的工具,主要包括6大功能:分类(Classification),回归(Reg ...