[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 function should return 3.

这个题目的解法还是很清楚的

一、整数不变,mask右移

使用一个mast不断与这个数字进行与操作,得到相应的bit是否是零

int mask = 1;

int num;

int count = 0;

for shift=0:31

mask = mask << shift

int bit = num & mask;

if bit!=0

count++;

end

end

二、mask不变,整数右移法。

这里只适合整数非负的情况,负数右移,符号位会被不断复制,最后变为全一。

时间: 2024-10-06 07:16:19

[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 位操作

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