Tabnine Logo
BigInteger
Code IndexAdd Tabnine to your IDE (free)

How to use
BigInteger
in
java.math

Best Java code snippets using java.math.BigInteger (Showing top 20 results out of 42,264)

Refine searchRefine arrow

  • BigDecimal
  • Date
origin: apache/incubator-dubbo

  @Override
  protected Object instantiate() throws Exception {
    return new BigInteger("0");
  }
}
origin: google/guava

/**
 * Computes the mean in a way that is obvious and resilient to overflow by using BigInteger
 * arithmetic.
 */
private static int computeMeanSafely(int x, int y) {
 BigInteger bigX = BigInteger.valueOf(x);
 BigInteger bigY = BigInteger.valueOf(y);
 BigDecimal bigMean =
   new BigDecimal(bigX.add(bigY)).divide(BigDecimal.valueOf(2), BigDecimal.ROUND_FLOOR);
 // parseInt blows up on overflow as opposed to intValue() which does not.
 return Integer.parseInt(bigMean.toString());
}
origin: stackoverflow.com

 import java.security.SecureRandom;
import java.math.BigInteger;

public final class SessionIdentifierGenerator {
 private SecureRandom random = new SecureRandom();

 public String nextSessionId() {
  return new BigInteger(130, random).toString(32);
 }
}
origin: spring-projects/spring-framework

public AlternativeJdkIdGenerator() {
  SecureRandom secureRandom = new SecureRandom();
  byte[] seed = new byte[8];
  secureRandom.nextBytes(seed);
  this.random = new Random(new BigInteger(seed).longValue());
}
origin: google/guava

@Override
BigInteger offset(BigInteger origin, long distance) {
 checkNonnegative(distance, "distance");
 return origin.add(BigInteger.valueOf(distance));
}
origin: prestodb/presto

@VisibleForTesting
public static BigDecimal average(LongDecimalWithOverflowAndLongState state, DecimalType type)
{
  BigDecimal sum = new BigDecimal(Decimals.decodeUnscaledValue(state.getLongDecimal()), type.getScale());
  BigDecimal count = BigDecimal.valueOf(state.getLong());
  if (state.getOverflow() != 0) {
    BigInteger overflowMultiplier = TWO.shiftLeft(UNSCALED_DECIMAL_128_SLICE_LENGTH * 8 - 2);
    BigInteger overflow = overflowMultiplier.multiply(BigInteger.valueOf(state.getOverflow()));
    sum = sum.add(new BigDecimal(overflow));
  }
  return sum.divide(count, type.getScale(), ROUND_HALF_UP);
}
origin: prestodb/presto

private static long bigIntegerDecimalToGenericIntegerType(BigInteger bigInteger, int sourceScale, long minValue, long maxValue)
{
  BigDecimal bigDecimal = new BigDecimal(bigInteger, sourceScale);
  BigInteger unscaledValue = bigDecimal.setScale(0, FLOOR).unscaledValue();
  if (unscaledValue.compareTo(BigInteger.valueOf(maxValue)) > 0) {
    return maxValue;
  }
  if (unscaledValue.compareTo(BigInteger.valueOf(minValue)) < 0) {
    return minValue;
  }
  return unscaledValue.longValueExact();
}
origin: spring-projects/spring-framework

@Test
public void convertBigDecimalToBigInteger() {
  String number = "987459837583750387355346";
  BigDecimal decimal = new BigDecimal(number);
  assertEquals(new BigInteger(number), NumberUtils.convertNumberToTargetClass(decimal, BigInteger.class));
}
origin: google/guava

@GwtIncompatible // TODO
@AndroidIncompatible // TODO(cpovirk): File BigDecimal.divide() rounding bug.
public void testDivNonZero() {
 for (long p : NONZERO_LONG_CANDIDATES) {
  for (long q : NONZERO_LONG_CANDIDATES) {
   for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) {
    long expected =
      new BigDecimal(valueOf(p)).divide(new BigDecimal(valueOf(q)), 0, mode).longValue();
    long actual = LongMath.divide(p, q, mode);
    if (expected != actual) {
     failFormat("expected divide(%s, %s, %s) = %s; got %s", p, q, mode, expected, actual);
    }
   }
  }
 }
}
origin: google/guava

