java大数据运算

     大数运算类似的文章有很多,本文主要分享一下自己的经验。把实现思路写一下,希望能够帮助到有需要的人。

    关于整数的运算通常有byte、short、int、long,当时数字有20位或者更长的时候,原有的数据类型已经不能使用,所以需要大数的运算,代码实现如下:

/** 

 * 实现大数的运算 通过Byte数组运算,实现了进制的灵活调整 

 *  

 * @author wangzhaoguo 

 *  

 */

public class BigNumberUtil { 

  

    /** 

     * 数组加法运算a+b 

     *  

     * @param a 

     * @param b 

     * @param hex 

     * @return 

     */

    public static byte[] add(byte[] a, byte[] b, short hex) { 

        if (a.length != b.length) 

            throw new RuntimeException("参数长度错误"); 

  

        byte[] value = a.clone(); 

        short flag = 0; 

        for (int i = value.length - 1; i >= 0; i--) { 

            short t = (short) (byteToShort(value[i], hex) + 

                    byteToShort(b[i], hex) + flag); 

            flag = 0; 

            if (t >= (hex * hex)) 

                flag = 1; 

  

            value[i] = shortToByte(t, hex); 

        } 

  

        if (flag != 0) { 

            throw new ArrayIndexOutOfBoundsException("结果越界"); 

        } 

  

        return value; 

    } 

  

    /** 

     * 数组减法运算 a-b 

     *  

     * @param a 

     * @param b 

     * @param hex 

     * @return 

     */

    public static byte[] sub(byte[] a, byte[] b, short hex) { 

        if (a.length != b.length) 

            throw new RuntimeException("参数长度错误"); 

  

        byte[] value = a.clone(); 

        short flag = 0; 

        for (int i = value.length - 1; i >= 0; i--) { 

            short t = (short) (byteToShort(value[i], hex) -  byteToShort(b[i], hex) + flag); 

  

            flag = 0; 

            if (t < 0) { 

                t += (hex * hex); 

                flag = -1; 

            } 

  

            value[i] = shortToByte(t, hex); 

        } 

  

        if (flag != 0) { 

            throw new ArrayIndexOutOfBoundsException("结果负值"); 

        } 

  

        return value; 

    } 

  

    /** 

     * 大小比较运算 

     *  

     * @param value 

     * @param temp 

     * @return 如果 value>temp 返回 1,小于-1,等于0 

     */

    public static short compareTo(byte[] value, byte[] temp) { 

        for (short i = 0; i < value.length; i++) { 

            short t = (short) (byteToShort(value[i], (short) 16) -  byteToShort(temp[i], (short) 16)); 

            if (t > 0) { 

                return 1; 

            } else if (t < 0) { 

                return -1; 

            } 

        } 

        return 0; 

    } 

  

    private static short byteToShort(byte data, short hex) { 

        return (short) ((data & 0xF) + ((data >> 4) & 0x0F) * hex); 

    } 

  

    private static byte shortToByte(short num, short hex) { 

        short data = (short) (num % (hex * hex)); 

        return (byte) (((data / hex) << 4) | (data % hex)); 

    } 

  

    public static void main(String args[]) { 

        byte[] a = new byte[] { 1, 7, 8 }; 

        byte[] b = new byte[] { 2, 6, 3 }; 

  

        byte[] c = BigNumberUtil.add(a, b, (short) 10); 

        System.out.println(StringUtil.bytes2HexString(a)); 

        System.out.println(StringUtil.bytes2HexString(b)); 

        System.out.println(StringUtil.bytes2HexString(c)); 

  

        c = BigNumberUtil.add(a, b, (short) 16); 

        System.out.println(StringUtil.bytes2HexString(a)); 

        System.out.println(StringUtil.bytes2HexString(b)); 

        System.out.println(StringUtil.bytes2HexString(c)); 

  

        c = BigNumberUtil.sub(b, a, (short) 16); 

        System.out.println(StringUtil.bytes2HexString(a)); 

        System.out.println(StringUtil.bytes2HexString(b)); 

        System.out.println(StringUtil.bytes2HexString(c)); 

  

        c = BigNumberUtil.sub(b, a, (short) 10); 

        System.out.println(StringUtil.bytes2HexString(a)); 

        System.out.println(StringUtil.bytes2HexString(b)); 

        System.out.println(StringUtil.bytes2HexString(c)); 

          

        short r = BigNumberUtil.compareTo(b, a); 

        System.out.println(StringUtil.bytes2HexString(a)); 

        System.out.println(StringUtil.bytes2HexString(b)); 

        System.out.println(r); 

          

        r = BigNumberUtil.compareTo(a, b); 

        System.out.println(StringUtil.bytes2HexString(a)); 

        System.out.println(StringUtil.bytes2HexString(b)); 

        System.out.println(r); 

          

        r = BigNumberUtil.compareTo(b, b); 

        System.out.println(StringUtil.bytes2HexString(a)); 

        System.out.println(StringUtil.bytes2HexString(b)); 

        System.out.println(r); 

    } 
}

    本文以byte[]数组和BCD码结合的形式进行实现,希望能够给大家提供参考。





你可能感兴趣的:(java)