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

 `777879808182838485868788` ```          random = new Random(2010);         }         random.nextBytes(randomBytes);         randomBytes  &= 0x0f;  /* clear version        */         randomBytes  |= 0x40;  /* set to version 4     */         long msb = new BigInteger(randomBytes).longValue();         random.nextBytes(randomBytes);         randomBytes  &= 0x3f;  /* clear variant        */         randomBytes  |= 0x80;  /* set to IETF variant  */         long lsb = new BigInteger(randomBytes).longValue();         record.setUUID("mmuid:"+new UUID(msb, lsb)); //\$NON-NLS-1\$   } ```
View Full Code Here

 `190191192193194195196197198199200` ```            if(data.equals("N/A")) { //\$NON-NLS-1\$                 row.add(null);             } else if(i==1 || i==4 || i== 5 || i==6 || i==7) {                 row.add(Double.valueOf(data));             } else if(i==8) {                 row.add(new BigInteger(data));             } else if(i==2) {                 if(!data.equals("0")){ //\$NON-NLS-1\$                     try {                         Date date = DATE_FORMAT.parse(data);                         row.add(new java.sql.Date(date.getTime())); ```
View Full Code Here

 `157158159160161162163164` ```  }   public static String getEtag(byte[] bytes) {     byte[] digest = md.digest(bytes);     BigInteger number = new BigInteger(1, digest);     return '0' + number.toString(16);  // prepend a '0' to get a proper MD5 hash   } ```
View Full Code Here

 `949596979899100101102103104105106107108109110111112113114115116117118119120121122123124` ```                    break;                 case DOUBLE:                     this.sum = new Double(0);                     break;                 case BIG_INTEGER:                     this.sum = new BigInteger(String.valueOf(0));                     break;                 case BIG_DECIMAL:                     this.sum = new BigDecimal(0);                     break;             }         }                     switch(this.accumulatorType) {                    case LONG:                 this.sum = new Long(((Long)this.sum).longValue() + ((Number)input).longValue());                 break;             case DOUBLE:                 this.sum = new Double(((Double)this.sum).doubleValue() + ((Number)input).doubleValue());                 break;             case BIG_INTEGER:                 this.sum = ((BigInteger)this.sum).add( (BigInteger) input );                 break;             case BIG_DECIMAL:                 if (input instanceof BigInteger) {                     BigInteger bigIntegerInput = (BigInteger) input;                     this.sum = ((BigDecimal)this.sum).add( new BigDecimal(bigIntegerInput) );                 } else {                     this.sum = ((BigDecimal)this.sum).add( (BigDecimal) input );                 }                 break;    ```
View Full Code Here

 `6970717273747576777879` ```      KeyFactory myKeyFac=KeyFactory.getInstance("DH");       DHPublicKeySpec keySpec=new DHPublicKeySpec(f, p, g);       PublicKey yourPubKey=myKeyFac.generatePublic(keySpec);       myKeyAgree.doPhase(yourPubKey, true);       byte[] mySharedSecret=myKeyAgree.generateSecret();       K=new BigInteger(mySharedSecret);       K_array=K.toByteArray(); //System.err.println("K.signum(): "+K.signum()+ //       " "+Integer.toHexString(mySharedSecret&0xff)+ //       " "+Integer.toHexString(K_array&0xff)); ```
View Full Code Here

 `808182838485` ```       K_array=mySharedSecret;     }     return K_array;   }   public void setP(byte[] p){ setP(new BigInteger(p)); } ```
View Full Code Here

 `818283848586` ```      K_array=mySharedSecret;     }     return K_array;   }   public void setP(byte[] p){ setP(new BigInteger(p)); }   public void setG(byte[] g){ setG(new BigInteger(g)); } ```
View Full Code Here

 `828384858687` ```    }     return K_array;   }   public void setP(byte[] p){ setP(new BigInteger(p)); }   public void setG(byte[] g){ setG(new BigInteger(g)); }   public void setF(byte[] f){ setF(new BigInteger(f)); } ```
View Full Code Here

 `424344454647484950515253` ```    signature=java.security.Signature.getInstance("SHA1withDSA");     keyFactory=KeyFactory.getInstance("DSA");   }       public void setPubKey(byte[] y, byte[] p, byte[] q, byte[] g) throws Exception{     DSAPublicKeySpec dsaPubKeySpec =   new DSAPublicKeySpec(new BigInteger(y),            new BigInteger(p),            new BigInteger(q),            new BigInteger(g));     PublicKey pubKey=keyFactory.generatePublic(dsaPubKeySpec);     signature.initVerify(pubKey);   } ```
View Full Code Here

 `515253545556575859606162` ```    PublicKey pubKey=keyFactory.generatePublic(dsaPubKeySpec);     signature.initVerify(pubKey);   }   public void setPrvKey(byte[] x, byte[] p, byte[] q, byte[] g) throws Exception{     DSAPrivateKeySpec dsaPrivKeySpec =   new DSAPrivateKeySpec(new BigInteger(x),             new BigInteger(p),             new BigInteger(q),             new BigInteger(g));     PrivateKey prvKey = keyFactory.generatePrivate(dsaPrivKeySpec);     signature.initSign(prvKey);   } ```
View Full Code Here