@AndroidIncompatible // slow
public void testDivNonZero() {
 for (int p : NONZERO_INTEGER_CANDIDATES) {
  for (int q : NONZERO_INTEGER_CANDIDATES) {
   for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) {
    // Skip some tests that fail due to GWT's non-compliant int implementation.
    // TODO(cpovirk): does this test fail for only some rounding modes or for all?
    if (p == -2147483648 && q == -1 && intsCanGoOutOfRange()) {
     continue;
    }
    int expected =
      new BigDecimal(valueOf(p)).divide(new BigDecimal(valueOf(q)), 0, mode).intValue();
    assertEquals(p + "/" + q, force32(expected), IntMath.divide(p, q, mode));
   }
  }
 }
}
origin: google/guava

public void testToString() {
 String[] tests = {
  "0",
  "ffffffffffffffff",
  "7fffffffffffffff",
  "ff1a618b7f65ea12",
  "5a4316b8c153ac4d",
  "6cf78a4b139a4e2a"
 };
 int[] bases = {2, 5, 7, 8, 10, 16};
 for (int base : bases) {
  for (String x : tests) {
   BigInteger xValue = new BigInteger(x, 16);
   long xLong = xValue.longValue(); // signed
   assertEquals(xValue.toString(base), UnsignedLongs.toString(xLong, base));
  }
 }
}
origin: stackoverflow.com

if (new BigInteger("1111000011110001", 2).toByteArray() == array)
origin: google/guava

public void testTryParse_radix() {
 for (int radix = Character.MIN_RADIX; radix <= Character.MAX_RADIX; radix++) {
  radixEncodeParseAndAssertEquals((long) 0, radix);
  radixEncodeParseAndAssertEquals((long) 8000, radix);
  radixEncodeParseAndAssertEquals((long) -8000, radix);
  radixEncodeParseAndAssertEquals(MAX_VALUE, radix);
  radixEncodeParseAndAssertEquals(MIN_VALUE, radix);
  assertNull("Radix: " + radix, Longs.tryParse("999999999999999999999999", radix));
  assertNull(
    "Radix: " + radix,
    Longs.tryParse(BigInteger.valueOf(MAX_VALUE).add(BigInteger.ONE).toString(), radix));
  assertNull(
    "Radix: " + radix,
    Longs.tryParse(BigInteger.valueOf(MIN_VALUE).subtract(BigInteger.ONE).toString(), radix));
 }
 assertNull("Hex string and dec parm", Longs.tryParse("FFFF", 10));
 assertEquals("Mixed hex case", 65535, Longs.tryParse("ffFF", 16).longValue());
}
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 testMod() {
 for (int x : ALL_INTEGER_CANDIDATES) {
  for (int m : POSITIVE_INTEGER_CANDIDATES) {
   assertEquals(valueOf(x).mod(valueOf(m)).intValue(), IntMath.mod(x, m));
  }
 }
}
origin: skylot/jadx

String generateRSAPublicKey() {
  RSAPublicKey pub = (RSAPublicKey) cert.getPublicKey();
  StringBuilder builder = new StringBuilder();
  append(builder, NLS.str("certificate.serialPubKeyType"), pub.getAlgorithm());
  append(builder, NLS.str("certificate.serialPubKeyExponent"), pub.getPublicExponent().toString(10));
  append(builder, NLS.str("certificate.serialPubKeyModulusSize"), Integer.toString(
      pub.getModulus().toString(2).length()));
  append(builder, NLS.str("certificate.serialPubKeyModulus"), pub.getModulus().toString(10));
  return builder.toString();
}
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: spring-projects/spring-framework

