String s = "-23423432"; BigInteger bs = new BigInteger(s); System.out.println(bs); //-23423432 BigInteger ba = bs.abs(); System.out.println(ba); //23423432
1.2 两数相加add()的使用
1 2 3 4 5 6
String s = "-23423432"; String ss = "23423433"; BigInteger bs = new BigInteger(s); BigInteger bss = new BigInteger(ss); BigInteger sad = bs.add(bss); System.out.println(sad); //1
1.3 两数相减subtract()的使用
1 2 3 4 5 6
String s = "23423432"; String ss = "23423433"; BigInteger bs = new BigInteger(s); BigInteger bss = new BigInteger(ss); BigInteger sad = bss.subtract(bs); //bss-bs System.out.println(sad); //1
1.4 两数相乘multiply()的使用
1 2 3 4 5 6
String s = "2"; String ss = "3"; BigInteger bs = new BigInteger(s); BigInteger bss = new BigInteger(ss); BigInteger sad = bss.multiply(bs); System.out.println(sad);
1.5 两数相除divide()的使用(商)
1 2 3 4 5 6
String s = "3"; String ss = "7"; BigInteger bs = new BigInteger(s); BigInteger bss = new BigInteger(ss); BigInteger sad = bss.divide(bs); // bss/bs System.out.println(sad); //2
1.6 求相反数negate()的使用
1 2 3 4 5 6 7 8
String s = "-3"; String ss = "7"; BigInteger bs = new BigInteger(s); BigInteger bss = new BigInteger(ss); BigInteger nebs = bs.negate(); BigInteger nebss = bss.negate(); System.out.println(nebs); // 3 System.out.println(nebss); // -7
1.7 求余数remainder()的使用
📢注意在小数中的使用!与mod()对照。
1 2 3 4 5 6
String s = "3"; String ss = "7"; BigInteger bs = new BigInteger(s); BigInteger bss = new BigInteger(ss); BigInteger nebs = bss.remainder(bs); // bss%bs System.out.println(nebs); // 1
1.8 求模mod()的使用
📢注意在小数中的使用!与remainder()对照。
1 2 3 4 5 6
String s = "3"; String ss = "7"; BigInteger bs = new BigInteger(s); BigInteger bss = new BigInteger(ss); BigInteger nebs = bss.mod(bs); System.out.println(nebs); //1
1.9 求幂次方pow()的使用
1 2 3 4 5
String s = "2"; int p = 4; BigInteger bs = new BigInteger(s); BigInteger nebs = bs.pow(p); System.out.println(nebs);
1.10 返回大整数的double值doubleValue()的使用
📢注意:当数字长度超过了7位,会自动采用科学计数法输出!!!
1 2 3 4
String s = "2234231"; BigInteger bs = new BigInteger(s); double ne= bs.doubleValue(); System.out.println(ne); //2234231.0
1.11 返回大整数的float值floatValue()的使用
1 2 3 4
String s = "2234231"; BigInteger bs = new BigInteger(s); float ne= bs.floatValue(); System.out.println(ne); //2234231.0
1.12 返回大整数的int值intValue()的使用
1 2 3 4
String s = "223423321"; BigInteger bs = new BigInteger(s); int ne= bs.intValue(); System.out.println(ne); //223423321
1.13 返回大整数的long值longValue()的使用
1 2 3 4
String s = "2234233214522333332"; BigInteger bs = new BigInteger(s); long ne= bs.longValue(); System.out.println(ne); //2234233214522333332
1.14 两数中的最大值max()的使用
1 2 3 4 5 6
String s = "2234232"; String ss = "2342342343"; BigInteger bs = new BigInteger(s); BigInteger bss = new BigInteger(ss); BigInteger ne= bss.max(bs); System.out.println(ne); //2342342343
1.15 两数中的最小值min()的使用
1 2 3 4 5 6
String s = "2234232"; String ss = "2342342343"; BigInteger bs = new BigInteger(s); BigInteger bss = new BigInteger(ss); BigInteger ne= bss.min(bs); System.out.println(ne); //2234232
1.16 求两数的最大公约数gcd()的使用
1 2 3 4 5 6
String s = "2234232"; String ss = "2342342343"; BigInteger bs = new BigInteger(s); BigInteger bss = new BigInteger(ss); BigInteger ne= bss.gcd(bs); System.out.println(ne); // 33
1.17 两个数的按位与and()的使用
1 2 3 4 5 6
String s = "23"; String ss = "7"; BigInteger bs = new BigInteger(s); BigInteger bss = new BigInteger(ss); BigInteger ne= bss.and(bs); System.out.println(ne); //7
1.18 两个数的按位或or()的使用
1 2 3 4 5 6
String s = "23"; String ss = "7"; BigInteger bs = new BigInteger(s); BigInteger bss = new BigInteger(ss); BigInteger ne= bss.or(bs); System.out.println(ne); // 23
1.19 当前数的非not()的使用
1 2 3 4
String ss = "7"; BigInteger bss = new BigInteger(ss); BigInteger ne= bss.not(); System.out.println(ne); // -8
1.20 两个数与非andNot()的使用
1 2 3 4 5 6
String s = "3"; String ss = "7"; BigInteger bs = new BigInteger(s); BigInteger bss = new BigInteger(ss); BigInteger ne= bss.andNot(bs); System.out.println(ne); // 4
1.21 两个数的异或
1 2 3 4 5 6
String s = "2"; String ss = "7"; BigInteger bs = new BigInteger(s); BigInteger bss = new BigInteger(ss); BigInteger ne= bss.xor(bs); System.out.println(ne); // 5
1.22 将当前数左移n位shiftLeft()的使用
1 2 3 4
String s = "2"; BigInteger bs = new BigInteger(s); BigInteger ne= bs.shiftLeft(3); System.out.println(ne); // 16
1.23 将当前数右移n位后shiftRight()的使用
1 2 3 4
String s = "64"; BigInteger bs = new BigInteger(s); BigInteger ne= bs.shiftRight(3); System.out.println(ne); // 8
2.BigInteger进制转换
2.1 十进制转其他进制
1 2 3 4 5 6 7 8 9 10 11 12 13
String string1 = new BigInteger("20", 10).toString(2); System.out.println("十进制的20转换成二进制是:"+string1);
String string2 = new BigInteger("20", 10).toString(8); System.out.println("十进制的20转换成八进制是:"+string2);
String string3 = new BigInteger("20", 10).toString(16); System.out.println("十进制的20转换成十六进制是:"+string3);