Article Directory
1. Description
Write a function, the input is an unsigned integer, return the number of digits in the binary expression of '1' (also known as Hamming weight).
2. Example
Input: 00000000000000000000000000001011
Output: 3
Explanation: In the input binary string 00000000000000000000000000001011, a total of three bits are '1'.
Input: 00000000000000000000000010000000
Output: 1
Explanation: In the input binary string 00000000000000000000000010000000, a total of one bit is '1'.
Input: 11111111111111111111111111111101
Output: 31
Explanation: In the input binary string 11111111111111111111111111111101, a total of 31 bits are '1'.
prompt
- Note that in some languages (such as Java), there is no unsigned integer type. In this case, both input and output will be designated as signed integer types, and should not affect your implementation, because the internal binary representation is the same regardless of whether the integer is signed or unsigned.
- In Java, the compiler uses two's complement notation to represent signed integers. Therefore, in example 3 above, the input represents a signed integer -3.
3. Analysis
A positive integer is 3, and its binary representation is "0000 0011". Perform 3&(3-1) operation on 3, as shown in the following table
operating | Integer representation | Binary representation |
---|---|---|
3 | 0000 0011 | |
& | 3 -1 = 2 | 0000 0010 |
= | 2 | 0000 0010 |
Continue to perform 2&(2-1) operation on the result 2 of 3&(3-1),
operating | Integer representation | Binary representation |
---|---|---|
2 | 0000 0010 | |
& | 2 -1 = 1 | 0000 0001 |
= | 0 | 0000 0000 |
Observing the above two tables, we can draw a preliminary conclusion: there are 2 1s in the binary representation of 3, and after 2 & operations, the result is 0.
Let's take another example of negative numbers.
A negative number is (-125), and its binary representation is "1000 0011".
Perform (-125) & (-125-1) operation on (-125), the process is shown in the following table,
operating | Integer representation | Binary representation |
---|---|---|
-125 | 1000 0011 | |
& | -125-1 = -126 | 1000 0010 |
= | -126 | 1000 0010 |
Continue to perform & operation on the result (-126) of (-125) & (-125-1), the process is shown in the following table,
operating | Integer representation | Binary representation |
---|---|---|
-126 | 1000 0010 | |
& | -126-1 = -127 | 1000 0001 |
= | -128 | (1)1000 0000 |
Continue to perform & operation on the result (-128) of (-126) & (-126-1), the process is shown in the following table,
operating | Integer representation | Binary representation |
---|---|---|
-128 | (1)1000 0000 | |
& | -128-1 = -129 | (1)0111 1111 |
= | 0 | 0000 0000 |
Observing these three tables, we can also draw a conclusion: (-125) binary has 3 1s. After 3 & operations, the result is 0.
Conclusion:
integer nnThe number of 1 in the binary string of n , equal tonnn & ( n n n -1) The number of operations.
4. Code
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int numOneBits = 0;
while(n != 0)
{
n = n & (n - 1);
++numOneBits;
}
return numOneBits;
}
}
But there is a simpler code:
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
return Integer.bitCount(n);
}
}
5. Verification
6. Source
- LeetCode 191. Number of bits 1
Source: LeetCode
Link: https://leetcode-cn.com/problems/number-of-1-bits
Copyright is owned by Leetcode Network.