1,方法一 a,将整数转化为二进制字符串 b,将字符串中0全换位空字符 c,计算现在字符串长度
public static int fun1(
int n){
return Integer.toBinaryString(n).replace(
"0",
"").length();
}
2,方法二 直接调用bitCount函数即可
public static int fun2(
int n){
return Integer.bitCount(n);
}
转载请注明原文地址: https://ju.6miu.com/read-5885.html