condinya
condinya

Reputation: 988

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

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

Upvotes: 8

Views: 2052

Answers (2)

mikera
mikera

Reputation: 106351

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));
    }
}

Upvotes: 7

Michael Borgwardt
Michael Borgwardt

Reputation: 346300

I suggest you use java.math.BigInteger

Upvotes: 13

Related Questions