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

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

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

BigInteger.testBit介绍

[英]Tests whether the bit at position n in this is set. The result is equivalent to this & pow(2, n) != 0.

Implementation Note: Usage of this method is not recommended as the current implementation is not efficient.
[中]测试是否设置了该位置n处的位。结果与此等价&pow(2,n)!=0
实施说明:不建议使用此方法,因为当前的实施效率不高。

代码示例

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

/**
 * Get value as eight bytes in big endian byte order.
 * @param value the value to convert
 * @return value as eight bytes in big endian byte order
 */
public static byte[] getBytes(BigInteger value) {
  byte[] result = new byte[8];
  long val = value.longValue();
  result[0] = (byte) ((val & BYTE_MASK));
  result[BYTE_1] = (byte) ((val & BYTE_1_MASK) >> BYTE_1_SHIFT);
  result[BYTE_2] = (byte) ((val & BYTE_2_MASK) >> BYTE_2_SHIFT);
  result[BYTE_3] = (byte) ((val & BYTE_3_MASK) >> BYTE_3_SHIFT);
  result[BYTE_4] = (byte) ((val & BYTE_4_MASK) >> BYTE_4_SHIFT);
  result[BYTE_5] = (byte) ((val & BYTE_5_MASK) >> BYTE_5_SHIFT);
  result[BYTE_6] = (byte) ((val & BYTE_6_MASK) >> BYTE_6_SHIFT);
  result[BYTE_7] = (byte) ((val & BYTE_7_MASK) >> BYTE_7_SHIFT);
  if (value.testBit(LEFTMOST_BIT_SHIFT)) {
    result[BYTE_7] |= LEFTMOST_BIT;
  }
  return result;
}

代码示例来源:origin: org.apache.commons/commons-compress

/**
 * Get value as eight bytes in big endian byte order.
 * @param value the value to convert
 * @return value as eight bytes in big endian byte order
 */
public static byte[] getBytes(final BigInteger value) {
  final byte[] result = new byte[8];
  final long val = value.longValue();
  result[0] = (byte) ((val & BYTE_MASK));
  result[BYTE_1] = (byte) ((val & BYTE_1_MASK) >> BYTE_1_SHIFT);
  result[BYTE_2] = (byte) ((val & BYTE_2_MASK) >> BYTE_2_SHIFT);
  result[BYTE_3] = (byte) ((val & BYTE_3_MASK) >> BYTE_3_SHIFT);
  result[BYTE_4] = (byte) ((val & BYTE_4_MASK) >> BYTE_4_SHIFT);
  result[BYTE_5] = (byte) ((val & BYTE_5_MASK) >> BYTE_5_SHIFT);
  result[BYTE_6] = (byte) ((val & BYTE_6_MASK) >> BYTE_6_SHIFT);
  result[BYTE_7] = (byte) ((val & BYTE_7_MASK) >> BYTE_7_SHIFT);
  if (value.testBit(LEFTMOST_BIT_SHIFT)) {
    result[BYTE_7] |= LEFTMOST_BIT;
  }
  return result;
}

代码示例来源:origin: alibaba/mdrill

/**
 * Get value as eight bytes in big endian byte order.
 * @param value the value to convert
 * @return value as eight bytes in big endian byte order
 */
public static byte[] getBytes(BigInteger value) {
  byte[] result = new byte[8];
  long val = value.longValue();
  result[0] = (byte) ((val & BYTE_MASK));
  result[BYTE_1] = (byte) ((val & BYTE_1_MASK) >> BYTE_1_SHIFT);
  result[BYTE_2] = (byte) ((val & BYTE_2_MASK) >> BYTE_2_SHIFT);
  result[BYTE_3] = (byte) ((val & BYTE_3_MASK) >> BYTE_3_SHIFT);
  result[BYTE_4] = (byte) ((val & BYTE_4_MASK) >> BYTE_4_SHIFT);
  result[BYTE_5] = (byte) ((val & BYTE_5_MASK) >> BYTE_5_SHIFT);
  result[BYTE_6] = (byte) ((val & BYTE_6_MASK) >> BYTE_6_SHIFT);
  result[BYTE_7] = (byte) ((val & BYTE_7_MASK) >> BYTE_7_SHIFT);
  if (value.testBit(LEFTMOST_BIT_SHIFT)) {
    result[BYTE_7] |= LEFTMOST_BIT;
  }
  return result;
}

代码示例来源:origin: com.h2database/h2

@Override
public Value divide(Value v) {
  ValueDecimal dec = (ValueDecimal) v;
  if (dec.value.signum() == 0) {
    throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL());
  }
  BigDecimal bd = value.divide(dec.value,
      value.scale() + DIVIDE_SCALE_ADD,
      BigDecimal.ROUND_HALF_DOWN);
  if (bd.signum() == 0) {
    bd = BigDecimal.ZERO;
  } else if (bd.scale() > 0) {
    if (!bd.unscaledValue().testBit(0)) {
      bd = bd.stripTrailingZeros();
    }
  }
  return ValueDecimal.get(bd);
}

