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.

位运算,找出int型数字为1的位的个数。因为是32位,外层循环有32次,但是一旦循环过的位数最大值大于当前值,就停止运算,因为之后的左移运算,全为0

int hammingWeight(uint32_t n) {
    int result = 0;
    int cursor = 1;
    for(int i = 1; i <= 32; i++)
    {
        if( (n & cursor) != 0)
        {
            result++;
        }
        cursor = cursor << 1;

        if(pow(2,i) - 1 >= n)
        {
            break;
        }
    }
    return result;
}
时间: 2024-08-30 01:41:36

191_Number of 1 Bits的相关文章

LeetCode 191_Number of 1 Bits

两种思路: 思路一: 1.n&1 可得到最低位的数字,然后加到count变量中即可 2.n>>>1,注意是三个>不是两个>,三个的是逻辑移位,两个的是算术移位(Java中的定义) 缺点就是:有多少位就要需要移动多少次 思路二: 1.假设n= 1111000111000 那 n-1 = 1111000110111, (n-1) & n = 1111000110000,刚好把最后一个1给干掉了.也就是说, (n-1)&n 刚好会从最后一位开始,每次会干掉一

[LeetCode]Reverse Bits

题目:Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through thi

191. 求1的位数 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 retu

【LeetCode】338. Counting Bits (2 solutions)

Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example:For num = 5 you should return [0,1,1,2,1,2]. Follow up

[LeetCode][Java][JavaScript]Counting Bits

Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example:For num = 5 you should return [0,1,1,2,1,2]. Follow up

Java [Leetcode 190]Reverse Bits

题目描述: everse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000). 解题思路: 移位操作. 代码如下: publi

leetcode笔记:Reverse Bits

一. 题目描述 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000). 二. 题目分析 题目的要求比较简单,输

Codeforces Round #276 (Div. 1) A. Bits 贪心

A. Bits Let's denote as  the number of bits set ('1' bits) in the binary representation of the non-negative integer x. You are given multiple queries consisting of pairs of integers l and r. For each query, find the x, such that l ≤ x ≤ r, and is max

LeetCode OJ:Number of 1 Bits(比特1的位数)

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 retu