zoukankan      html  css  js  c++  java
  • 大数字运算——1、BigInteger

    package com.wh.BigInteger;
    
    import java.math.BigInteger;
    import java.util.Arrays;
    
    /**
     * @author 王恒
     * @datetime 2017年4月6日 上午11:08:21
     * @description
     * 实现两个超级大的数据进行运算
     */
    public class TestBigInteger {
    
    	public static void main(String[] args) {
    
    		BigInteger b1 = new BigInteger("111111111111111111111111111111111111111111");
    		BigInteger b2 = new BigInteger("222222222222222222222222222222222222222222");
    		BigInteger b3 = new BigInteger("10");
    		//相加
    		System.out.println(b1.add(b2));  
    		//减去
    		System.out.println(b1.subtract(b2));
    		//相乘
    		System.out.println(b1.multiply(b2));
    		//相除
    		System.out.println(b2.divide(b3)); 
    		//取余
    		System.out.println(b2.remainder(b3));
    		//返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组
    		System.out.println(Arrays.toString(b2.divideAndRemainder(b3)));
    		
    		
    		System.out.println("
    
    ");
    		//a.pow(b)      a的b次方       a是Integer类型      b是int类型
    		System.out.println(b1.pow(10));
    		//negate取反数
    		System.out.println("negate()    "+b1.negate());
    		
    
    		System.out.println("
    
    以下为位运算"); 
    		//shiftLeft 左位移      shiftRight   右位移
    		System.out.println("左位移:      "+b1.shiftLeft(2));
    		System.out.println("右唯一:      "+b1.shiftRight(2));
    		//and:与       or:或          反码:~(加一取反)
    		System.out.println("and:与           "+b1.and(b2));
    		System.out.println("or:或              "+b1.or(b2));
    		System.out.println("反码:~    "+b1.not());
    		
    		
    		System.out.println("
    
    以下为比较运算"); 
    		System.out.println(b1.compareTo(b2));//值只有-1、0、1分别对应<、=、>
    		System.out.println(b2.compareTo(b1));
    		System.out.println(b2.compareTo(b2));
    		System.out.println(b1.equals(b2));//比较值是否相等
    		System.out.println(b1.equals(b1)); 
    		System.out.println(b1.max(b2));//给出两者之间的最大值
    		System.out.println(b1.min(b2));//给出两者之间的最小值
    		
    		
    	}
    
    }
    

      

    运算结果:

    333333333333333333333333333333333333333333
    -111111111111111111111111111111111111111111
    24691358024691358024691358024691358024691308641975308641975308641975308641975308642
    22222222222222222222222222222222222222222
    2
    [22222222222222222222222222222222222222222, 2]
    
    
    
    286797199079244131332225723124083690656613672283088775926871539310870055713547973981830372425140375061578119065354852721792901011661948180202381259878763579451954764039338146620324977185189604156428598178760679846232913097169726611955208182084556710224309621717847073734227136689544918036932527850895361396335442651304897816078075313164451661202668091206709514013338618237095867402327523490604258900950612575601
    negate()    -111111111111111111111111111111111111111111
    
    
    以下为位运算
    左位移:      444444444444444444444444444444444444444444
    右唯一:      27777777777777777777777777777777777777777
    and:与           1366618052755712315811601266990055448966
    or:或              331966715280577621017521732066343277884367
    反码:~    -111111111111111111111111111111111111111112
    
    
    以下为比较运算
    -1
    1
    0
    false
    true
    222222222222222222222222222222222222222222
    111111111111111111111111111111111111111111
    

      

  • 相关阅读:
    [学习笔记]基于值域预处理的快速 GCD
    [学习笔记]整除偏序与zeta变换、Möbius变换、lcm卷积、gcd卷积
    [学习笔记]MinMax容斥
    [学习笔记]Pollard Rho算法
    [学习笔记]Miller Rabin测试
    [学习笔记]万能欧几里得
    用C#写程序安装包 Joe
    linux 命令
    几个有用的php字符串过滤,转换函数
    linux挂载 Windows分区
  • 原文地址:https://www.cnblogs.com/1020182600HENG/p/6673311.html
Copyright © 2011-2022 走看看