Java Code Examples for java.math.BigInteger#intValue()
The following examples show how to use
java.math.BigInteger#intValue() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FiniteFields.java From RipplePower with Apache License 2.0 | 6 votes |
public static FiniteField getPrimeField(BigInteger characteristic) { int bitLength = characteristic.bitLength(); if (characteristic.signum() <= 0 || bitLength < 2) { throw new IllegalArgumentException("'characteristic' must be >= 2"); } if (bitLength < 3) { switch (characteristic.intValue()) { case 2: return GF_2; case 3: return GF_3; } } return new PrimeField(characteristic); }
Example 2
Source File: RegistrationKey.java From tribaltrouble with GNU General Public License v2.0 | 6 votes |
public final static String encode(long key_code) { key_code = key_code & 0x7fffffffffffffffl; BigInteger key_big = new BigInteger(splitToBytes(key_code)); BigInteger crc = computeChecksum(key_code); key_big = key_big.or(crc.shiftLeft(64)); int mask = CHAR_TO_WORD.length() - 1; int shifting = computeShifting(); StringBuffer encoded_key = new StringBuffer(); while (encoded_key.length() < STRIPPED_LENGTH) { int index = key_big.intValue() & mask; char c = CHAR_TO_WORD.charAt(index); encoded_key.append(c); key_big = key_big.shiftRight(shifting); } encoded_key.reverse(); encoded_key.insert(12, SEPARATOR); encoded_key.insert(8, SEPARATOR); encoded_key.insert(4, SEPARATOR); return encoded_key.toString(); }
Example 3
Source File: Pfx.java From RipplePower with Apache License 2.0 | 6 votes |
private Pfx( ASN1Sequence seq) { BigInteger version = ((ASN1Integer)seq.getObjectAt(0)).getValue(); if (version.intValue() != 3) { throw new IllegalArgumentException("wrong version for PFX PDU"); } contentInfo = ContentInfo.getInstance(seq.getObjectAt(1)); if (seq.size() == 3) { macData = MacData.getInstance(seq.getObjectAt(2)); } }
Example 4
Source File: DERObjectIdentifier.java From BiglyBT with GNU General Public License v2.0 | 6 votes |
private void writeField( OutputStream out, BigInteger fieldValue) throws IOException { int byteCount = (fieldValue.bitLength()+6)/7; if (byteCount == 0) { out.write(0); } else { BigInteger tmpValue = fieldValue; byte[] tmp = new byte[byteCount]; for (int i = byteCount-1; i >= 0; i--) { tmp[i] = (byte) ((tmpValue.intValue() & 0x7f) | 0x80); tmpValue = tmpValue.shiftRight(7); } tmp[byteCount-1] &= 0x7f; out.write(tmp); } }
Example 5
Source File: MethodData.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
/** * Constructs a MethodData object. * @param encoding a Der-encoded data. * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data. * @exception IOException if an I/O error occurs while reading encoded data. */ public MethodData(DerValue encoding) throws Asn1Exception, IOException { DerValue der; if (encoding.getTag() != DerValue.tag_Sequence) { throw new Asn1Exception(Krb5.ASN1_BAD_ID); } der = encoding.getData().getDerValue(); if ((der.getTag() & 0x1F) == 0x00) { BigInteger bint = der.getData().getBigInteger(); methodType = bint.intValue(); } else throw new Asn1Exception(Krb5.ASN1_BAD_ID); if (encoding.getData().available() > 0) { der = encoding.getData().getDerValue(); if ((der.getTag() & 0x1F) == 0x01) { methodData = der.getData().getOctetString(); } else throw new Asn1Exception(Krb5.ASN1_BAD_ID); } if (encoding.getData().available() > 0) throw new Asn1Exception(Krb5.ASN1_BAD_ID); }
Example 6
Source File: DataTypeArray.java From ClickHouse-Native-JDBC with Apache License 2.0 | 6 votes |
@Override public Object[] deserializeBinaryBulk(int rows, BinaryDeserializer deserializer) throws IOException, SQLException { ClickHouseArray[] data = new ClickHouseArray[rows]; if (rows == 0) { return data; } Object[] offsets = offsetIDataType.deserializeBinaryBulk(rows, deserializer); ClickHouseArray res = new ClickHouseArray( elemDataType.deserializeBinaryBulk(((BigInteger) offsets[rows - 1]).intValue() , deserializer)); for (int row = 0, lastOffset = 0; row < rows; row++) { BigInteger offset = (BigInteger) offsets[row]; data[row] = res.slice(lastOffset, offset.intValue() - lastOffset); lastOffset = offset.intValue(); } return data; }
Example 7
Source File: UInt256.java From incubator-tuweni with Apache License 2.0 | 5 votes |
/** * Return a {@link UInt256} containing the specified value. * * @param value the value to create a {@link UInt256} for * @return a {@link UInt256} containing the specified value * @throws IllegalArgumentException if the value is negative or too large to be represented as a UInt256 */ public static UInt256 valueOf(BigInteger value) { checkArgument(value.signum() >= 0, "Argument must be positive"); checkArgument(value.bitLength() <= 256, "Argument is too large to represent a UInt256"); if (value.compareTo(BI_MAX_CONSTANT) <= 0) { return CONSTANTS[value.intValue()]; } int[] ints = new int[INTS_SIZE]; for (int i = INTS_SIZE - 1; i >= 0; --i) { ints[i] = value.intValue(); value = value.shiftRight(32); } return new UInt256(ints); }
Example 8
Source File: DerInputBuffer.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
/** * Returns the integer which takes up the specified number * of bytes in this buffer. * @throws IOException if the result is not within the valid * range for integer, i.e. between Integer.MIN_VALUE and * Integer.MAX_VALUE. * @param len the number of bytes to use. * @return the integer. */ public int getInteger(int len) throws IOException { BigInteger result = getBigInteger(len, false); if (result.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0) { throw new IOException("Integer below minimum valid value"); } if (result.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0) { throw new IOException("Integer exceeds maximum valid value"); } return result.intValue(); }
Example 9
Source File: BigIntegerMath.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
/** * Returns the square root of {@code x}, rounded with the specified rounding mode. * * @throws IllegalArgumentException if {@code x < 0} * @throws ArithmeticException if {@code mode} is {@link RoundingMode#UNNECESSARY} and * {@code sqrt(x)} is not an integer */ @GwtIncompatible // TODO @SuppressWarnings("fallthrough") public static BigInteger sqrt(BigInteger x, RoundingMode mode) { checkNonNegative("x", x); if (fitsInLong(x)) { return BigInteger.valueOf(LongMath.sqrt(x.longValue(), mode)); } BigInteger sqrtFloor = sqrtFloor(x); switch (mode) { case UNNECESSARY: checkRoundingUnnecessary(sqrtFloor.pow(2).equals(x)); // fall through case FLOOR: case DOWN: return sqrtFloor; case CEILING: case UP: int sqrtFloorInt = sqrtFloor.intValue(); boolean sqrtFloorIsExact = (sqrtFloorInt * sqrtFloorInt == x.intValue()) // fast check mod 2^32 && sqrtFloor.pow(2).equals(x); // slow exact check return sqrtFloorIsExact ? sqrtFloor : sqrtFloor.add(BigInteger.ONE); case HALF_DOWN: case HALF_UP: case HALF_EVEN: BigInteger halfSquare = sqrtFloor.pow(2).add(sqrtFloor); /* * We wish to test whether or not x <= (sqrtFloor + 0.5)^2 = halfSquare + 0.25. Since both x * and halfSquare are integers, this is equivalent to testing whether or not x <= * halfSquare. */ return (halfSquare.compareTo(x) >= 0) ? sqrtFloor : sqrtFloor.add(BigInteger.ONE); default: throw new AssertionError(); } }
Example 10
Source File: Asn1Utils.java From android-testdpc with Apache License 2.0 | 5 votes |
private static int bigIntegerToInt(BigInteger bigInt) throws CertificateParsingException { if (bigInt.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0 || bigInt.compareTo(BigInteger.ZERO) < 0) { throw new CertificateParsingException("INTEGER out of bounds"); } return bigInt.intValue(); }
Example 11
Source File: Gas.java From incubator-tuweni with Apache License 2.0 | 5 votes |
/** * Return a {@link Gas} containing the specified value. * * @param value The value to create a {@link Gas} for. * @return A {@link Gas} containing the specified value. * @throws IllegalArgumentException If the value is negative. */ public static Gas valueOf(BigInteger value) { checkArgument(value.signum() >= 0, "Argument must be positive"); if (value.compareTo(BI_MAX_CONSTANT) <= 0) { return CONSTANTS[value.intValue()]; } try { return new Gas(value.longValueExact()); } catch (ArithmeticException e) { throw new IllegalArgumentException(e.getMessage(), e); } }
Example 12
Source File: RecursiveDigitSum.java From Hackerrank-Solutions with MIT License | 5 votes |
static int digitSumUsingMathTrick(String n, int k) { BigInteger n1 = new BigInteger(n); n1 = n1.multiply(new BigInteger(k + "")); n1 = n1.remainder(new BigInteger("9")); return n1.intValue() == 0 ? 9 : n1.intValue(); }
Example 13
Source File: BigFractionSym.java From symja_android_library with GNU General Public License v3.0 | 5 votes |
@Override public final boolean equalsFraction(final int numerator, final int denominator) { BigInteger num = fFraction.getNumerator(); BigInteger den = fFraction.getDenominator(); return num.intValue() == numerator && den.intValue() == denominator && num.bitLength() <= 31 && den.bitLength() <= 31; }
Example 14
Source File: IntegerValue.java From oopsla15-artifact with Eclipse Public License 1.0 | 4 votes |
static IInteger newInteger(BigInteger value) { if (value.bitLength() > 31) { return new BigIntegerValue(value); } return new IntegerValue(value.intValue()); }
Example 15
Source File: McElieceKeyFactorySpi.java From RipplePower with Apache License 2.0 | 4 votes |
/** * Converts, if possible, a key specification into a * {@link BCMcEliecePrivateKey}. Currently, the following key specifications * are supported: {@link McEliecePrivateKeySpec}, * {@link PKCS8EncodedKeySpec}. * * @param keySpec the key specification * @return the McEliece private key * @throws InvalidKeySpecException if the KeySpec is not supported. */ public PrivateKey generatePrivate(KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof McEliecePrivateKeySpec) { return new BCMcEliecePrivateKey((McEliecePrivateKeySpec)keySpec); } else if (keySpec instanceof PKCS8EncodedKeySpec) { // get the DER-encoded Key according to PKCS#8 from the spec byte[] encKey = ((PKCS8EncodedKeySpec)keySpec).getEncoded(); // decode the PKCS#8 data structure to the pki object PrivateKeyInfo pki; try { pki = PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(encKey)); } catch (IOException e) { throw new InvalidKeySpecException("Unable to decode PKCS8EncodedKeySpec: " + e); } try { ASN1Primitive innerType = pki.parsePrivateKey().toASN1Primitive(); // build and return the actual key ASN1Sequence privKey = (ASN1Sequence)innerType; // decode oidString (but we don't need it right now) String oidString = ((ASN1ObjectIdentifier)privKey.getObjectAt(0)) .toString(); // decode <n> BigInteger bigN = ((ASN1Integer)privKey.getObjectAt(1)).getValue(); int n = bigN.intValue(); // decode <k> BigInteger bigK = ((ASN1Integer)privKey.getObjectAt(2)).getValue(); int k = bigK.intValue(); // decode <fieldPoly> byte[] encFieldPoly = ((ASN1OctetString)privKey.getObjectAt(3)) .getOctets(); // decode <goppaPoly> byte[] encGoppaPoly = ((ASN1OctetString)privKey.getObjectAt(4)) .getOctets(); // decode <sInv> byte[] encSInv = ((ASN1OctetString)privKey.getObjectAt(5)).getOctets(); // decode <p1> byte[] encP1 = ((ASN1OctetString)privKey.getObjectAt(6)).getOctets(); // decode <p2> byte[] encP2 = ((ASN1OctetString)privKey.getObjectAt(7)).getOctets(); //decode <h> byte[] encH = ((ASN1OctetString)privKey.getObjectAt(8)).getOctets(); // decode <qInv> ASN1Sequence qSeq = (ASN1Sequence)privKey.getObjectAt(9); byte[][] encQInv = new byte[qSeq.size()][]; for (int i = 0; i < qSeq.size(); i++) { encQInv[i] = ((ASN1OctetString)qSeq.getObjectAt(i)).getOctets(); } return new BCMcEliecePrivateKey(new McEliecePrivateKeySpec(OID, n, k, encFieldPoly, encGoppaPoly, encSInv, encP1, encP2, encH, encQInv)); } catch (IOException cce) { throw new InvalidKeySpecException( "Unable to decode PKCS8EncodedKeySpec."); } } throw new InvalidKeySpecException("Unsupported key specification: " + keySpec.getClass() + "."); }
Example 16
Source File: ObjectIdentifier.java From openjdk-8 with GNU General Public License v2.0 | 4 votes |
/** * Private helper method for serialization. To be compatible with old * versions of JDK. * @return components in an int array, if all the components are less than * Integer.MAX_VALUE. Otherwise, null. */ private int[] toIntArray() { int length = encoding.length; int[] result = new int[20]; int which = 0; int fromPos = 0; for (int i = 0; i < length; i++) { if ((encoding[i] & 0x80) == 0) { // one section [fromPos..i] if (i - fromPos + 1 > 4) { BigInteger big = new BigInteger(pack(encoding, fromPos, i-fromPos+1, 7, 8)); if (fromPos == 0) { result[which++] = 2; BigInteger second = big.subtract(BigInteger.valueOf(80)); if (second.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) == 1) { return null; } else { result[which++] = second.intValue(); } } else { if (big.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) == 1) { return null; } else { result[which++] = big.intValue(); } } } else { int retval = 0; for (int j = fromPos; j <= i; j++) { retval <<= 7; byte tmp = encoding[j]; retval |= (tmp & 0x07f); } if (fromPos == 0) { if (retval < 80) { result[which++] = retval / 40; result[which++] = retval % 40; } else { result[which++] = 2; result[which++] = retval - 80; } } else { result[which++] = retval; } } fromPos = i+1; } if (which >= result.length) { result = Arrays.copyOf(result, which + 10); } } return Arrays.copyOf(result, which); }
Example 17
Source File: ObjectIdentifier.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
/** * Private helper method for serialization. To be compatible with old * versions of JDK. * @return components in an int array, if all the components are less than * Integer.MAX_VALUE. Otherwise, null. */ private int[] toIntArray() { int length = encoding.length; int[] result = new int[20]; int which = 0; int fromPos = 0; for (int i = 0; i < length; i++) { if ((encoding[i] & 0x80) == 0) { // one section [fromPos..i] if (i - fromPos + 1 > 4) { BigInteger big = new BigInteger(pack(encoding, fromPos, i-fromPos+1, 7, 8)); if (fromPos == 0) { result[which++] = 2; BigInteger second = big.subtract(BigInteger.valueOf(80)); if (second.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) == 1) { return null; } else { result[which++] = second.intValue(); } } else { if (big.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) == 1) { return null; } else { result[which++] = big.intValue(); } } } else { int retval = 0; for (int j = fromPos; j <= i; j++) { retval <<= 7; byte tmp = encoding[j]; retval |= (tmp & 0x07f); } if (fromPos == 0) { if (retval < 80) { result[which++] = retval / 40; result[which++] = retval % 40; } else { result[which++] = 2; result[which++] = retval - 80; } } else { result[which++] = retval; } } fromPos = i+1; } if (which >= result.length) { result = Arrays.copyOf(result, which + 10); } } return Arrays.copyOf(result, which); }
Example 18
Source File: DatatypeConverter.java From mpxj with GNU Lesser General Public License v2.1 | 4 votes |
/** * Parse work units. * * @param value work units value * @return TimeUnit instance */ public static final TimeUnit parseWorkUnits(BigInteger value) { TimeUnit result = TimeUnit.HOURS; if (value != null) { switch (value.intValue()) { case 1: { result = TimeUnit.MINUTES; break; } case 3: { result = TimeUnit.DAYS; break; } case 4: { result = TimeUnit.WEEKS; break; } case 5: { result = TimeUnit.MONTHS; break; } case 7: { result = TimeUnit.YEARS; break; } default: case 2: { result = TimeUnit.HOURS; break; } } } return (result); }
Example 19
Source File: XMLGregorianCalendarImpl.java From jdk8u60 with GNU General Public License v2.0 | 3 votes |
/** * <p>Set low and high order component of XSD <code>dateTime</code> year field.</p> * * <p>Unset this field by invoking the setter with a parameter value of <code>null</code>.</p> * * @param year value constraints summarized in <a href="#datetimefield-year">year field of date/time field mapping table</a>. * * @throws IllegalArgumentException if <code>year</code> parameter is * outside value constraints for the field as specified in * <a href="#datetimefieldmapping">date/time field mapping table</a>. */ public void setYear(BigInteger year) { if (year == null) { this.eon = null; this.year = DatatypeConstants.FIELD_UNDEFINED; } else { BigInteger temp = year.remainder(BILLION); this.year = temp.intValue(); setEon(year.subtract(temp)); } }
Example 20
Source File: ULong.java From CPE552-Java with GNU General Public License v3.0 | 3 votes |
/** * Throw exception if value out of range (long version) * * @param value Value to check * @return value if it is in range * @throws ArithmeticException if value is out of range */ public static short checkSigned(BigInteger value) throws ArithmeticException { if (value.compareTo(BigInteger.ZERO) < 0 || value.intValue() > MAX_VALUE) { throw new ArithmeticException("Value is out of range : " + value); } return value.shortValue(); }