## Examples of java.math.BigInteger

• java.math.BigInteger
Immutable arbitrary-precision integers. All operations behave as if BigIntegers were represented in two's-complement notation (like Java's primitive integer types). BigInteger provides analogues to all of Java's primitive integer operators, and all relevant methods from java.lang.Math. Additionally, BigInteger provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.

Semantics of arithmetic operations exactly mimic those of Java's integer arithmetic operators, as defined in The Java Language Specification. For example, division by zero throws an {@code ArithmeticException}, and division of a negative by a positive yields a negative (or zero) remainder. All of the details in the Spec concerning overflow are ignored, as BigIntegers are made as large as necessary to accommodate the results of an operation.

Semantics of shift operations extend those of Java's shift operators to allow for negative shift distances. A right-shift with a negative shift distance results in a left shift, and vice-versa. The unsigned right shift operator ( {@code >>>}) is omitted, as this operation makes little sense in combination with the "infinite word size" abstraction provided by this class.

Semantics of bitwise logical operations exactly mimic those of Java's bitwise integer operators. The binary operators ( {@code and}, {@code or}, {@code xor}) implicitly perform sign extension on the shorter of the two operands prior to performing the operation.

Comparison operations perform signed integer comparisons, analogous to those performed by Java's relational and equality operators.

Modular arithmetic operations are provided to compute residues, perform exponentiation, and compute multiplicative inverses. These methods always return a non-negative result, between {@code 0} and {@code (modulus - 1)}, inclusive.

Bit operations operate on a single bit of the two's-complement representation of their operand. If necessary, the operand is sign- extended so that it contains the designated bit. None of the single-bit operations can produce a BigInteger with a different sign from the BigInteger being operated on, as they affect only a single bit, and the "infinite word size" abstraction provided by this class ensures that there are infinitely many "virtual sign bits" preceding each BigInteger.

For the sake of brevity and clarity, pseudo-code is used throughout the descriptions of BigInteger methods. The pseudo-code expression {@code (i + j)} is shorthand for "a BigInteger whose value isthat of the BigInteger {@code i} plus that of the BigInteger {@code j}." The pseudo-code expression {@code (i == j)} is shorthand for" {@code true} if and only if the BigInteger {@code i} represents the samevalue as the BigInteger {@code j}." Other pseudo-code expressions are interpreted similarly.

All methods and constructors in this class throw {@code NullPointerException} when passeda null object reference for any input parameter. @see BigDecimal @author Josh Bloch @author Michael McCloskey @since JDK1.1

 `117118119120121122123124125126127128129130` ```                assertNotNull(copy.getResults());         assertEquals(1, copy.getResults().length);         assertNotNull(copy.getResults()[0]);         assertEquals(4, copy.getResults()[0].size());         assertEquals(new BigInteger("100"), copy.getResults()[0].get(0)); //\$NON-NLS-1\$         assertEquals(new BigInteger("200"), copy.getResults()[0].get(1)); //\$NON-NLS-1\$         assertEquals(new BigInteger("300"), copy.getResults()[0].get(2)); //\$NON-NLS-1\$         assertEquals(new BigInteger("400"), copy.getResults()[0].get(3)); //\$NON-NLS-1\$                 assertNotNull(copy.getWarnings());         assertEquals(2, copy.getWarnings().size());         assertEquals(Exception.class, copy.getWarnings().get(0).getClass());         assertEquals("warning1", ((Exception)copy.getWarnings().get(0)).getMessage()); //\$NON-NLS-1\$ ```
View Full Code Here

 `15931594159515961597159815991600160116021603` ```    GroupSymbol g = new GroupSymbol("a.g1"); //\$NON-NLS-1\$     From from = new From();     from.addGroup(g);     Select select = new Select();     select.addSymbol(new ExpressionSymbol("expr", new Constant(new BigInteger("1000000000000000000000000")))); //\$NON-NLS-1\$ //\$NON-NLS-2\$                 Query query = new Query();     query.setSelect(select);     query.setFrom(from);     ```
View Full Code Here

 `9899100101102103104105106107108` ```    public void testMostTypes() throws Exception {         Object[][] results = new Object[][] {             new Object[] { new Integer(0), "ABCDEFGHIJ", new Float(0), new Long(0), new Double(0), new Byte((byte)0), //\$NON-NLS-1\$                 LoopbackExecution.SQL_DATE_VAL, LoopbackExecution.TIME_VAL,                 LoopbackExecution.TIMESTAMP_VAL, Boolean.FALSE,                 new BigInteger("0"), new BigDecimal("0"), "ABCDEFGHIJ", //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$                 new Short((short)0), new Character('c')                  }          };                 helpTestQuery("SELECT intkey, StringKey, floatnum, longnum, doublenum, bytenum, " + //\$NON-NLS-1\$ ```
