LargeInteger 相当于 BigInteger 的 and?



LargeInteger似乎没有与 BigInteger's and的等效函数。

由于 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;
             ^

如何复制BigIntegerandLargeInteger上使用?

org.jscience.mathematics.number.LargeInteger似乎没有类似的位函数 and(如果我有sougth sougth the Right class&版本)。

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,条件需要||而不是&&

从文档来判断,有一些方法将大型能级转换为字节数组,还可以从字节数组中创建一个大型词。因此,您可以执行以下操作:

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

现在,如果我错了,请纠正我,但是原始的python代码似乎只能执行n & 1。既然您有偶数()和奇数()方法,为什么不使用它们?以下身份保持:

 large & 1 = large.odd() ? 1 : 0

最新更新