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

 `919293949596979899100101102103` ```        if (n < 0) {             throw new IllegalArgumentException(n + "<0");         } else if (n < 2) {             return BigInteger.ONE;         }         BigInteger x = new BigInteger("" + n);         BigInteger result = x;         for (int i = n - 1; i >= 2; i--) {             x = x.subtract(BigInteger.ONE);             result = result.multiply(x);         }         return result;     } ```
View Full Code Here

 `783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848` ```            else if (Float.class.equals(clazz))                 return new Float(number.floatValue());             else if (BigDecimal.class.equals(clazz))                 return new BigDecimal(number.doubleValue());             else if (BigInteger.class.equals(clazz))                 return new BigInteger(number.toString());            // else             // TODO log warn                       } else if (isDateTime(clazz)) {           return handleAsDate(clazz,value,customizeInfo,method);         } else if (clazz.isEnum()) {             String sValue = (value instanceof Map) ? ((String)((Map)value).get("_name")) : (String) value;             try{               return Enum.valueOf(clazz, sValue);             }catch(Throwable th){               // try in value fields               for(Object conzt: clazz.getEnumConstants()){             try{               // If xml value annotation use it.               if(clazz.getDeclaredField(((Enum)conzt).name()).                   getAnnotation(XmlEnumValue.class).value().equals(sValue)){                 return conzt;               }             }catch(Throwable t){};           }               // user error?               return null;             }         } else if (value instanceof String) {             String sValue = (String) value;             if(sValue.trim().isEmpty() && Number.class.isAssignableFrom(clazz)){               sValue = "0";               if(traceEnabled)traceLog.warn("Empty string passed for number. Converting it to zero. Field : " + method.getName());             }             if (String.class.equals(clazz))               return value;             else if (Boolean.TYPE.equals(clazz))                 return Boolean.parseBoolean(sValue);             else if (Boolean.class.equals(clazz))                 return Boolean.valueOf(sValue);             else if (Short.TYPE.equals(clazz)   || Short.class.equals(clazz))                 return Short.decode(sValue);             else if (Byte.TYPE.equals(clazz)   || Byte.class.equals(clazz))                 return Byte.decode(sValue);             else if (Integer.TYPE.equals(clazz) || Integer.class.equals(clazz))                 return Integer.decode(sValue);             else if (Long.TYPE.equals(clazz)  || Long.class.equals(clazz))                 return Long.decode(sValue);             else if (Float.TYPE.equals(clazz))                 return Float.parseFloat(sValue);             else if (Float.class.equals(clazz))                 return Float.valueOf(sValue);             else if (Double.TYPE.equals(clazz))                 return Double.parseDouble(sValue);             else if (Double.class.equals(clazz))                 return Double.valueOf(sValue);             else if (BigDecimal.class.equals(clazz))                 return new BigDecimal(sValue);             else if (BigInteger.class.equals(clazz))                 return new BigInteger(sValue);             else if (Character.TYPE.equals(clazz) || Character.class.equals(clazz)) {                 char charValue = 0;                 if (sValue.length() > 0) {                     charValue = sValue.charAt(0);                 } ```
View Full Code Here

 `117118119120121122123124125126` ```     private void testParseSpecialValues() throws SQLException {         for (int i = -10; i < 10; i++) {             testParseSpecialValue("" + ((long) Integer.MIN_VALUE + i));             testParseSpecialValue("" + ((long) Integer.MAX_VALUE + i));             BigInteger bi = BigInteger.valueOf(i);             testParseSpecialValue(bi.add(BigInteger.valueOf(Long.MIN_VALUE)).toString());             testParseSpecialValue(bi.add(BigInteger.valueOf(Long.MAX_VALUE)).toString());         }     } ```
View Full Code Here

 `612613614615616617618619620621622` ```                tuples[index].add(null);                 tuples[index].add(null);                 tuples[index].add(null);                 tuples[index].add(null);                 tuples[index].add(new Short((short)i));                 tuples[index].add(new BigInteger(i+"")); //\$NON-NLS-1\$                 tuples[index].add(new BigDecimal(i+".0")); //\$NON-NLS-1\$                 tuples[index].add(null);             }                         dataMgr.registerTuples(metadata, "bqt1.smalla", tuples); //\$NON-NLS-1\$ ```
