java中BigInteger的用法

BigInteger介绍

在Java中能提供操作的最大的数据类型的是long,但现实生活中如果有些数字超过了long的取值范围怎么办呢?Java提供了BigInteger方法
在Java中可以使用BigInteger用来操作大整数,也可以用来转换进制,而这些大数会以字符串的形式传入

package Four;

import java.math.BigInteger;

/*
 * BigInteger用法
 * 
 * */
public class BigIntegerTest {
	public static void main(String[] args) {
		BigInteger big1 = new BigInteger("100000");
		BigInteger big2 = new BigInteger("8");
		// BigInteger add(BigInteger val) 返回两个大整数的和
		System.out.println(big1.add(big2));
		// BigInteger and(BigInteger val) 返回两个大整数的按位与的结果
		System.out.println(big1.and(big2));
		// BigInteger andNot(BigInteger val) 返回两个大整数与非的结果
		System.out.println(big1.andNot(big2));
		// BigInteger divide(BigInteger val) 返回两个大整数的商 }
		System.out.println(big1.divide(big2));
		//double doubleValue()   返回大整数的double类型的值
		System.out.println(big1.doubleValue());
		//float floatValue()   返回大整数的float类型的值
		System.out.println(big1.floatValue());
		//BigInteger gcd(BigInteger val)  返回大整数的最大公约数
		System.out.println(big1.gcd(big2));
		//int intValue() 返回大整数的整型值
		System.out.println(big1.intValue());
		//long longValue() 返回大整数的long型值
		System.out.println(big1.longValue());
		//BigInteger max(BigInteger val) 返回两个大整数的最大者
		System.out.println(big1.max(big2));
		//BigInteger min(BigInteger val) 返回两个大整数的最小者
		System.out.println(big1.min(big2));
		//BigInteger mod(BigInteger val) 用当前大整数对val求模
		System.out.println(big1.mod(big2));
		//BigInteger multiply(BigInteger val) 返回两个大整数的积
		System.out.println(big1.multiply(big2));
		//BigInteger negate() 返回当前大整数的相反数
		System.out.println(big1.negate());
		//BigInteger not() 返回当前大整数的非
		System.out.println(big1.not());
		//BigInteger or(BigInteger val) 返回两个大整数的按位或
		System.out.println(big1.or(big2));
		//BigInteger pow(int exponent) 返回当前大整数的exponent次方
		System.out.println(big1.pow(9));
		//BigInteger remainder(BigInteger val) 返回当前大整数除以val的余数
		System.out.println(big1.remainder(big2));
		//BigInteger leftShift(int n) 将当前大整数左移n位后返回
		System.out.println(big1.shiftLeft(3));
		//BigInteger rightShift(int n) 将当前大整数右移n位后返回
		System.out.println(big1.shiftRight(3));
		//BigInteger subtract(BigInteger val)返回两个大整数相减的结果
		System.out.println(big1.subtract(big2));
		//byte[] toByteArray(BigInteger val)将大整数转换成二进制反码保存在byte数组中
		byte[] b1 = big1.toByteArray();
		//String toString() 将当前大整数转换成十进制的字符串形式
		System.out.println(big1.toString());
		//BigInteger xor(BigInteger val) 返回两个大整数的异或
		System.out.println(big1.xor(big2));
		
		System.out.println("*******************************************");
		
		//利用BigInteger抓换进制
		String s1  = new BigInteger("100",10).toString(2);
		System.out.println("十进制的100转换为2进制是:"+s1);
		String s2  = new BigInteger("100",10).toString(8);
		System.out.println("十进制的100转换为8进制是:"+s2);
		String s3  = new BigInteger("100",10).toString(16);
		System.out.println("十进制的100转换为16进制是:"+s3);
		String s4  = new BigInteger("100",8).toString(10);
		System.out.println("八进制的100转换为10进制是:"+s4);
	}
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值