本文整理汇总了Java中java.math.BigInteger.getLowestSetBit方法的典型用法代码示例。如果您正苦于以下问题:Java BigInteger.getLowestSetBit方法的具体用法?Java BigInteger.getLowestSetBit怎么用?Java BigInteger.getLowestSetBit使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.math.BigInteger
的用法示例。
在下文中一共展示了BigInteger.getLowestSetBit方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: ECFieldF2m
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* Creates an elliptic curve characteristic 2 finite
* field which has 2^{@code m} elements with
* polynomial basis.
* The reduction polynomial for this field is based
* on {@code rp} whose i-th bit corresponds to
* the i-th coefficient of the reduction polynomial.<p>
* Note: A valid reduction polynomial is either a
* trinomial (X^{@code m} + X^{@code k} + 1
* with {@code m} > {@code k} >= 1) or a
* pentanomial (X^{@code m} + X^{@code k3}
* + X^{@code k2} + X^{@code k1} + 1 with
* {@code m} > {@code k3} > {@code k2}
* > {@code k1} >= 1).
* @param m with 2^{@code m} being the number of elements.
* @param rp the BigInteger whose i-th bit corresponds to
* the i-th coefficient of the reduction polynomial.
* @exception NullPointerException if {@code rp} is null.
* @exception IllegalArgumentException if {@code m}
* is not positive, or {@code rp} does not represent
* a valid reduction polynomial.
*/
public ECFieldF2m(int m, BigInteger rp) {
// check m and rp
this.m = m;
this.rp = rp;
if (m <= 0) {
throw new IllegalArgumentException("m is not positive");
}
int bitCount = this.rp.bitCount();
if (!this.rp.testBit(0) || !this.rp.testBit(m) ||
((bitCount != 3) && (bitCount != 5))) {
throw new IllegalArgumentException
("rp does not represent a valid reduction polynomial");
}
// convert rp into ks
BigInteger temp = this.rp.clearBit(0).clearBit(m);
this.ks = new int[bitCount-2];
for (int i = this.ks.length-1; i >= 0; i--) {
int index = temp.getLowestSetBit();
this.ks[i] = index;
temp = temp.clearBit(index);
}
}
示例2: lucasSequence
import java.math.BigInteger; //导入方法依赖的package包/类
private static BigInteger[] lucasSequence(BigInteger p, BigInteger P, BigInteger Q, BigInteger k) {
int n = k.bitLength();
int s = k.getLowestSetBit();
BigInteger Uh = BigInteger.ONE;
BigInteger Vl = TWO;
BigInteger Vh = P;
BigInteger Ql = BigInteger.ONE;
BigInteger Qh = BigInteger.ONE;
for (int j = n - 1; j >= s + 1; --j) {
Ql = Ql.multiply(Qh).mod(p);
if (k.testBit(j)) {
Qh = Ql.multiply(Q).mod(p);
Uh = Uh.multiply(Vh).mod(p);
Vl = Vh.multiply(Vl).subtract(P.multiply(Ql)).mod(p);
Vh = Vh.multiply(Vh).subtract(Qh.shiftLeft(1)).mod(p);
} else {
Qh = Ql;
Uh = Uh.multiply(Vl).subtract(Ql).mod(p);
Vh = Vh.multiply(Vl).subtract(P.multiply(Ql)).mod(p);
Vl = Vl.multiply(Vl).subtract(Ql.shiftLeft(1)).mod(p);
}
}
Ql = Ql.multiply(Qh).mod(p);
Qh = Ql.multiply(Q).mod(p);
Uh = Uh.multiply(Vl).subtract(Ql).mod(p);
Vl = Vh.multiply(Vl).subtract(P.multiply(Ql)).mod(p);
Ql = Ql.multiply(Qh).mod(p);
for (int j = 1; j <= s; ++j) {
Uh = Uh.multiply(Vl).mod(p);
Vl = Vl.multiply(Vl).subtract(Ql.shiftLeft(1)).mod(p);
Ql = Ql.multiply(Ql).mod(p);
}
return new BigInteger[] { Uh, Vl };
}
示例3: bigToDouble
import java.math.BigInteger; //导入方法依赖的package包/类
static double bigToDouble(BigInteger x) {
// This is an extremely fast implementation of BigInteger.doubleValue(). JDK patch pending.
BigInteger absX = x.abs();
int exponent = absX.bitLength() - 1;
// exponent == floor(log2(abs(x)))
if (exponent < Long.SIZE - 1) {
return x.longValue();
} else if (exponent > MAX_EXPONENT) {
return x.signum() * POSITIVE_INFINITY;
}
/*
* We need the top SIGNIFICAND_BITS + 1 bits, including the "implicit" one bit. To make
* rounding easier, we pick out the top SIGNIFICAND_BITS + 2 bits, so we have one to help us
* round up or down. twiceSignifFloor will contain the top SIGNIFICAND_BITS + 2 bits, and
* signifFloor the top SIGNIFICAND_BITS + 1.
*
* It helps to consider the real number signif = absX * 2^(SIGNIFICAND_BITS - exponent).
*/
int shift = exponent - SIGNIFICAND_BITS - 1;
long twiceSignifFloor = absX.shiftRight(shift).longValue();
long signifFloor = twiceSignifFloor >> 1;
signifFloor &= SIGNIFICAND_MASK; // remove the implied bit
/*
* We round up if either the fractional part of signif is strictly greater than 0.5 (which is
* true if the 0.5 bit is set and any lower bit is set), or if the fractional part of signif is
* >= 0.5 and signifFloor is odd (which is true if both the 0.5 bit and the 1 bit are set).
*/
boolean increment = (twiceSignifFloor & 1) != 0
&& ((signifFloor & 1) != 0 || absX.getLowestSetBit() < shift);
long signifRounded = increment ? signifFloor + 1 : signifFloor;
long bits = (long) ((exponent + EXPONENT_BIAS)) << SIGNIFICAND_BITS;
bits += signifRounded;
/*
* If signifRounded == 2^53, we'd need to set all of the significand bits to zero and add 1 to
* the exponent. This is exactly the behavior we get from just adding signifRounded to bits
* directly. If the exponent is MAX_DOUBLE_EXPONENT, we round up (correctly) to
* Double.POSITIVE_INFINITY.
*/
bits |= x.signum() & SIGN_MASK;
return longBitsToDouble(bits);
}
示例4: isPowerOfTwo
import java.math.BigInteger; //导入方法依赖的package包/类
/**
* Returns {@code true} if {@code x} represents a power of two.
*/
public static boolean isPowerOfTwo(BigInteger x) {
checkNotNull(x);
return x.signum() > 0 && x.getLowestSetBit() == x.bitLength() - 1;
}
示例5: lucasSequence
import java.math.BigInteger; //导入方法依赖的package包/类
private static BigInteger[] lucasSequence(
BigInteger p,
BigInteger P,
BigInteger Q,
BigInteger k)
{
int n = k.bitLength();
int s = k.getLowestSetBit();
BigInteger Uh = ECConstants.ONE;
BigInteger Vl = ECConstants.TWO;
BigInteger Vh = P;
BigInteger Ql = ECConstants.ONE;
BigInteger Qh = ECConstants.ONE;
for (int j = n - 1; j >= s + 1; --j)
{
Ql = Ql.multiply(Qh).mod(p);
if (k.testBit(j))
{
Qh = Ql.multiply(Q).mod(p);
Uh = Uh.multiply(Vh).mod(p);
Vl = Vh.multiply(Vl).subtract(P.multiply(Ql)).mod(p);
Vh = Vh.multiply(Vh).subtract(Qh.shiftLeft(1)).mod(p);
}
else
{
Qh = Ql;
Uh = Uh.multiply(Vl).subtract(Ql).mod(p);
Vh = Vh.multiply(Vl).subtract(P.multiply(Ql)).mod(p);
Vl = Vl.multiply(Vl).subtract(Ql.shiftLeft(1)).mod(p);
}
}
Ql = Ql.multiply(Qh).mod(p);
Qh = Ql.multiply(Q).mod(p);
Uh = Uh.multiply(Vl).subtract(Ql).mod(p);
Vl = Vh.multiply(Vl).subtract(P.multiply(Ql)).mod(p);
Ql = Ql.multiply(Qh).mod(p);
for (int j = 1; j <= s; ++j)
{
Uh = Uh.multiply(Vl).mod(p);
Vl = Vl.multiply(Vl).subtract(Ql.shiftLeft(1)).mod(p);
Ql = Ql.multiply(Ql).mod(p);
}
return new BigInteger[]{ Uh, Vl };
}
示例6: bigToDouble
import java.math.BigInteger; //导入方法依赖的package包/类
static double bigToDouble(BigInteger x) {
// This is an extremely fast implementation of BigInteger.doubleValue(). JDK patch pending.
BigInteger absX = x.abs();
int exponent = absX.bitLength() - 1;
// exponent == floor(log2(abs(x)))
if (exponent < Long.SIZE - 1) {
return x.longValue();
} else if (exponent > MAX_EXPONENT) {
return x.signum() * POSITIVE_INFINITY;
}
/*
* We need the top SIGNIFICAND_BITS + 1 bits, including the "implicit" one bit. To make rounding
* easier, we pick out the top SIGNIFICAND_BITS + 2 bits, so we have one to help us round up or
* down. twiceSignifFloor will contain the top SIGNIFICAND_BITS + 2 bits, and signifFloor the
* top SIGNIFICAND_BITS + 1.
*
* It helps to consider the real number signif = absX * 2^(SIGNIFICAND_BITS - exponent).
*/
int shift = exponent - SIGNIFICAND_BITS - 1;
long twiceSignifFloor = absX.shiftRight(shift).longValue();
long signifFloor = twiceSignifFloor >> 1;
signifFloor &= SIGNIFICAND_MASK; // remove the implied bit
/*
* We round up if either the fractional part of signif is strictly greater than 0.5 (which is
* true if the 0.5 bit is set and any lower bit is set), or if the fractional part of signif is
* >= 0.5 and signifFloor is odd (which is true if both the 0.5 bit and the 1 bit are set).
*/
boolean increment =
(twiceSignifFloor & 1) != 0 && ((signifFloor & 1) != 0 || absX.getLowestSetBit() < shift);
long signifRounded = increment ? signifFloor + 1 : signifFloor;
long bits = (long) ((exponent + EXPONENT_BIAS)) << SIGNIFICAND_BITS;
bits += signifRounded;
/*
* If signifRounded == 2^53, we'd need to set all of the significand bits to zero and add 1 to
* the exponent. This is exactly the behavior we get from just adding signifRounded to bits
* directly. If the exponent is MAX_DOUBLE_EXPONENT, we round up (correctly) to
* Double.POSITIVE_INFINITY.
*/
bits |= x.signum() & SIGN_MASK;
return longBitsToDouble(bits);
}
示例7: FastLucasSequence
import java.math.BigInteger; //导入方法依赖的package包/类
private static BigInteger[] FastLucasSequence(final BigInteger p, final BigInteger P, final BigInteger Q,
final BigInteger k) {
final int n = k.bitLength();
final int s = k.getLowestSetBit();
BigInteger Uh = BigInteger.ONE;
BigInteger Vl = BigInteger.valueOf(2);
BigInteger Vh = P;
BigInteger Ql = BigInteger.ONE;
BigInteger Qh = BigInteger.ONE;
for (int j = n - 1; j >= (s + 1); --j) {
Ql = (Ql.multiply(Qh)).mod(p);
if (k.testBit(j)) {
Qh = (Ql.multiply(Q)).mod(p);
Uh = (Uh.multiply(Vh)).mod(p);
Vl = (Vh.multiply(Vl).subtract(P.multiply(Ql))).mod(p);
Vh = ((Vh.multiply(Vh)).subtract((Qh.shiftLeft(1)))).mod(p);
} else {
Qh = Ql;
Uh = (Uh.multiply(Vl).subtract(Ql)).mod(p);
Vh = (Vh.multiply(Vl).subtract(P.multiply(Ql))).mod(p);
Vl = ((Vl.multiply(Vl)).subtract((Ql.shiftLeft(1)))).mod(p);
}
}
Ql = Ql.multiply(Qh).mod(p);
Qh = Ql.multiply(Q).mod(p);
Uh = Uh.multiply(Vl).subtract(Ql).mod(p);
Vl = Vh.multiply(Vl).subtract(P.multiply(Ql)).mod(p);
Ql = Ql.multiply(Qh).mod(p);
for (int j = 1; j <= s; ++j) {
Uh = Uh.multiply(Vl).multiply(p);
Vl = ((Vl.multiply(Vl)).subtract((Ql.shiftLeft(1)))).mod(p);
Ql = (Ql.multiply(Ql)).mod(p);
}
return new BigInteger[] { Uh, Vl };
}
示例8: isEven
import java.math.BigInteger; //导入方法依赖的package包/类
public static boolean isEven(final BigInteger number) {
return number.getLowestSetBit() != 0;
}