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

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

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

BigInteger.setBit介绍

[英]Returns a BigInteger which has the same binary representation as this but with the bit at position n set. The result is equivalent to this | pow(2, n).

Implementation Note: Usage of this method is not recommended as the current implementation is not efficient.
[中]返回一个BigInteger,它具有与此相同的二进制表示形式,但位位于设置的位置n。结果相当于这个| pow(2,n)。
实施说明:不建议使用此方法,因为当前的实施效率不高。

代码示例

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

/** Returns the value of this {@code UnsignedLong} as a {@link BigInteger}. */
public BigInteger bigIntegerValue() {
 BigInteger bigInt = BigInteger.valueOf(value & UNSIGNED_MASK);
 if (value < 0) {
  bigInt = bigInt.setBit(Long.SIZE - 1);
 }
 return bigInt;
}

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

/** Returns the value of this {@code UnsignedLong} as a {@link BigInteger}. */
public BigInteger bigIntegerValue() {
 BigInteger bigInt = BigInteger.valueOf(value & UNSIGNED_MASK);
 if (value < 0) {
  bigInt = bigInt.setBit(Long.SIZE - 1);
 }
 return bigInt;
}

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

/**
 * Returns the smallest power of two greater than or equal to {@code x}. This is equivalent to
 * {@code BigInteger.valueOf(2).pow(log2(x, CEILING))}.
 *
 * @throws IllegalArgumentException if {@code x <= 0}
 * @since 20.0
 */
@Beta
public static BigInteger ceilingPowerOfTwo(BigInteger x) {
 return BigInteger.ZERO.setBit(log2(x, RoundingMode.CEILING));
}

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

/**
 * Returns the largest power of two less than or equal to {@code x}. This is equivalent to {@code
 * BigInteger.valueOf(2).pow(log2(x, FLOOR))}.
 *
 * @throws IllegalArgumentException if {@code x <= 0}
 * @since 20.0
 */
@Beta
public static BigInteger floorPowerOfTwo(BigInteger x) {
 return BigInteger.ZERO.setBit(log2(x, RoundingMode.FLOOR));
}

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

/**
 * Returns the largest power of two less than or equal to {@code x}. This is equivalent to {@code
 * BigInteger.valueOf(2).pow(log2(x, FLOOR))}.
 *
 * @throws IllegalArgumentException if {@code x <= 0}
 * @since 20.0
 */
@Beta
public static BigInteger floorPowerOfTwo(BigInteger x) {
 return BigInteger.ZERO.setBit(log2(x, RoundingMode.FLOOR));
}

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

/**
 * Generates a number in [0, 2^numBits) with an exponential distribution. The floor of the log2 of
 * the result is chosen uniformly at random in [0, numBits), and then the result is chosen in that
 * range uniformly at random. Zero is treated as having log2 == 0.
 */
static BigInteger randomNonNegativeBigInteger(int numBits) {
 int digits = RANDOM_SOURCE.nextInt(numBits);
 if (digits == 0) {
  return new BigInteger(1, RANDOM_SOURCE);
 } else {
  return new BigInteger(digits, RANDOM_SOURCE).setBit(digits);
 }
}

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

/**
 * Returns the smallest power of two greater than or equal to {@code x}. This is equivalent to
 * {@code BigInteger.valueOf(2).pow(log2(x, CEILING))}.
 *
 * @throws IllegalArgumentException if {@code x <= 0}
 * @since 20.0
 */
@Beta
public static BigInteger ceilingPowerOfTwo(BigInteger x) {
 return BigInteger.ZERO.setBit(log2(x, RoundingMode.CEILING));
}

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

/**
 * Returns the largest power of two less than or equal to {@code x}. This is equivalent to {@code
 * BigInteger.valueOf(2).pow(log2(x, FLOOR))}.
 *
 * @throws IllegalArgumentException if {@code x <= 0}
 * @since 20.0
 */
@Beta
public static BigInteger floorPowerOfTwo(BigInteger x) {
 return BigInteger.ZERO.setBit(log2(x, RoundingMode.FLOOR));
}

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

public void testLog2Floor() {
 for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) {
  for (RoundingMode mode : asList(FLOOR, DOWN)) {
   int result = BigIntegerMath.log2(x, mode);
   assertTrue(ZERO.setBit(result).compareTo(x) <= 0);
   assertTrue(ZERO.setBit(result + 1).compareTo(x) > 0);
  }
 }
}

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

public void testLog2HalfDown() {
 for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) {
  int result = BigIntegerMath.log2(x, HALF_DOWN);
  BigInteger x2 = x.pow(2);
  // x^2 <= 2^(2 * result + 1), or else we would have rounded up
  assertTrue(ZERO.setBit(2 * result + 1).compareTo(x2) >= 0);
  // x^2 > 2^(2 * result - 1), or else we would have rounded down
  assertTrue(result == 0 || ZERO.setBit(2 * result - 1).compareTo(x2) < 0);
 }
}

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

