Java Code Examples for java.math.BigInteger#intValueExact()
The following examples show how to use
java.math.BigInteger#intValueExact() .
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: CheckTypeVisitor.java From Arend with Apache License 2.0 | 6 votes |
@Override public @Nullable TypecheckingResult checkNumber(@NotNull BigInteger number, @Nullable CoreExpression expectedType, @NotNull ConcreteExpression marker) { if (!((expectedType == null || expectedType instanceof Expression) && marker instanceof Concrete.Expression)) { throw new IllegalArgumentException(); } boolean isNegative = number.signum() < 0; Expression resultExpr; try { int value = number.intValueExact(); resultExpr = new SmallIntegerExpression(isNegative ? -value : value); } catch (ArithmeticException e) { resultExpr = new BigIntegerExpression(isNegative ? number.negate() : number); } TypecheckingResult result; if (isNegative) { result = new TypecheckingResult(ExpressionFactory.Neg(resultExpr), ExpressionFactory.Int()); } else { result = new TypecheckingResult(resultExpr, ExpressionFactory.Nat()); } return checkResult((Expression) expectedType, result, (Concrete.Expression) marker); }
Example 2
Source File: BigIntType.java From es6draft with MIT License | 6 votes |
/** * BigInt::exponentiate (base, exponent) * * @param cx * the execution context * @param base * the base value * @param exponent * the exponent value * @return the value of {@code base} raised to the power {@code exponent} */ public static BigInteger exponentiate(ExecutionContext cx, BigInteger base, BigInteger exponent) { /* step 1 */ if (exponent.signum() < 0) { throw newRangeError(cx, Messages.Key.BigIntNegativeExponent); } /* steps 2-3 */ if (exponent.signum() == 0) { return BigInteger.ONE; } if (base.signum() == 0) { return BigInteger.ZERO; } if (base.equals(BigInteger.ONE) || base.equals(NEGATIVE_ONE)) { return base.signum() > 0 || !exponent.testBit(0) ? BigInteger.ONE : NEGATIVE_ONE; } if (exponent.bitLength() > 31) { throw newRangeError(cx, Messages.Key.BigIntValueTooLarge); } int exponentAsInt = exponent.intValueExact(); try { return base.pow(exponentAsInt); } catch (ArithmeticException e) { throw newRangeError(cx, Messages.Key.BigIntValueTooLarge); } }
Example 3
Source File: BigIntType.java From es6draft with MIT License | 6 votes |
/** * BigInt::leftShift (x, y) * * @param cx * the execution context * @param x * the x value * @param y * the y value * @return the result of left-shifting {@code x} by {@code y} */ public static BigInteger leftShift(ExecutionContext cx, BigInteger x, BigInteger y) { if (x.signum() == 0) { return BigInteger.ZERO; } if (y.signum() == 0) { return x; } if (y.bitLength() > 31) { if (y.signum() > 0) { throw newRangeError(cx, Messages.Key.BigIntValueTooLarge); } return BigInteger.ZERO; } int yAsInt = y.intValueExact(); try { return x.shiftLeft(yAsInt); } catch (ArithmeticException e) { throw newRangeError(cx, Messages.Key.BigIntValueTooLarge); } }
Example 4
Source File: BigIntType.java From es6draft with MIT License | 6 votes |
/** * BigInt::signedRightShift (x, y) * * @param cx * the execution context * @param x * the x value * @param y * the y value * @return the result of right-shifting {@code x} by {@code y} */ public static BigInteger signedRightShift(ExecutionContext cx, BigInteger x, BigInteger y) { if (x.signum() == 0) { return BigInteger.ZERO; } if (y.signum() == 0) { return x; } if (y.bitLength() > 31) { if (y.signum() < 0) { throw newRangeError(cx, Messages.Key.BigIntValueTooLarge); } return BigInteger.ZERO; } int yAsInt = y.intValueExact(); try { return x.shiftRight(yAsInt); } catch (ArithmeticException e) { throw newRangeError(cx, Messages.Key.BigIntValueTooLarge); } }
Example 5
Source File: Asn1BerParser.java From Xpatch with Apache License 2.0 | 5 votes |
private static int integerToInt(ByteBuffer encoded) throws Asn1DecodingException { BigInteger value = integerToBigInteger(encoded); try { return value.intValueExact(); } catch (ArithmeticException e) { throw new Asn1DecodingException( String.format("INTEGER cannot be represented as int: %1$d (0x%1$x)", value), e); } }
Example 6
Source File: BridgeDeserializer.java From aion with MIT License | 5 votes |
private static int parseMeta(@Nonnull final byte[] call, final int offset) { // check for out of bounds exceptions, each one of these is important to check since integer // overflow can be taken advantage of to pass this block if one of these is omitted. if ((offset < 0) || (offset > call.length) || (offset + LIST_META > call.length)) return ERR_INT; // more minimum length checks final byte[] pt = new byte[LIST_META]; System.arraycopy(call, offset, pt, 0, LIST_META); // check that destination is parse-able final BigInteger bigInt = new BigInteger(1, pt); if (bigInt.compareTo(INT_MAX_VAL) > 0) return ERR_INT; try { return bigInt.intValueExact(); } catch (ArithmeticException e) { /* * Catch case S#3, handles the case where someone could overload * the biginteger, and try to create a value larger than INT_MAX, * in this case we should consider the meta decode a failure and * return -1; */ return ERR_INT; } }
Example 7
Source File: Sar.java From securify with Apache License 2.0 | 5 votes |
@Override public void computeResultValues() { if (getInput()[0].hasConstantValue() && getInput()[1].hasConstantValue() && getOutput()[0].getConstantValue() == Variable.VALUE_UNDEFINED) { BigInteger r; BigInteger a = BigIntUtil.fromUint256(getInput()[0].getConstantValue()); BigInteger b = BigIntUtil.fromInt256(getInput()[1].getConstantValue()); if(a.compareTo(BigInteger.valueOf(256)) >= 0) { if(b.signum() == -1) { r = BigInteger.valueOf(-1); }else { r = BigInteger.ZERO; } } else if(b.signum() != -1) { r = b.shiftRight(a.intValueExact()); } else { r = b; for (int i = 0; i < a.intValueExact(); i++) { r.shiftRight(1); r.setBit(256); } } getOutput()[0].setConstantValue(BigIntUtil.toInt256(r)); } else { getOutput()[0].setConstantValue(Variable.VALUE_ANY); } }
Example 8
Source File: MincodeParser.java From divolte-collector with Apache License 2.0 | 5 votes |
@SuppressWarnings("PMD.EmptyCatchBlock") private void setNumberValue(final BigInteger value) { _numTypesValid = NR_BIGINT; _numberBigInt = value; // Jackson expects all smaller types to be filled in, // so do this until they don't fit. try { _numberLong = value.longValueExact(); _numTypesValid |= NR_LONG; _numberInt = value.intValueExact(); _numTypesValid |= NR_INT; } catch (final ArithmeticException e) { // Harmless; means we reached a type into which it won't fit. } }
Example 9
Source File: X509Cert.java From xipki with Apache License 2.0 | 4 votes |
/** * Gets the certificate constraints path length from the * critical {@code BasicConstraints} extension, (OID = 2.5.29.19). * <p/> * The basic constraints extension identifies whether the subject * of the certificate is a Certificate Authority (CA) and * how deep a certification path may exist through that CA. The * {@code pathLenConstraint} field (see below) is meaningful * only if {@code cA} is set to TRUE. In this case, it gives the * maximum number of CA certificates that may follow this certificate in a * certification path. A value of zero indicates that only an end-entity * certificate may follow in the path. * <p/> * The ASN.1 definition for this is: * <pre> * BasicConstraints ::= SEQUENCE { * cA BOOLEAN DEFAULT FALSE, * pathLenConstraint INTEGER (0..MAX) OPTIONAL } * </pre> * * @return the value of {@code pathLenConstraint} if the * BasicConstraints extension is present in the certificate and the * subject of the certificate is a CA, otherwise -1. * If the subject of the certificate is a CA and * {@code pathLenConstraint} does not appear, * {@code Integer.MAX_VALUE} is returned to indicate that there is no * limit to the allowed length of the certification path. */ public int getBasicConstraints() { if (basicConstrains == -2) { synchronized (sync) { if (bcInstance != null) { byte[] extnValue = getCoreExtValue(Extension.basicConstraints); if (extnValue == null) { basicConstrains = -1; } else { BasicConstraints bc = BasicConstraints.getInstance(extnValue); if (bc.isCA()) { BigInteger bn = bc.getPathLenConstraint(); basicConstrains = bn == null ? Integer.MAX_VALUE : bn.intValueExact(); } else { basicConstrains = -1; } } } else { basicConstrains = jceInstance.getBasicConstraints(); } } } return basicConstrains; }
Example 10
Source File: JsonReader.java From logging-log4j2 with Apache License 2.0 | 4 votes |
private Number readNumber() { // Read sign. buffer.setLength(0); if (readChar == '-') { bufferReadChar(); } // Read fraction. boolean floatingPoint = false; bufferDigits(); if (readChar == '.') { bufferReadChar(); bufferDigits(); floatingPoint = true; } // Read exponent. if (readChar == 'e' || readChar == 'E') { floatingPoint = true; bufferReadChar(); if (readChar == '+' || readChar == '-') { bufferReadChar(); } bufferDigits(); } // Convert the read number. final String string = buffer.toString(); if (floatingPoint) { return new BigDecimal(string); } else { final BigInteger bigInteger = new BigInteger(string); try { return bigInteger.intValueExact(); } catch (ArithmeticException ignoredIntOverflow) { try { return bigInteger.longValueExact(); } catch (ArithmeticException ignoredLongOverflow) { return bigInteger; } } } }
Example 11
Source File: Main.java From Java-Coding-Problems with MIT License | 3 votes |
public static void main(String[] args) { BigInteger nr = BigInteger.valueOf(Long.MAX_VALUE); long nrLong = nr.longValue(); System.out.println(nr + " as long is: " + nrLong); int nrInt = nr.intValue(); System.out.println(nr + " as int is: " + nrInt); short nrShort = nr.shortValue(); System.out.println(nr + " as short is: " + nrShort); byte nrByte = nr.byteValue(); System.out.println(nr + " as byte is: " + nrByte); long nrExactLong = nr.longValueExact(); // ok System.out.println(nr + " as exact long is: " + nrExactLong); int nrExactInt = nr.intValueExact(); // ArithmeticException System.out.println(nr + " as exact int is: " + nrExactInt); short nrExactShort = nr.shortValueExact(); // ArithmeticException System.out.println(nr + " as exact short is: " + nrExactShort); byte nrExactByte = nr.byteValueExact(); // ArithmeticException System.out.println(nr + " as exact byte is: " + nrExactByte); }
Example 12
Source File: IntegerTextField.java From ghidra with Apache License 2.0 | 3 votes |
/** * Returns the current value as an int. * * <P> If the field has no current value, 0 will be returned. If * the value is bigger (or smaller) than an int, it will be cast to an int. * * <P> If using this method, it is highly recommended that you set the max value to {@link Integer#MAX_VALUE} * or lower. * * @return the current value as an int. Or 0 if there is no value * @throws ArithmeticException if the value in this field will not fit into an int */ public int getIntValue() { BigInteger currentValue = getValue(); if (currentValue == null) { return 0; } return currentValue.intValueExact(); }