0

LargeInteger似乎没有与BigInteger'sand等效的功能。

and(BigInteger val)Returns a BigInteger whose value is (this & val). (This method returns a negative BigInteger if and only if this and val are both negative.)”开始,我试图遵循这个很好的答案来复制testBit

static LargeInteger and(LargeInteger i, LargeInteger j) {
    return i & j;
}

但编译器报告

error: bad operand types for binary operator '&'
    return i & j;
             ^

怎样才能被复制BigInteger上来and使用LargeInteger

4

2 回答 2

0

从文档来看,有将 LargeIntegers 转换为字节数组的方法,也有从字节数组创建 LargeInteger 的方法。因此,您可以执行以下操作:

convert operands to byte arrays
combine the individual bytes with the operator you want (&, |, ^)
convert resulting byte array back to LargeInteger

现在,如果我错了,请纠正我,但原来的 python 代码似乎只做n & 1. 既然你有 even() 和 odd() 方法,为什么不使用它们呢?以下身份成立:

 large & 1 = large.odd() ? 1 : 0
于 2014-01-22T17:22:56.323 回答
0

org.jscience.mathematics.number.LargeInteger似乎没有类似的按位功能and(如果我有正确的类和版本)。

static LargeInteger and(LargeInteger lhs, LargeInteger rhs) {
     long l = lhs.longValue(); // Low order bits
     long r = rhs.longValue();
     long lo = l & r;

     LargeInteger hi = LargeInteger.ZERO;
     if (lhs.bitLength() > 64 && rhs.bitLength() > 64) {
         hi = and(lhs.shiftRight(64), rhs.shiftRight(64)).shiftLeft(64);
     }
     return hi.plus(lo);
}

请注意,or条件需要按位||而不是&&.

于 2014-01-22T17:27:20.237 回答