package data_structure;
public class DoubleData {
//判断二进制有多少个1 法一
public static int fun(int x){
int count = 0;
while(x>0){
count++;
x = x&(x-1);
}
return count;
}
//法二 求二进制中的1位数
public static int funnuy(int y){
int count = 0;
int mod=1;
while(y>=mod){
if((y&mod)>0){
count++;
}
mod =mod<<1;
}
return count;
}
//求8进制中1的位数 ,或者求n进制(8换成n)
public static int bie(int y){
int count = 0;
double mod=0;
while(y>=mod){
mod =Math.pow(8,mod);
if((y&(int)mod)>0){
count++;
}
}
return count;
}
public static void main(String[] args) {
// System.out.println(fun(15));
// System.out.println(funnuy(15));
System.out.println(bie(3));
}
}
如上所示,以上三个算法对于 正整数成立,如果是负数的话直接用Math.abs()就可以解决,
这些都是网上找不到我自己写的