Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to multiply two large numbers(say 512 bits) in java

Tags:

java

i want to multiply two 512 bit integers in java and store the result.suggest some method to do this.

like image 245
condinya Avatar asked May 29 '10 11:05

condinya


2 Answers

I suggest you use java.math.BigInteger

like image 175
Michael Borgwardt Avatar answered Oct 26 '22 11:10

Michael Borgwardt


Use java.math.BigInteger

A quick example of usage:

import java.math.BigInteger;

public class BigIntegerTest {
    public static void main(String[] args) {
        BigInteger b1 = new BigInteger("200000000000000000000000000000000001");
        BigInteger b2 = new BigInteger("400000000000000000000000000000000000");

        System.out.println(b1.multiply(b2));
        System.out.println(b1.bitCount());
        System.out.println(b1.pow(13));
    }
}
like image 30
mikera Avatar answered Oct 26 '22 10:10

mikera