View Full Code Here

 `6869707172737475767778` ```    X509V3CertificateGenerator certificateGenerator =       new X509V3CertificateGenerator();         certificateGenerator.setSignatureAlgorithm( "MD5WithRSAEncryption" );         certificateGenerator.setSerialNumber( new BigInteger( ""+SystemTime.getCurrentTime()));               X509Name  issuer_dn = new X509Name(true,cert_dn);         certificateGenerator.setIssuerDN(issuer_dn);     ```
View Full Code Here

 `152153154155156157158159160161162` ```    throws AEVerifierException, Exception   {     KeyFactory key_factory = KeyFactory.getInstance("RSA");         RSAPublicKeySpec   public_key_spec =       new RSAPublicKeySpec( new BigInteger(modulus,16), new BigInteger(pub_exp,16));     RSAPublicKey public_key   = (RSAPublicKey)key_factory.generatePublic( public_key_spec );         Signature  sig = Signature.getInstance("MD5withRSA" ); ```
View Full Code Here

 `273274275276277278279280` ```     this.chararray_field = (boolean_field ? new char[] {char_field} : null);     this.bytearray_field = (boolean_field ? new byte[] {byte_field} : null);     this.doublearray_field = (boolean_field ? new double[] {double_field} : null);     this.string_field = (boolean_field ? "str" + longval : null);     biginteger_field = (boolean_field ? new BigInteger("" + longval) : null);     bigdecimal_field = (boolean_field ? new BigDecimal(biginteger_field) : null);   } ```
View Full Code Here

 `328329330331332333334335336337338` ```      Assert.assertEquals("chararray_field bad value", char_field, chararray_field[0]);       Assert.assertEquals("bytearray_field bad value", 1, bytearray_field.length);       Assert.assertEquals("bytearray_field bad value", byte_field, bytearray_field[0]);       Assert.assertEquals("doubleArray_field bad value", 1, doublearray_field.length);       Assert.assertEquals("string_field bad value", "str" + longval, string_field);       Assert.assertEquals("biginteger_field bad value", new BigInteger("" + longval), biginteger_field);       BigDecimal expected = new BigDecimal(biginteger_field);       BigDecimal found = bigdecimal_field;       Assert.assertTrue("bigdecimal_field bad value, expected " + expected         + ", found: " + found,         expected.compareTo(found)==0); ```
View Full Code Here

 `134135136137138139140141` ```     this.chararray_field = (boolean_field ? new char[] {char_field} : null);     this.bytearray_field = (boolean_field ? new byte[] {byte_field} : null);     this.doubleArray_field = (boolean_field ? new double[] {double_field} : null);     this.string_field = (boolean_field ? "str" + longval : null);     biginteger_field = (boolean_field ? new BigInteger("" + longval) : null);     bigdecimal_field = (boolean_field ? new BigDecimal(biginteger_field) : null);   } ```
View Full Code Here

 `189190191192193194195196197198199` ```      Assert.assertEquals("chararray_field bad value", char_field, chararray_field[0]);       Assert.assertEquals("bytearray_field bad value", 1, bytearray_field.length);       Assert.assertEquals("bytearray_field bad value", byte_field, bytearray_field[0]);       Assert.assertEquals("doubleArray_field bad value", 1, doubleArray_field.length);       Assert.assertEquals("string_field bad value", "str" + longval, string_field);       Assert.assertEquals("biginteger_field bad value", new BigInteger("" + longval), biginteger_field);       BigDecimal expected = new BigDecimal(biginteger_field);       BigDecimal found = bigdecimal_field;       Assert.assertTrue("bigdecimal_field bad value, expected " + expected         + ", found: " + found,         expected.compareTo(found)==0); ```
View Full Code Here

 `8687888990919293949596` ```        } else if (clazz == Boolean.class) {             return (T) new Boolean(false);         } else if (clazz == BigDecimal.class) {             return (T) new BigDecimal(COUNTER.getAndIncrement());         } else if (clazz == BigInteger.class) {             return (T) new BigInteger("" + COUNTER.getAndIncrement());         } else if (clazz == java.sql.Date.class) {             return (T) new java.sql.Date(COUNTER.getAndIncrement());         } else if (clazz == java.sql.Time.class) {             return (T) new java.sql.Time(COUNTER.getAndIncrement());         } else if (clazz == java.sql.Timestamp.class) { ```
View Full Code Here