public void testLog2HalfUp() {
 for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) {
  int result = BigIntegerMath.log2(x, HALF_UP);
  BigInteger x2 = x.pow(2);
  // x^2 < 2^(2 * result + 1), or else we would have rounded up
  assertTrue(ZERO.setBit(2 * result + 1).compareTo(x2) > 0);
  // x^2 >= 2^(2 * result - 1), or else we would have rounded down
  assertTrue(result == 0 || ZERO.setBit(2 * result - 1).compareTo(x2) <= 0);
 }
}

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

public void testLog2Ceiling() {
 for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) {
  for (RoundingMode mode : asList(CEILING, UP)) {
   int result = BigIntegerMath.log2(x, mode);
   assertTrue(ZERO.setBit(result).compareTo(x) >= 0);
   assertTrue(result == 0 || ZERO.setBit(result - 1).compareTo(x) < 0);
  }
 }
}

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

public void testAsUnsignedBigIntegerValue() {
 for (long value : TEST_LONGS) {
  BigInteger expected =
    (value >= 0)
      ? BigInteger.valueOf(value)
      : BigInteger.valueOf(value).add(BigInteger.ZERO.setBit(64));
  assertEquals(
    UnsignedLongs.toString(value),
    expected,
    UnsignedLong.fromLongBits(value).bigIntegerValue());
 }
}

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

@GwtIncompatible // BigIntegerMath // TODO(cpovirk): GWT-enable BigIntegerMath
public void testConstantMaxPowerOfSqrt2Unsigned() {
 assertEquals(
   /*expected=*/ BigIntegerMath.sqrt(BigInteger.ZERO.setBit(2 * Integer.SIZE - 1), FLOOR)
     .intValue(),
   /*actual=*/ IntMath.MAX_POWER_OF_SQRT2_UNSIGNED);
}

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

@GwtIncompatible // TODO
public void testConstantMaxPowerOfSqrt2Unsigned() {
 assertEquals(
   /*expected=*/ BigIntegerMath.sqrt(BigInteger.ZERO.setBit(2 * Long.SIZE - 1), FLOOR)
     .longValue(),
   /*actual=*/ LongMath.MAX_POWER_OF_SQRT2_UNSIGNED);
}

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

@Test
public void testUnscaledBigIntegerToDecimalOverflow()
{
  assertUnscaledBigIntegerToDecimalOverflows(MAX_DECIMAL_UNSCALED_VALUE.add(BigInteger.ONE));
  assertUnscaledBigIntegerToDecimalOverflows(MAX_DECIMAL_UNSCALED_VALUE.setBit(95));
  assertUnscaledBigIntegerToDecimalOverflows(MAX_DECIMAL_UNSCALED_VALUE.setBit(127));
  assertUnscaledBigIntegerToDecimalOverflows(MIN_DECIMAL_UNSCALED_VALUE.subtract(BigInteger.ONE));
}

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

@GwtIncompatible // TODO
public void testConstantSqrt2PrecomputedBits() {
 assertEquals(
   BigIntegerMath.sqrt(
     BigInteger.ZERO.setBit(2 * BigIntegerMath.SQRT2_PRECOMPUTE_THRESHOLD + 1), FLOOR),
   BigIntegerMath.SQRT2_PRECOMPUTED_BITS);
}

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

@GwtIncompatible // too slow
public void testEquals() {
 EqualsTester equalsTester = new EqualsTester();
 for (long a : TEST_LONGS) {
  BigInteger big =
    (a >= 0) ? BigInteger.valueOf(a) : BigInteger.valueOf(a).add(BigInteger.ZERO.setBit(64));
  equalsTester.addEqualityGroup(
    UnsignedLong.fromLongBits(a),
    UnsignedLong.valueOf(big),
    UnsignedLong.valueOf(big.toString()),
    UnsignedLong.valueOf(big.toString(16), 16));
 }
 equalsTester.testEquals();
}

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

public void testParseLongWithRadixLimits() {
 BigInteger max = BigInteger.ZERO.setBit(64).subtract(ONE);
 // loops through all legal radix values.
 for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
  // tests can successfully parse a number string with this radix.
  String maxAsString = max.toString(radix);
  assertEquals(max.longValue(), UnsignedLongs.parseUnsignedLong(maxAsString, radix));
  try {
   // tests that we get exception whre an overflow would occur.
   BigInteger overflow = max.add(ONE);
   String overflowAsString = overflow.toString(radix);
   UnsignedLongs.parseUnsignedLong(overflowAsString, radix);
   fail();
  } catch (NumberFormatException expected) {
  }
 }
 try {
  UnsignedLongs.parseUnsignedLong("1234567890abcdef1", 16);
  fail();
 } catch (NumberFormatException expected) {
 }
}

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

public void testLog2Exact() {
 for (BigInteger x : POSITIVE_BIGINTEGER_CANDIDATES) {
  // We only expect an exception if x was not a power of 2.
  boolean isPowerOf2 = BigIntegerMath.isPowerOfTwo(x);
  try {
   assertEquals(x, ZERO.setBit(BigIntegerMath.log2(x, UNNECESSARY)));
   assertTrue(isPowerOf2);
  } catch (ArithmeticException e) {
   assertFalse(isPowerOf2);
  }
 }
}

相关文章