@Test
public void parseNumberAsNegativeHex() {
  String aByte = "-0x80";
  String aShort = "-0x8000";
  String anInteger = "-0x80000000";
  String aLong = "-0x8000000000000000";
  String aReallyBigInt = "FEBD4E677898DFEBFFEE44";
  assertNegativeByteEquals(aByte);
  assertNegativeShortEquals(aShort);
  assertNegativeIntegerEquals(anInteger);
  assertNegativeLongEquals(aLong);
  assertEquals("BigInteger did not parse",
      new BigInteger(aReallyBigInt, 16).negate(), NumberUtils.parseNumber("-0x" + aReallyBigInt, BigInteger.class));
}
origin: google/guava

@GwtIncompatible // TODO
public void testConstantsHalfPowersOf10() {
 for (int i = 0; i < LongMath.halfPowersOf10.length; i++) {
  assertEquals(
    BigIntegerMath.sqrt(BigInteger.TEN.pow(2 * i + 1), FLOOR),
    BigInteger.valueOf(LongMath.halfPowersOf10[i]));
 }
 BigInteger nextBigger =
   BigIntegerMath.sqrt(BigInteger.TEN.pow(2 * LongMath.halfPowersOf10.length + 1), FLOOR);
 assertTrue(nextBigger.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0);
}
origin: google/guava

@AndroidIncompatible // slow
@GwtIncompatible // TODO
public void testMod() {
 for (long x : ALL_LONG_CANDIDATES) {
  for (long m : POSITIVE_LONG_CANDIDATES) {
   assertEquals(valueOf(x).mod(valueOf(m)).longValue(), LongMath.mod(x, m));
  }
 }
}
java.mathBigInteger

Javadoc

An immutable arbitrary-precision signed integer.

Fast Cryptography

This implementation is efficient for operations traditionally used in cryptography, such as the generation of large prime numbers and computation of the modular inverse.

Slow Two's Complement Bitwise Operations

This API includes operations for bitwise operations in two's complement representation. Two's complement is not the internal representation used by this implementation, so such methods may be inefficient. Use java.util.BitSet for high-performance bitwise operations on arbitrarily-large sequences of bits.

Most used methods

  • <init>
    This internal constructor differs from its public cousin with the arguments reversed in two ways: it
  • valueOf
    Returns a BigInteger with the given two's complement representation. Assumes that the input array wi
  • toString
  • compareTo
    Compares this BigInteger with value. Returns -1if this < value, 0 if this == value and 1if this > va
  • longValue
    Returns this BigInteger as a long value. If this is too big to be represented as a long, then this %
  • add
    Adds the contents of the int arrays x and y. This method allocates a new int array to hold the answe
  • intValue
    Converts this BigInteger to an int. This conversion is analogous to a narrowing primitive conversion
  • toByteArray
    Returns the two's complement representation of this BigInteger in a byte array.
  • equals
    Compares this BigInteger with the specified Object for equality.
  • multiply
    Returns a BigInteger whose value is this * value.
  • subtract
    Subtracts the contents of the second int arrays (little) from the first (big). The first int array (
  • bitLength
    Calculate bitlength of contents of the first len elements an int array, assuming there are no leadin
  • subtract,
  • bitLength,
  • divide,
  • negate,
  • signum,
  • mod,
  • pow,
  • hashCode,
  • shiftLeft,
  • doubleValue

Popular in Java

  • Updating database using SQL prepared statement
  • scheduleAtFixedRate (Timer)
  • getContentResolver (Context)
  • getApplicationContext (Context)
  • Table (com.google.common.collect)
    A collection that associates an ordered pair of keys, called a row key and a column key, with a sing
  • Color (java.awt)
    The Color class is used to encapsulate colors in the default sRGB color space or colors in arbitrary
  • MessageDigest (java.security)
    Uses a one-way hash function to turn an arbitrary number of bytes into a fixed-length byte sequence.
  • TimeZone (java.util)
    TimeZone represents a time zone offset, and also figures out daylight savings. Typically, you get a
  • Annotation (javassist.bytecode.annotation)
    The annotation structure.An instance of this class is returned bygetAnnotations() in AnnotationsAttr
  • LogFactory (org.apache.commons.logging)
    Factory for creating Log instances, with discovery and configuration features similar to that employ
  • Top plugins for WebStorm
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now