LargeInteger эквивалентно BigInteger и?
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;
^
Как может BigInteger
"s and
воспроизводиться для использования на LargeInteger
?
2 ответа
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
состояние нуждается ||
вместо &&
,
Судя по документации, существуют методы для преобразования LargeInteger в байтовые массивы, а также для создания 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