## 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

 `3031323334353637383940` ```        print("Object", new Object());         print("Timestamp", new java.sql.Timestamp(0));         print("Date", new java.sql.Date(0));         print("Time", new java.sql.Time(0));         print("BigDecimal", new BigDecimal("0"));         print("BigInteger", new BigInteger("0"));         print("String", new String("Hello"));         print("Data", Data.create(null, 10));         print("Row", new Row(new Value, 0));         System.out.println();         for (int i = 1; i < 128; i += i) { ```
View Full Code Here

 `424344454647484950515253` ```    }     private void test(int type, long minValue, long maxValue) {         values = New.arrayList();         this.dataType = type;         this.min = new BigInteger("" + minValue);         this.max = new BigInteger("" + maxValue);         add(0);         add(minValue);         add(maxValue);         add(maxValue - 1);         add(minValue + 1); ```
View Full Code Here

 `858687888990919293949596` ```            fail();         }     }     private void testValues(Value va, Value vb) {         BigInteger a = new BigInteger(va.getString());         BigInteger b = new BigInteger(vb.getString());         successExpected = inRange(a.negate());         try {             checkIfExpected(va.negate().getString(), a.negate().toString());             onSuccess();         } catch (Exception e) { ```
View Full Code Here

 `439440441442443444445` ```    @Test public void testConvertStringLong1() throws Exception {         helpConvert("5", "long", new Long(5)); //\$NON-NLS-1\$ //\$NON-NLS-2\$     }     @Test public void testConvertStringBigInteger1() throws Exception {         helpConvert("5", "biginteger", new BigInteger("5")); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     } ```
View Full Code Here

 `444445446447448449450` ```        helpConvert("5", "biginteger", new BigInteger("5")); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     }     @Test public void testConvertStringBigInteger2() throws Exception {         String bigInt = "" + Integer.MAX_VALUE + "111"; //\$NON-NLS-1\$ //\$NON-NLS-2\$         helpConvert(bigInt, "biginteger", new BigInteger(bigInt)); //\$NON-NLS-1\$     } ```
View Full Code Here

 `460461462463464465466` ```    @Test public void testConvertStringBigDecimal1() throws Exception {         helpConvert("5.2", "bigdecimal", new BigDecimal("5.2")); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     }     @Test public void testConvertDoubleBigInteger() throws Exception {         helpConvert(new Double(1.0d), "biginteger", new BigInteger("1")); //\$NON-NLS-1\$ //\$NON-NLS-2\$     } ```
View Full Code Here

 `464465466467468469470` ```    @Test public void testConvertDoubleBigInteger() throws Exception {         helpConvert(new Double(1.0d), "biginteger", new BigInteger("1")); //\$NON-NLS-1\$ //\$NON-NLS-2\$     }     @Test public void testConvertFloatBigInteger() throws Exception {         helpConvert(new Float(1.0), "biginteger", new BigInteger("1")); //\$NON-NLS-1\$ //\$NON-NLS-2\$     } ```
View Full Code Here

 `468469470471472473474` ```    @Test public void testConvertFloatBigInteger() throws Exception {         helpConvert(new Float(1.0), "biginteger", new BigInteger("1")); //\$NON-NLS-1\$ //\$NON-NLS-2\$     }     @Test public void testConvertBigDecimalBigInteger() throws Exception {         helpConvert(new BigDecimal("1.0"), "biginteger", new BigInteger("1")); //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     } ```
View Full Code Here

 `1163116411651166116711681169` ```    @Test public void testMod3() {         assertEquals(-40, FunctionMethods.mod(-340, -60), 0);     }         @Test public void testMod4() {         assertEquals(new BigInteger("-1"), FunctionMethods.mod(new BigInteger("-3"), new BigInteger("2")));   //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     } ```
View Full Code Here

 `1167116811691170117111721173` ```    @Test public void testMod4() {         assertEquals(new BigInteger("-1"), FunctionMethods.mod(new BigInteger("-3"), new BigInteger("2")));   //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     }         @Test public void testMod5() {         assertEquals(new BigInteger("-1"), FunctionMethods.mod(new BigInteger("-3"), new BigInteger("-2")));   //\$NON-NLS-1\$ //\$NON-NLS-2\$ //\$NON-NLS-3\$     } ```
View Full Code Here