本文整理了Java中java.math.BigInteger.bitLength()
方法的一些代码示例,展示了BigInteger.bitLength()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BigInteger.bitLength()
方法的具体详情如下:
包路径:java.math.BigInteger
类名称:BigInteger
方法名:bitLength
[英]One plus the bitLength of this BigInteger. Zeros means unitialized. (either value is acceptable).
[中]一加上这个大整数的位长度。零表示单位化。(任一值均可接受)。
代码示例来源:origin: google/guava
/** 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;
}
代码示例来源:origin: plutext/docx4j
public boolean isBelowMaxRep() {
int sc = _significand.bitLength() - C_64;
return _significand.compareTo(BI_MAX_BASE.shiftLeft(sc)) < 0;
}
public boolean isAboveMinRep() {
代码示例来源:origin: plutext/docx4j
private void mulShift(BigInteger multiplicand, int multiplierShift) {
_significand = _significand.multiply(multiplicand);
_binaryExponent += multiplierShift;
// check for too much precision
int sc = (_significand.bitLength() - MIN_PRECISION) & 0xFFFFFFE0;
// mask makes multiples of 32 which optimises BigInteger.shiftRight
if (sc > 0) {
// no need to round because we have at least 8 bits of extra precision
_significand = _significand.shiftRight(sc);
_binaryExponent += sc;
}
}
代码示例来源:origin: stackoverflow.com
int nlen = n.bitLength();
BigInteger nm1 = n.subtract(BigInteger.ONE);
BigInteger r, s;
do {
s = new BigInteger(nlen + 100, rnd);
r = s.mod(n);
} while (s.subtract(r).add(nm1).bitLength() >= nlen + 100);
// result is in 'r'
代码示例来源:origin: org.apache.ant/ant
private static long parseBinaryBigInteger(final byte[] buffer,
final int offset,
final int length,
final boolean negative) {
final byte[] remainder = new byte[length - 1];
System.arraycopy(buffer, offset + 1, remainder, 0, length - 1);
BigInteger val = new BigInteger(remainder);
if (negative) {
// 2's complement
val = val.add(BigInteger.valueOf(-1)).not();
}
if (val.bitLength() > 63) {
throw new IllegalArgumentException(String.format(
"At offset %d, %d byte binary number exceeds maximum signed long value",
offset, length));
}
return negative ? -val.longValue() : val.longValue();
}
代码示例来源:origin: stackoverflow.com
public BigInteger nextRandomBigInteger(BigInteger n) {
Random rand = new Random();
BigInteger result = new BigInteger(n.bitLength(), rand);
while( result.compareTo(n) >= 0 ) {
result = new BigInteger(n.bitLength(), rand);
}
return result;
}
代码示例来源:origin: stackoverflow.com
public static BigInteger random(Random rand, BigInteger minValue, BigInteger maxValue) {
BigInteger range = maxValue.subtract(minValue).add(BigInteger.ONE);
int bits = range.bitLength();
BigInteger ret;
do {
ret = new BigInteger(bits, rand);
} while(ret.compareTo(range) >= 0);
return ret.add(minValue);
}
代码示例来源:origin: i2p/i2p.i2p
/** @return 0 to max-1 or -1 for us */
public int getRange(T key) {
Integer rv;
synchronized (_distanceCache) {
rv = _distanceCache.get(key);
if (rv == null) {
// easy way when _bValue == 1
//rv = Integer.valueOf(_bigUs.xor(new BigInteger(1, key.getData())).bitLength() - 1);
BigInteger xor = _bigUs.xor(new BigInteger(1, key.getData()));
int range = xor.bitLength() - 1;
if (_bValue > 1) {
int toShift = range + 1 - _bValue;
int highbit = range;
range <<= _bValue - 1;
if (toShift >= 0) {
int extra = xor.clearBit(highbit).shiftRight(toShift).intValue();
range += extra;
//Log log = I2PAppContext.getGlobalContext().logManager().getLog(KBucketSet.class);
//if (log.shouldLog(Log.DEBUG))
// log.debug("highbit " + highbit + " toshift " + toShift + " extra " + extra + " new " + range);
}
}
rv = Integer.valueOf(range);
_distanceCache.put(key, rv);
}
}
return rv.intValue();
}
代码示例来源:origin: plantuml/plantuml
public BigDecimal eval(List<? extends Number> parameters) {
assertNotNull(parameters.get(0));
/*
* From The Java Programmers Guide To numerical Computing (Ronald Mak, 2003)
*/
BigDecimal x = (BigDecimal) parameters.get(0);
if (x.compareTo(BigDecimal.ZERO) == 0) {
return new BigDecimal(0);
}
if (x.signum() < 0) {
throw new ExpressionException("Argument to SQRT() function must not be negative");
}
BigInteger n = x.movePointRight(mc.getPrecision() << 1).toBigInteger();
int bits = (n.bitLength() + 1) >> 1;
BigInteger ix = n.shiftRight(bits);
BigInteger ixPrev;
BigInteger test;
do {
ixPrev = ix;
ix = ix.add(n.divide(ix)).shiftRight(1);
// Give other threads a chance to work;
Thread.yield();
test = ix.subtract(ixPrev).abs();
} while (test.compareTo(BigInteger.ZERO) != 0 && test.compareTo(BigInteger.ONE) != 0);
return new BigDecimal(ix, mc.getPrecision());
}
});
代码示例来源:origin: robovm/robovm
if (this.a.signum() < 0 || this.a.compareTo(p) >= 0) {
throw new IllegalArgumentException("the a is not in the field");
if (this.b.signum() < 0 || this.b.compareTo(p) >= 0) {
throw new IllegalArgumentException("the b is not in the field");
if (!(this.a.bitLength() <= fieldSizeInBits)) {
throw new IllegalArgumentException("the a is not in the field");
if (!(this.b.bitLength() <= fieldSizeInBits)) {
throw new IllegalArgumentException("the b is not in the field");
代码示例来源:origin: google/guava
int logFloor = x.bitLength() - 1;
switch (mode) {
case UNNECESSARY:
if (logFloor < SQRT2_PRECOMPUTE_THRESHOLD) {
BigInteger halfPower =
SQRT2_PRECOMPUTED_BITS.shiftRight(SQRT2_PRECOMPUTE_THRESHOLD - logFloor);
if (x.compareTo(halfPower) <= 0) {
return logFloor;
} else {
int logX2Floor = x2.bitLength() - 1;
return (logX2Floor < 2 * logFloor + 1) ? logFloor : logFloor + 1;
代码示例来源:origin: pholser/junit-quickcheck
public static BigInteger choose(
SourceOfRandomness random,
BigInteger min,
BigInteger max) {
BigInteger range = max.subtract(min).add(BigInteger.ONE);
BigInteger generated;
do {
generated = random.nextBigInteger(range.bitLength());
} while (generated.compareTo(range) >= 0);
return generated.add(min);
}
代码示例来源:origin: org.apache.commons/commons-lang3
final BigInteger t = isAdd ? uvp.add(upv) : uvp.subtract(upv);
if (w.bitLength() > 31) {
throw new ArithmeticException("overflow: numerator too large after multiply");
代码示例来源:origin: google/guava
static double bigToDouble(BigInteger x) {
int exponent = absX.bitLength() - 1;
return x.signum() * POSITIVE_INFINITY;
long twiceSignifFloor = absX.shiftRight(shift).longValue();
long signifFloor = twiceSignifFloor >> 1;
bits |= x.signum() & SIGN_MASK;
return longBitsToDouble(bits);
代码示例来源:origin: pholser/junit-quickcheck
@Override protected void primeSourceOfRandomness() {
numberOfBits = max.movePointRight(3).subtract(min.movePointRight(3)).toBigInteger().bitLength();
when(randomForParameterGenerator.nextBigInteger(numberOfBits))
.thenReturn(new BigInteger("2").pow(numberOfBits).subtract(ONE))
.thenReturn(ONE)
.thenReturn(TEN)
.thenReturn(ZERO)
.thenReturn(new BigInteger("234234234234"));
}
代码示例来源:origin: org.apache.commons/commons-compress
private static long parseBinaryBigInteger(final byte[] buffer,
final int offset,
final int length,
final boolean negative) {
final byte[] remainder = new byte[length - 1];
System.arraycopy(buffer, offset + 1, remainder, 0, length - 1);
BigInteger val = new BigInteger(remainder);
if (negative) {
// 2's complement
val = val.add(BigInteger.valueOf(-1)).not();
}
if (val.bitLength() > 63) {
throw new IllegalArgumentException("At offset " + offset + ", "
+ length + " byte binary number"
+ " exceeds maximum signed long"
+ " value");
}
return negative ? -val.longValue() : val.longValue();
}
代码示例来源:origin: stackoverflow.com
X9ECParameters x9 = NISTNamedCurves.getByName("P-224"); // or whatever curve you want to use
ECPoint g = x9.getG();
BigInteger n = x9.getN();
int nBitLength = n.bitLength();
BigInteger x;
do
{
x = new BigInteger(nBitLength, random);
}
while (x.equals(ZERO) || (x.compareTo(n) >= 0));
ECPoint randomPoint = g.multiply(x);
代码示例来源:origin: stackoverflow.com
public static int myRandom(int min, int max, Random r){
if (max <= min)
throw new RuntimeException("max value must be greater than min value: max="+max +", min="+min);
BigInteger maxB = BigInteger.valueOf(max);
BigInteger minB = BigInteger.valueOf(min);
BigInteger range = maxB.subtract(minB);
do{
BigInteger result = new BigInteger(range.bitLength(), r);
if (result.compareTo(range)<=0)
return result.add(minB).intValueExact();
}while(true);
}
代码示例来源:origin: com.madgag.spongycastle/core
private BigInteger bitsToInt(byte[] t)
{
BigInteger v = new BigInteger(1, t);
if (t.length * 8 > n.bitLength())
{
v = v.shiftRight(t.length * 8 - n.bitLength());
}
return v;
}
}
代码示例来源:origin: prestodb/presto
private static void writeBigInteger(OutputStream output, BigInteger value)
throws IOException
{
// encode the signed number as a positive integer
value = value.shiftLeft(1);
int sign = value.signum();
if (sign < 0) {
value = value.negate();
value = value.subtract(ONE);
}
int length = value.bitLength();
while (true) {
long lowBits = value.longValue() & 0x7fffffffffffffffL;
length -= 63;
// write out the next 63 bits worth of data
for (int i = 0; i < 9; ++i) {
// if this is the last byte, leave the high bit off
if (length <= 0 && (lowBits & ~0x7f) == 0) {
output.write((byte) lowBits);
return;
}
else {
output.write((byte) (0x80 | (lowBits & 0x7f)));
lowBits >>>= 7;
}
}
value = value.shiftRight(63);
}
}
}
内容来源于网络,如有侵权,请联系作者删除!