2034 if (modVal.equals(ONE)) 2035 return ONE; 2036 2037 MutableBigInteger a = new MutableBigInteger(modVal); 2038 MutableBigInteger b = new MutableBigInteger(m); 2039 2040 MutableBigInteger result = a.mutableModInverse(b); 2041 return result.toBigInteger(1); 2042 } 2043 2044 // Shift Operations 2045 2046 /** 2047 * Returns a BigInteger whose value is {@code (this << n)}. 2048 * The shift distance, {@code n}, may be negative, in which case 2049 * this method performs a right shift. 2050 * (Computes <tt>floor(this * 2<sup>n</sup>)</tt>.) 2051 * 2052 * @param n shift distance, in bits. 2053 * @return {@code this << n} 2054 * @see #shiftRight 2055 */ 2056 public BigInteger shiftLeft(int n) { 2057 if (signum == 0) 2058 return ZERO; 2059 if (n==0) 2060 return this; 2061 if (n<0) 2062 return shiftRight(-n); 2063 2064 int nInts = n >>> 5; 2065 int nBits = n & 0x1f; 2066 int magLen = mag.length; 2067 int newMag[] = null; 2068 2069 if (nBits == 0) { 2070 newMag = new int[magLen + nInts]; 2071 for (int i=0; i<magLen; i++) 2072 newMag[i] = mag[i]; 2073 } else { 2074 int i = 0; 2075 int nBits2 = 32 - nBits; 2076 int highBits = mag[0] >>> nBits2; 2077 if (highBits != 0) { 2078 newMag = new int[magLen + nInts + 1]; 2079 newMag[i++] = highBits; 2080 } else { 2081 newMag = new int[magLen + nInts]; 2082 } 2083 int j=0; 2084 while (j < magLen-1) 2085 newMag[i++] = mag[j++] << nBits | mag[j] >>> nBits2; 2086 newMag[i] = mag[j] << nBits; 2087 } 2088 2089 return new BigInteger(newMag, signum); 2090 } 2091 2092 /** 2093 * Returns a BigInteger whose value is {@code (this >> n)}. Sign 2094 * extension is performed. The shift distance, {@code n}, may be 2095 * negative, in which case this method performs a left shift. 2096 * (Computes <tt>floor(this / 2<sup>n</sup>)</tt>.) 2097 * 2098 * @param n shift distance, in bits. 2099 * @return {@code this >> n} 2100 * @see #shiftLeft 2101 */ 2102 public BigInteger shiftRight(int n) { 2103 if (n==0) 2104 return this; 2105 if (n<0) 2106 return shiftLeft(-n); 2107 2108 int nInts = n >>> 5; 2109 int nBits = n & 0x1f; 2110 int magLen = mag.length; 2111 int newMag[] = null; 2112 2113 // Special case: entire contents shifted off the end 2114 if (nInts >= magLen) 2115 return (signum >= 0 ? ZERO : negConst[1]); 2116 2117 if (nBits == 0) { 2118 int newMagLen = magLen - nInts; 2119 newMag = new int[newMagLen]; 2120 for (int i=0; i<newMagLen; i++) 2121 newMag[i] = mag[i]; 2122 } else { 2123 int i = 0; 2124 int highBits = mag[0] >>> nBits; 2125 if (highBits != 0) { 2126 newMag = new int[magLen - nInts]; | 2034 if (modVal.equals(ONE)) 2035 return ONE; 2036 2037 MutableBigInteger a = new MutableBigInteger(modVal); 2038 MutableBigInteger b = new MutableBigInteger(m); 2039 2040 MutableBigInteger result = a.mutableModInverse(b); 2041 return result.toBigInteger(1); 2042 } 2043 2044 // Shift Operations 2045 2046 /** 2047 * Returns a BigInteger whose value is {@code (this << n)}. 2048 * The shift distance, {@code n}, may be negative, in which case 2049 * this method performs a right shift. 2050 * (Computes <tt>floor(this * 2<sup>n</sup>)</tt>.) 2051 * 2052 * @param n shift distance, in bits. 2053 * @return {@code this << n} 2054 * @throws ArithmeticException if the shift distance is {@code 2055 * Integer.MIN_VALUE}. 2056 * @see #shiftRight 2057 */ 2058 public BigInteger shiftLeft(int n) { 2059 if (signum == 0) 2060 return ZERO; 2061 if (n==0) 2062 return this; 2063 if (n<0) { 2064 if (n == Integer.MIN_VALUE) { 2065 throw new ArithmeticException("Shift distance of Integer.MIN_VALUE not supported."); 2066 } else { 2067 return shiftRight(-n); 2068 } 2069 } 2070 2071 int nInts = n >>> 5; 2072 int nBits = n & 0x1f; 2073 int magLen = mag.length; 2074 int newMag[] = null; 2075 2076 if (nBits == 0) { 2077 newMag = new int[magLen + nInts]; 2078 for (int i=0; i<magLen; i++) 2079 newMag[i] = mag[i]; 2080 } else { 2081 int i = 0; 2082 int nBits2 = 32 - nBits; 2083 int highBits = mag[0] >>> nBits2; 2084 if (highBits != 0) { 2085 newMag = new int[magLen + nInts + 1]; 2086 newMag[i++] = highBits; 2087 } else { 2088 newMag = new int[magLen + nInts]; 2089 } 2090 int j=0; 2091 while (j < magLen-1) 2092 newMag[i++] = mag[j++] << nBits | mag[j] >>> nBits2; 2093 newMag[i] = mag[j] << nBits; 2094 } 2095 2096 return new BigInteger(newMag, signum); 2097 } 2098 2099 /** 2100 * Returns a BigInteger whose value is {@code (this >> n)}. Sign 2101 * extension is performed. The shift distance, {@code n}, may be 2102 * negative, in which case this method performs a left shift. 2103 * (Computes <tt>floor(this / 2<sup>n</sup>)</tt>.) 2104 * 2105 * @param n shift distance, in bits. 2106 * @return {@code this >> n} 2107 * @throws ArithmeticException if the shift distance is {@code 2108 * Integer.MIN_VALUE}. 2109 * @see #shiftLeft 2110 */ 2111 public BigInteger shiftRight(int n) { 2112 if (n==0) 2113 return this; 2114 if (n<0) { 2115 if (n == Integer.MIN_VALUE) { 2116 throw new ArithmeticException("Shift distance of Integer.MIN_VALUE not supported."); 2117 } else { 2118 return shiftLeft(-n); 2119 } 2120 } 2121 2122 int nInts = n >>> 5; 2123 int nBits = n & 0x1f; 2124 int magLen = mag.length; 2125 int newMag[] = null; 2126 2127 // Special case: entire contents shifted off the end 2128 if (nInts >= magLen) 2129 return (signum >= 0 ? ZERO : negConst[1]); 2130 2131 if (nBits == 0) { 2132 int newMagLen = magLen - nInts; 2133 newMag = new int[newMagLen]; 2134 for (int i=0; i<newMagLen; i++) 2135 newMag[i] = mag[i]; 2136 } else { 2137 int i = 0; 2138 int highBits = mag[0] >>> nBits; 2139 if (highBits != 0) { 2140 newMag = new int[magLen - nInts]; |