我想比较两种用 Java 实现的乘法方法,它们对大数使用移位运算。因此我需要足够大的 BigIntegers。
由于我想逐位比较它们,生成具有 n 位的 BigIntegers 的最佳方法是什么,这些位完全用于乘法运算。
到目前为止我的方法是这样的:
byte[] bits = new byte[bitLength];
BigInteger number = new BigInteger(bits).flipBit(bitLength);
最佳答案
这个怎么样:
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
}
}
换句话说,设置比你想要的高一位的位,然后减去一个得到一个使用所有低位的值。
关于java - 获取 n 位 BigInteger 的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8086588/