View Full Code Here

 `63496350635163526353635463556356635763586359` ```                tuples[row].add(new Integer(row));  //IntNum                 tuples[row].add(String.valueOf(row)); //StringNum                 for(int col=0; col<10; col++) { //FloatNum, LongNum, DoubleNum, ByteNum, DateValue, TimeValue, TimestampValue, BooleanValue, CharValue, ShortValue                     tuples[row].add(null);                    }                    tuples[row].add(new BigInteger(String.valueOf(row))); //BigIntegerValue                 tuples[row].add(new BigDecimal(row)); //BigDecimalValue                 tuples[row].add(null);    //ObjectValue             }             dataMgr.registerTuples(metadata, groupName, tuples);         } ```
View Full Code Here

 `6404640564066407640864096410641164126413641464156416641764186419` ```        +"SELECT StringKey, (SELECT BigIntegerValue FROM BQT3.SmallA WHERE BQT3.SmallA.BigIntegerValue = BQT2.SmallA.StringNum) FROM BQT2.SmallA"; //\$NON-NLS-1\$                         // Create expected results         List[] expected = new List[] {             Arrays.asList(new Object[] { "0", new BigInteger("0") }), //\$NON-NLS-1\$  //\$NON-NLS-2\$             Arrays.asList(new Object[] { "1", new BigInteger("1") }), //\$NON-NLS-1\$  //\$NON-NLS-2\$             Arrays.asList(new Object[] { "2", new BigInteger("2") }), //\$NON-NLS-1\$  //\$NON-NLS-2\$             Arrays.asList(new Object[] { "0", new BigInteger("0") }), //\$NON-NLS-1\$  //\$NON-NLS-2\$             Arrays.asList(new Object[] { "1", new BigInteger("1") }), //\$NON-NLS-1\$  //\$NON-NLS-2\$             Arrays.asList(new Object[] { "2", new BigInteger("2") }), //\$NON-NLS-1\$  //\$NON-NLS-2\$         };                    // Construct data manager with data         FakeDataManager dataManager = new FakeDataManager();         sampleDataBQT2a(dataManager); ```
View Full Code Here

 `64396440644164426443644464456446644764486449` ```                String sql = "SELECT StringKey, BigIntegerValue FROM VQT.Defect15355 WHERE StringKey = '0'";  //\$NON-NLS-1\$                 // Create expected results         List[] expected = new List[] {             Arrays.asList(new Object[] { "0", new BigInteger("0") }), //\$NON-NLS-1\$  //\$NON-NLS-2\$         };                    // Construct data manager with data         FakeDataManager dataManager = new FakeDataManager();         sampleDataBQT2a(dataManager); ```
View Full Code Here

 `64696470647164726473647464756476647764786479` ```                String sql = "SELECT StringKey, BigIntegerValue FROM VQT.Defect15355a WHERE StringKey = '0'";  //\$NON-NLS-1\$                 // Create expected results         List[] expected = new List[] {             Arrays.asList(new Object[] { "0", new BigInteger("0") }), //\$NON-NLS-1\$  //\$NON-NLS-2\$         };                    // Construct data manager with data         FakeDataManager dataManager = new FakeDataManager();         sampleDataBQT2a(dataManager); ```
View Full Code Here

 `64996500650165026503650465056506650765086509` ```                String sql = "SELECT BigIntegerValue, StringKey FROM VQT.Defect15355 WHERE StringKey = '0'";  //\$NON-NLS-1\$                 // Create expected results         List[] expected = new List[] {             Arrays.asList(new Object[] { new BigInteger("0"), "0" }), //\$NON-NLS-1\$  //\$NON-NLS-2\$         };                    // Construct data manager with data         FakeDataManager dataManager = new FakeDataManager();         sampleDataBQT2a(dataManager); ```
View Full Code Here

 `65296530653165326533653465356536653765386539` ```                String sql = "SELECT BigIntegerValue FROM VQT.Defect15355 WHERE StringKey = '0'";  //\$NON-NLS-1\$                 // Create expected results         List[] expected = new List[] {             Arrays.asList(new Object[] { new BigInteger("0") }), //\$NON-NLS-1\$          };                    // Construct data manager with data         FakeDataManager dataManager = new FakeDataManager();         sampleDataBQT2a(dataManager); ```
View Full Code Here