求二进制中1的个数扩展至n进制

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()就可以解决,

这些都是网上找不到我自己写的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值