代码示例来源:origin: ethereum/ethereumj

Fp12 cyclotomicExp(BigInteger pow) {
  Fp12 res = _1;
  for (int i = pow.bitLength() - 1; i >=0; i--) {
    res = res.cyclotomicSquared();
    if (pow.testBit(i)) {
      res = res.mul(this);
    }
  }
  return res;
}

代码示例来源:origin: org.apache.commons/commons-math3

/**
 * Raise a BigInteger to a BigInteger power.
 *
 * @param k Number to raise.
 * @param e Exponent (must be positive or zero).
 * @return k<sup>e</sup>
 * @throws NotPositiveException if {@code e < 0}.
 */
public static BigInteger pow(final BigInteger k, BigInteger e) throws NotPositiveException {
  if (e.compareTo(BigInteger.ZERO) < 0) {
    throw new NotPositiveException(LocalizedFormats.EXPONENT, e);
  }
  BigInteger result = BigInteger.ONE;
  BigInteger k2p    = k;
  while (!BigInteger.ZERO.equals(e)) {
    if (e.testBit(0)) {
      result = result.multiply(k2p);
    }
    k2p = k2p.multiply(k2p);
    e = e.shiftRight(1);
  }
  return result;
}

代码示例来源:origin: org.mongodb/mongo-java-driver

private Decimal128(final BigDecimal initialValue, final boolean isNegative) {
  long localHigh = 0;
  long localLow = 0;
  BigDecimal value = clampAndRound(initialValue);
  long exponent = -value.scale();
  if ((exponent < MIN_EXPONENT) || (exponent > MAX_EXPONENT)) {
    throw new AssertionError("Exponent is out of range for Decimal128 encoding: " + exponent); }
  if (value.unscaledValue().bitLength() > MAX_BIT_LENGTH) {
    throw new AssertionError("Unscaled roundedValue is out of range for Decimal128 encoding:" + value.unscaledValue());
  }
  BigInteger significand = value.unscaledValue().abs();
  int bitLength = significand.bitLength();
  for (int i = 0; i < Math.min(64, bitLength); i++) {
    if (significand.testBit(i)) {
      localLow |= 1L << i;
    }
  }
  for (int i = 64; i < bitLength; i++) {
    if (significand.testBit(i)) {
      localHigh |= 1L << (i - 64);
    }
  }
  long biasedExponent = exponent + EXPONENT_OFFSET;
  localHigh |= biasedExponent << 49;
  if (value.signum() == -1 || isNegative) {
    localHigh |= SIGN_BIT_MASK;
  }
  high = localHigh;
  low = localLow;
}

代码示例来源:origin: apache/flink

