Prove safety Office- binary number 1

//请实现一个函数,输入一个整数,输出该数二进制表示中 1 的个数。例如,把 9 表示成二进制是 1001,有 2 位是 1。因此,如果输入 9,则该函数输出 
//2。 
//
// 示例 1: 
//
// 输入:00000000000000000000000000001011
//输出:3
//解释:输入的二进制串 00000000000000000000000000001011 中,共有三位为 '1'。
// 
//
// 示例 2: 
//
// 输入:00000000000000000000000010000000
//输出:1
//解释:输入的二进制串 00000000000000000000000010000000 中,共有一位为 '1'。
// 
//
// 示例 3: 
//
// 输入:11111111111111111111111111111101
//输出:31
//解释:输入的二进制串 11111111111111111111111111111101 中,共有 31 位为 '1'。 
//
// 
//
// 注意:本题与主站 191 题相同:https://leetcode-cn.com/problems/number-of-1-bits/ 
// Related Topics 位运算


//leetcode submit region begin(Prohibit modification and deletion)
public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
        
    }
}
//leetcode submit region end(Prohibit modification and deletion)

submit

public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
          return Integer.bitCount(n);
    }
}

Here Insert Picture Description

Internally

public static int bitCount(int i) {
  // HD, Figure 5-2
   i = i - ((i >>> 1) & 0x55555555);
   i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
   i = (i + (i >>> 4)) & 0x0f0f0f0f;
   i = i + (i >>> 8);
   i = i + (i >>> 16);
   return i & 0x3f;
}
Published 148 original articles · won praise 158 · views 60000 +

Guess you like

Origin blog.csdn.net/qq_17623363/article/details/104928577