java.math.BigInteger.shiftRight()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(9.1k)|赞(0)|评价(0)|浏览(132)

本文整理了Java中java.math.BigInteger.shiftRight()方法的一些代码示例,展示了BigInteger.shiftRight()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BigInteger.shiftRight()方法的具体详情如下:
包路径:java.math.BigInteger
类名称:BigInteger
方法名:shiftRight

BigInteger.shiftRight介绍

[英]Returns a BigInteger whose value is this >> n. For negative arguments, the result is also negative. The shift distance may be negative which means that this is shifted left.

Implementation Note: Usage of this method on negative values is not recommended as the current implementation is not efficient.
[中]

代码示例

代码示例来源:origin: org.apache.poi/poi

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: google/guava

sqrt0 = sqrtApproxWithDoubles(x.shiftRight(shift)).shiftLeft(shift >> 1);
BigInteger sqrt1 = sqrt0.add(x.divide(sqrt0)).shiftRight(1);
if (sqrt0.equals(sqrt1)) {
 return sqrt0;
 sqrt1 = sqrt0.add(x.divide(sqrt0)).shiftRight(1);
} while (sqrt1.compareTo(sqrt0) < 0);
return sqrt0;

代码示例来源: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: Graylog2/graylog2-server

private void calculate() throws UnknownHostException {
  final int targetSize;
  final BigInteger mask;
  if (inetAddress.getAddress().length == 4) {
    targetSize = 4;
    mask = (new BigInteger(1, MASK_IPV4)).not().shiftRight(prefixLength);
  } else {
    targetSize = 16;
    mask = (new BigInteger(1, MASK_IPV6)).not().shiftRight(prefixLength);
  }
  final BigInteger ipVal = new BigInteger(1, inetAddress.getAddress());
  final BigInteger startIp = ipVal.and(mask);
  final BigInteger endIp = startIp.add(mask.not());
  final byte[] startIpArr = toBytes(startIp.toByteArray(), targetSize);
  final byte[] endIpArr = toBytes(endIp.toByteArray(), targetSize);
  this.startAddress = InetAddress.getByAddress(startIpArr);
  this.endAddress = InetAddress.getByAddress(endIpArr);
}

代码示例来源:origin: robovm/robovm

mantissa = quotAndRem[0].shiftLeft(2).add(
      BigInteger.valueOf((compRem * (compRem + 3)) / 2 + 1));
  exponent -= 2;