for (int bytePos = 0; bytePos < 8 && bitPos >= 0; bytePos++, bitPos--) {
  b <<= 1;
  if (record.testBit(bitPos)) {
    b |= 1;

代码示例来源:origin: lealone/Lealone

@Override
public Value divide(Value v) {
  ValueDecimal dec = (ValueDecimal) v;
  if (dec.value.signum() == 0) {
    throw DbException.get(ErrorCode.DIVISION_BY_ZERO_1, getSQL());
  }
  BigDecimal bd = value.divide(dec.value, value.scale() + DIVIDE_SCALE_ADD, BigDecimal.ROUND_HALF_DOWN);
  if (bd.signum() == 0) {
    bd = BigDecimal.ZERO;
  } else if (bd.scale() > 0) {
    if (!bd.unscaledValue().testBit(0)) {
      String s = bd.toString();
      int i = s.length() - 1;
      while (i >= 0 && s.charAt(i) == '0') {
        i--;
      }
      if (i < s.length() - 1) {
        s = s.substring(0, i + 1);
        bd = new BigDecimal(s);
      }
    }
  }
  return ValueDecimal.get(bd);
}

代码示例来源:origin: ethereum/ethereumj

public DataWord signExtend(byte k) {
  if (0 > k || k > 31)
    throw new IndexOutOfBoundsException();
  byte mask = this.sValue().testBit((k * 8) + 7) ? (byte) 0xff : 0;
  byte[] newData = this.copyData();
  for (int i = 31; i > k; i--) {
    newData[31 - i] = mask;
  }
  return new DataWord(newData);
}

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

compRem = longCompareTo(Math.abs(rem) * 2,Math.abs(divisor));
compRem = roundingBehavior(quotient.testBit(0) ? 1 : 0,
    sign * (5 + compRem), roundingMode);
compRem = roundingBehavior(quotient.testBit(0) ? 1 : 0,
    sign * (5 + compRem), roundingMode);

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

/**
 * Returns a {@code BigInteger} which has the same binary representation
 * as {@code this} but with the bit at position n set. The result is
 * equivalent to {@code this | pow(2, n)}.
 *
 * <p><b>Implementation Note:</b> Usage of this method is not recommended as
 * the current implementation is not efficient.
 *
 * @param n position where the bit in {@code this} has to be set.
 * @throws ArithmeticException if {@code n < 0}.
 */
public BigInteger setBit(int n) {
  prepareJavaRepresentation();
  if (!testBit(n)) {
    return BitLevel.flipBit(this, n);
  } else {
    return this;
  }
}

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

/**
 * Returns a {@code BigInteger} which has the same binary representation
 * as {@code this} but with the bit at position n cleared. The result is
 * equivalent to {@code this & ~pow(2, n)}.
 *
 * <p><b>Implementation Note:</b> Usage of this method is not recommended as
 * the current implementation is not efficient.
 *
 * @param n position where the bit in {@code this} has to be cleared.
 * @throws ArithmeticException if {@code n < 0}.
 */
public BigInteger clearBit(int n) {
  prepareJavaRepresentation();
  if (testBit(n)) {
    return BitLevel.flipBit(this, n);
  } else {
    return this;
  }
}

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

if ((this.rp.bitLength() != (m+1)) ||
  (rp_bc != TPB_LEN && rp_bc != PPB_LEN) ||
  (!this.rp.testBit(0) || !this.rp.testBit(m)) ) {
  throw new IllegalArgumentException("rp is invalid");

代码示例来源:origin: ethereum/ethereumj

public BN128<T> mul(BigInteger s) {
  if (s.compareTo(BigInteger.ZERO) == 0) // P * 0 = 0
    return zero();
  if (isZero()) return this; // 0 * s = 0
  BN128<T> res = zero();
  for (int i = s.bitLength() - 1; i >= 0; i--) {
    res = res.dbl();
    if (s.testBit(i)) {
      res = res.add(this);
    }
  }
  return res;
}

代码示例来源:origin: ethereum/ethereumj

private static List<EllCoeffs> calcEllCoeffs(BN128G2 base) {
  List<EllCoeffs> coeffs = new ArrayList<>();
  BN128G2 addend = base;
  // for each bit except most significant one
  for (int i = LOOP_COUNT.bitLength() - 2; i >=0; i--) {
    Precomputed doubling = flippedMillerLoopDoubling(addend);
    addend = doubling.g2;
    coeffs.add(doubling.coeffs);
    if (LOOP_COUNT.testBit(i)) {
      Precomputed addition = flippedMillerLoopMixedAddition(base, addend);
      addend = addition.g2;
      coeffs.add(addition.coeffs);
    }
  }
  BN128G2 q1 = base.mulByP();
  BN128G2 q2 = q1.mulByP();
  q2 = new BN128G2(q2.x, q2.y.negate(), q2.z) ; // q2.y = -q2.y
  Precomputed addition = flippedMillerLoopMixedAddition(q1, addend);
  addend = addition.g2;
  coeffs.add(addition.coeffs);
  addition = flippedMillerLoopMixedAddition(q2, addend);
  coeffs.add(addition.coeffs);
  return coeffs;
}

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

while (!strippedBI.testBit(0)) {

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

compRem =  roundingBehavior( integerAndFraction[0].testBit(0) ? 1 : 0,
    integerAndFraction[1].signum() * (5 + compRem),
    mc.getRoundingMode());

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

@GwtIncompatible // TODO
public void testSqrtHalfEven() {
 for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) {
  BigInteger halfEven = BigIntegerMath.sqrt(x, HALF_EVEN);
  // Now figure out what rounding mode we should behave like (it depends if FLOOR was
  // odd/even).
  boolean floorWasOdd = BigIntegerMath.sqrt(x, FLOOR).testBit(0);
  assertEquals(BigIntegerMath.sqrt(x, floorWasOdd ? HALF_UP : HALF_DOWN), halfEven);
 }
}

代码示例来源:origin: ethereum/ethereumj

private static Fp12 millerLoop(BN128G1 g1, BN128G2 g2) {
  // convert to affine coordinates
  g1 = g1.toAffine();
  g2 = g2.toAffine();
  // calculate Ell coefficients
  List<EllCoeffs> coeffs = calcEllCoeffs(g2);
  Fp12 f = Fp12._1;
  int idx = 0;
  // for each bit except most significant one
  for (int i = LOOP_COUNT.bitLength() - 2; i >=0; i--) {
    EllCoeffs c = coeffs.get(idx++);
    f = f.squared();
    f = f.mulBy024(c.ell0, g1.y.mul(c.ellVW), g1.x.mul(c.ellVV));
    if (LOOP_COUNT.testBit(i)) {
      c = coeffs.get(idx++);
      f = f.mulBy024(c.ell0, g1.y.mul(c.ellVW), g1.x.mul(c.ellVV));
    }
  }
  EllCoeffs c = coeffs.get(idx++);
  f = f.mulBy024(c.ell0, g1.y.mul(c.ellVW), g1.x.mul(c.ellVV));
  c = coeffs.get(idx);
  f = f.mulBy024(c.ell0, g1.y.mul(c.ellVW), g1.x.mul(c.ellVV));
  return f;
}

相关文章