2

I want to compare two multiplication methods implemented in Java which use shift operations on big numbers. Thus I need sufficiently large BigIntegers.

Since I want to compare them bit-wise, what would be the best approach to generate BigIntegers with n bits which are fully used in the multiplication operation.

My approach so far is this:

byte[] bits = new byte[bitLength]; BigInteger number = new BigInteger(bits).flipBit(bitLength); 

1 Answer 1

2

How about this:

import java.math.BigInteger; public class Test { public static void main(String[] args) { int bits = 3; BigInteger value = BigInteger.ZERO .setBit(bits) .subtract(BigInteger.ONE); System.out.println(value); // Prints 7 == 111 in binary } } 

In other words, set the bit which is one higher than you want, then subtract one to get a value which uses all the lower bits.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.