discardedSize = mantissa.bitLength() - 54;
if (discardedSize > 0) {// (n > 54)
  bits = mantissa.shiftRight(discardedSize).longValue();
  tempBits = bits;

代码示例来源: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: prestodb/presto

sqrt0 = sqrtApproxWithDoubles(x.shiftRight(shift)).shiftLeft(shift >> 1);
BigInteger sqrt1 = sqrt0.add(x.divide(sqrt0)).shiftRight(1);
if (sqrt0.equals(sqrt1)) {
 return sqrt0;
 sqrt1 = sqrt0.add(x.divide(sqrt0)).shiftRight(1);
} while (sqrt1.compareTo(sqrt0) < 0);
return sqrt0;

代码示例来源:origin: stackoverflow.com

return new BigFraction(numerator.multiply(f.denominator).add(denominator.multiply(f.numerator)),
            denominator.multiply(f.denominator));
return new BigFraction(numerator.add(denominator.multiply(b)),
            denominator, true);
BigInteger tmpDen = denominator.shiftRight(twos); // x / 2^n === x >> n
int precision = Math.max(numerator.bitLength(), denominator.bitLength());
precision = (int)Math.ceil(precision / LG10);

代码示例来源:origin: google/guava

static double bigToDouble(BigInteger x) {
 int exponent = absX.bitLength() - 1;
 long twiceSignifFloor = absX.shiftRight(shift).longValue();
 long signifFloor = twiceSignifFloor >> 1;

代码示例来源:origin: google/j2objc

sqrt0 = sqrtApproxWithDoubles(x.shiftRight(shift)).shiftLeft(shift >> 1);
BigInteger sqrt1 = sqrt0.add(x.divide(sqrt0)).shiftRight(1);
if (sqrt0.equals(sqrt1)) {
 return sqrt0;
 sqrt1 = sqrt0.add(x.divide(sqrt0)).shiftRight(1);
} while (sqrt1.compareTo(sqrt0) < 0);
return sqrt0;

代码示例来源:origin: stackoverflow.com

public static BigInteger sqrt(BigInteger x) {
  BigInteger div = BigInteger.ZERO.setBit(x.bitLength()/2);
  BigInteger div2 = div;
  // Loop until we hit the same value twice in a row, or wind
  // up alternating.
  for(;;) {
    BigInteger y = div.add(x.divide(div)).shiftRight(1);
    if (y.equals(div) || y.equals(div2))
      return y;
    div2 = div;
    div = y;
  }
}

代码示例来源:origin: stackoverflow.com

private static final double LOG2 = Math.log(2.0);

/**
 * Computes the natural logarithm of a BigInteger. Works for really big
 * integers (practically unlimited)
 * 
 * @param val Argument, positive integer
 * @return Natural logarithm, as in <tt>Math.log()</tt>
 */
public static double logBigInteger(BigInteger val) {
  int blex = val.bitLength() - 1022; // any value in 60..1023 is ok
  if (blex > 0)
    val = val.shiftRight(blex);
  double res = Math.log(val.doubleValue());
  return blex > 0 ? res + blex * LOG2 : res;
}

代码示例来源:origin: wildfly/wildfly

sqrt0 = sqrtApproxWithDoubles(x.shiftRight(shift)).shiftLeft(shift >> 1);
BigInteger sqrt1 = sqrt0.add(x.divide(sqrt0)).shiftRight(1);
if (sqrt0.equals(sqrt1)) {
 return sqrt0;
 sqrt1 = sqrt0.add(x.divide(sqrt0)).shiftRight(1);
} while (sqrt1.compareTo(sqrt0) < 0);
return sqrt0;

代码示例来源:origin: com.madgag.spongycastle/core

private static BigInteger isqrt(BigInteger x)
{
  BigInteger g0 = x.shiftRight(x.bitLength() / 2);
  for (;;)
  {
    BigInteger g1 = g0.add(x.divide(g0)).shiftRight(1);
    if (g1.equals(g0))
    {
      return g1;
    }
    g0 = g1;
  }
}

代码示例来源:origin: plutext/docx4j

private TenPower(int index) {
  BigInteger fivePowIndex = FIVE.pow(index);
  int bitsDueToFiveFactors = fivePowIndex.bitLength();
  int px = 80 + bitsDueToFiveFactors;
  BigInteger fx = BigInteger.ONE.shiftLeft(px).divide(fivePowIndex);
  int adj = fx.bitLength() - 80;
  _divisor = fx.shiftRight(adj);
  bitsDueToFiveFactors -= adj;
  _divisorShift = -(bitsDueToFiveFactors+index+80);
  int sc = fivePowIndex.bitLength() - 68;
  if (sc > 0) {
    _multiplierShift = index + sc;
    _multiplicand = fivePowIndex.shiftRight(sc);
  } else {
    _multiplierShift = index;
    _multiplicand = fivePowIndex;
  }
}

代码示例来源:origin: prestodb/presto

private void assertShiftRightArray8(BigInteger value, int rightShifts, boolean roundUp)
{
  BigInteger expectedResult = value.shiftRight(rightShifts);
  if (roundUp) {
    expectedResult = expectedResult.add(BigInteger.ONE);
  }
  int[] ints = toInt8Array(value);
  Slice result = unscaledDecimal();
  shiftRightArray8(ints, rightShifts, result);
  assertEquals(decodeUnscaledValue(result), expectedResult);
}

代码示例来源:origin: redfish64/TinyTravelTracker

private static BigInteger isqrt(BigInteger x)
{
  BigInteger g0 = x.shiftRight(x.bitLength() / 2);
  for (;;)
  {
    BigInteger g1 = g0.add(x.divide(g0)).shiftRight(1);
    if (g1.equals(g0))
    {
      return g1;
    }
    g0 = g1;
  }
}

代码示例来源:origin: org.apache.poi/poi

private TenPower(int index) {
  BigInteger fivePowIndex = FIVE.pow(index);
  int bitsDueToFiveFactors = fivePowIndex.bitLength();
  int px = 80 + bitsDueToFiveFactors;
  BigInteger fx = BigInteger.ONE.shiftLeft(px).divide(fivePowIndex);
  int adj = fx.bitLength() - 80;
  _divisor = fx.shiftRight(adj);
  bitsDueToFiveFactors -= adj;
  _divisorShift = -(bitsDueToFiveFactors+index+80);
  int sc = fivePowIndex.bitLength() - 68;
  if (sc > 0) {
    _multiplierShift = index + sc;
    _multiplicand = fivePowIndex.shiftRight(sc);
  } else {
    _multiplierShift = index;
    _multiplicand = fivePowIndex;
  }
}

代码示例来源:origin: prestodb/presto

@Test
public void testShiftRight()
{
  assertShiftRight(unscaledDecimal(0), 0, true, unscaledDecimal(0));
  assertShiftRight(unscaledDecimal(0), 33, true, unscaledDecimal(0));
  assertShiftRight(unscaledDecimal(1), 1, true, unscaledDecimal(1));
  assertShiftRight(unscaledDecimal(-4), 1, true, unscaledDecimal(-2));
  assertShiftRight(unscaledDecimal(1L << 32), 32, true, unscaledDecimal(1));
  assertShiftRight(unscaledDecimal(1L << 31), 32, true, unscaledDecimal(1));
  assertShiftRight(unscaledDecimal(1L << 31), 32, false, unscaledDecimal(0));
  assertShiftRight(unscaledDecimal(3L << 33), 34, true, unscaledDecimal(2));
  assertShiftRight(unscaledDecimal(3L << 33), 34, false, unscaledDecimal(1));
  assertShiftRight(unscaledDecimal(BigInteger.valueOf(0x7FFFFFFFFFFFFFFFL).setBit(63).setBit(64)), 1, true, unscaledDecimal(BigInteger.ONE.shiftLeft(64)));
  assertShiftRight(MAX_DECIMAL, 1, true, unscaledDecimal(MAX_DECIMAL_UNSCALED_VALUE.shiftRight(1).add(BigInteger.ONE)));
  assertShiftRight(MIN_DECIMAL, 1, true, unscaledDecimal(MAX_DECIMAL_UNSCALED_VALUE.shiftRight(1).add(BigInteger.ONE).negate()));
  assertShiftRight(MAX_DECIMAL, 66, true, unscaledDecimal(MAX_DECIMAL_UNSCALED_VALUE.shiftRight(66).add(BigInteger.ONE)));
}

代码示例来源:origin: org.bouncycastle/bcprov-debug-jdk15on

private static BigInteger isqrt(BigInteger x)
{
  BigInteger g0 = x.shiftRight(x.bitLength() / 2);
  for (;;)
  {
    BigInteger g1 = g0.add(x.divide(g0)).shiftRight(1);
    if (g1.equals(g0))
    {
      return g1;
    }
    g0 = g1;
  }
}

相关文章