[LeetCode] 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.

Hide Tags

Bit Manipulation


#include <iostream>
using namespace std;
class Solution {
public:
    int hammingWeight(uint32_t n) {
        int sum =0;
        while(n){
            if(n&-n)    sum++;
            n = n&(n-1);
        }
        return sum;
    }
};

int main()
{
    uint32_t n = 11;
    Solution sol;
    cout<<sol.hammingWeight(n)<<endl;
    return 0;
}
时间: 2024-12-28 00:48:08

[LeetCode] Number of 1 Bits 位操作的相关文章

2016.5.16——leetcode:Number of 1 Bits ,

leetcode:Number of 1 Bits 1.Number of 1 Bits 本题收获: 1.Hamming weight:即二进制中1的个数 2.n &= (n-1)[n = n & (n-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 exam

[Leetcode] Number of 1 Bits 关于位操作的思考

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 fun

[LeetCode] 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

[LeetCode]Number of 1 Bits

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 fun

LeetCode Number of 1 Bits 计算1的个数

题意:提供一个无符号32位整型uint32_t变量,返回其二进制形式的1的个数. 思路:取出一位,就右移1位,挤掉它,循环32次,逐个判断.没难度就不解释了,可能有更好解法,等待第2次思考. 1 class Solution { 2 public: 3 int hammingWeight(uint32_t n) { 4 if(!n) return 0; 5 if(n==1) return 1; 6 7 uint32_t MASK = 1, temp=n; 8 int i, cnt = 0; 9

leetcode number of 1 bits python

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 fun

LeetCode——Number of 1 Bits

public int hammingWeight(int n) { String b_str = Integer.toBinaryString(n); int b_count = 0; for(int i=0; i<b_str.length(); i++) { if(b_str.charAt(i) == '1') { b_count ++; } } return b_count; }

LeetCode:Number of 1 Bits - 整数的汉明重量

1.题目名称 Number of 1 Bits(整数的汉明重量) 2.题目地址 https://leetcode.com/problems/number-of-1-bits/ 3.题目内容 英文:Write a function that takes an unsigned integer and returns the number of '1' bits it has. 中文:写一个函数,输入一个无符号整数,返回其中值为1的比特位的个数(这个值也被称为数字汉明重量) 例如,32位整型数字11

[LeetCode][JavaScript]Number of 1 Bits

https://leetcode.com/problems/number-of-1-bits/ 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 representat