/** * Converts this BigInteger to a {@code long}. This * conversion is analogous to a * <i>narrowing primitive conversion</i> from {@code long} to * {@code int} as defined in section 5.1.3 of * <cite>The Java™ Language Specification</cite>: * if this BigInteger is too big to fit in a * {@code long}, only the low-order 64 bits are returned. * Note that this conversion can lose information about the * overall magnitude of the BigInteger value as well as return a * result with the opposite sign. * * @return this BigInteger converted to a {@code long}. */ public long longValue() { long result = 0; for (int i=1; i>=0; i--) result = (result << 32) + (getInt(i) & LONG_MASK); return result; }
/** * Converts this BigInteger to an {@code int}. This * conversion is analogous to a * <i>narrowing primitive conversion</i> from {@code long} to * {@code int} as defined in section 5.1.3 of * <cite>The Java™ Language Specification</cite>: * if this BigInteger is too big to fit in an * {@code int}, only the low-order 32 bits are returned. * Note that this conversion can lose information about the * overall magnitude of the BigInteger value as well as return a * result with the opposite sign. * * @return this BigInteger converted to an {@code int}. */ public int intValue() { int result = 0; result = getInt(0); return result; }
/** * Converts this BigInteger to an {@code int}. This * conversion is analogous to a * <i>narrowing primitive conversion</i> from {@code long} to * {@code int} as defined in section 5.1.3 of * <cite>The Java™ Language Specification</cite>: * if this BigInteger is too big to fit in an * {@code int}, only the low-order 32 bits are returned. * Note that this conversion can lose information about the * overall magnitude of the BigInteger value as well as return a * result with the opposite sign. * * @return this BigInteger converted to an {@code int}. */ public int intValue() { int result = 0; result = getInt(0); return result; }
/** * Returns {@code true} if and only if the designated bit is set. * (Computes {@code ((this & (1<<n)) != 0)}.) * * @param n index of bit to test. * @return {@code true} if and only if the designated bit is set. * @throws ArithmeticException {@code n} is negative. */ public boolean testBit(int n) { if (n<0) throw new ArithmeticException("Negative bit address"); return (getInt(n >>> 5) & (1 << (n & 31))) != 0; }
/** * Returns {@code true} if and only if the designated bit is set. * (Computes {@code ((this & (1<<n)) != 0)}.) * * @param n index of bit to test. * @return {@code true} if and only if the designated bit is set. * @throws ArithmeticException {@code n} is negative. */ public boolean testBit(int n) { if (n<0) throw new ArithmeticException("Negative bit address"); return (getInt(n >>> 5) & (1 << (n & 31))) != 0; }
/** * Returns a BigInteger whose value is {@code (~this)}. (This method * returns a negative value if and only if this BigInteger is * non-negative.) * * @return {@code ~this} */ public BigInteger not() { int[] result = new int[intLength()]; for (int i=0; i<result.length; i++) result[i] = ~getInt(result.length-i-1); return valueOf(result); }
/** * Returns a BigInteger whose value is {@code (~this)}. (This method * returns a negative value if and only if this BigInteger is * non-negative.) * * @return {@code ~this} */ public BigInteger not() { int[] result = new int[intLength()]; for (int i=0; i<result.length; i++) result[i] = ~getInt(result.length-i-1); return valueOf(result); }
/** * Returns a BigInteger whose value is {@code (this | val)}. (This method * returns a negative BigInteger if and only if either this or val is * negative.) * * @param val value to be OR'ed with this BigInteger. * @return {@code this | val} */ public BigInteger or(BigInteger val) { int[] result = new int[Math.max(intLength(), val.intLength())]; for (int i=0; i<result.length; i++) result[i] = (getInt(result.length-i-1) | val.getInt(result.length-i-1)); return valueOf(result); }
/** * Returns a BigInteger whose value is {@code (this & val)}. (This * method returns a negative BigInteger if and only if this and val are * both negative.) * * @param val value to be AND'ed with this BigInteger. * @return {@code this & val} */ public BigInteger and(BigInteger val) { int[] result = new int[Math.max(intLength(), val.intLength())]; for (int i=0; i<result.length; i++) result[i] = (getInt(result.length-i-1) & val.getInt(result.length-i-1)); return valueOf(result); }
/** * Returns a BigInteger whose value is {@code (this ^ val)}. (This method * returns a negative BigInteger if and only if exactly one of this and * val are negative.) * * @param val value to be XOR'ed with this BigInteger. * @return {@code this ^ val} */ public BigInteger xor(BigInteger val) { int[] result = new int[Math.max(intLength(), val.intLength())]; for (int i=0; i<result.length; i++) result[i] = (getInt(result.length-i-1) ^ val.getInt(result.length-i-1)); return valueOf(result); }
/** * Returns a BigInteger whose value is {@code (this ^ val)}. (This method * returns a negative BigInteger if and only if exactly one of this and * val are negative.) * * @param val value to be XOR'ed with this BigInteger. * @return {@code this ^ val} */ public BigInteger xor(BigInteger val) { int[] result = new int[Math.max(intLength(), val.intLength())]; for (int i=0; i<result.length; i++) result[i] = (getInt(result.length-i-1) ^ val.getInt(result.length-i-1)); return valueOf(result); }
/** * Returns a BigInteger whose value is {@code (this & ~val)}. This * method, which is equivalent to {@code and(val.not())}, is provided as * a convenience for masking operations. (This method returns a negative * BigInteger if and only if {@code this} is negative and {@code val} is * positive.) * * @param val value to be complemented and AND'ed with this BigInteger. * @return {@code this & ~val} */ public BigInteger andNot(BigInteger val) { int[] result = new int[Math.max(intLength(), val.intLength())]; for (int i=0; i<result.length; i++) result[i] = (getInt(result.length-i-1) & ~val.getInt(result.length-i-1)); return valueOf(result); }
/** * Returns a BigInteger whose value is {@code (this & val)}. (This * method returns a negative BigInteger if and only if this and val are * both negative.) * * @param val value to be AND'ed with this BigInteger. * @return {@code this & val} */ public BigInteger and(BigInteger val) { int[] result = new int[Math.max(intLength(), val.intLength())]; for (int i=0; i<result.length; i++) result[i] = (getInt(result.length-i-1) & val.getInt(result.length-i-1)); return valueOf(result); }
/** * Returns a BigInteger whose value is {@code (this | val)}. (This method * returns a negative BigInteger if and only if either this or val is * negative.) * * @param val value to be OR'ed with this BigInteger. * @return {@code this | val} */ public BigInteger or(BigInteger val) { int[] result = new int[Math.max(intLength(), val.intLength())]; for (int i=0; i<result.length; i++) result[i] = (getInt(result.length-i-1) | val.getInt(result.length-i-1)); return valueOf(result); }
/** * Returns a BigInteger whose value is {@code (this & ~val)}. This * method, which is equivalent to {@code and(val.not())}, is provided as * a convenience for masking operations. (This method returns a negative * BigInteger if and only if {@code this} is negative and {@code val} is * positive.) * * @param val value to be complemented and AND'ed with this BigInteger. * @return {@code this & ~val} */ public BigInteger andNot(BigInteger val) { int[] result = new int[Math.max(intLength(), val.intLength())]; for (int i=0; i<result.length; i++) result[i] = (getInt(result.length-i-1) & ~val.getInt(result.length-i-1)); return valueOf(result); }
/** * Returns a BigInteger whose value is equivalent to this BigInteger * with the designated bit flipped. * (Computes {@code (this ^ (1<<n))}.) * * @param n index of bit to flip. * @return {@code this ^ (1<<n)} * @throws ArithmeticException {@code n} is negative. */ public BigInteger flipBit(int n) { if (n<0) throw new ArithmeticException("Negative bit address"); int intNum = n >>> 5; int[] result = new int[Math.max(intLength(), intNum+2)]; for (int i=0; i<result.length; i++) result[result.length-i-1] = getInt(i); result[result.length-intNum-1] ^= (1 << (n & 31)); return valueOf(result); }
/** * Returns a BigInteger whose value is equivalent to this BigInteger * with the designated bit set. (Computes {@code (this | (1<<n))}.) * * @param n index of bit to set. * @return {@code this | (1<<n)} * @throws ArithmeticException {@code n} is negative. */ public BigInteger setBit(int n) { if (n<0) throw new ArithmeticException("Negative bit address"); int intNum = n >>> 5; int[] result = new int[Math.max(intLength(), intNum+2)]; for (int i=0; i<result.length; i++) result[result.length-i-1] = getInt(i); result[result.length-intNum-1] |= (1 << (n & 31)); return valueOf(result); }
/** * Returns a BigInteger whose value is equivalent to this BigInteger * with the designated bit cleared. * (Computes {@code (this & ~(1<<n))}.) * * @param n index of bit to clear. * @return {@code this & ~(1<<n)} * @throws ArithmeticException {@code n} is negative. */ public BigInteger clearBit(int n) { if (n<0) throw new ArithmeticException("Negative bit address"); int intNum = n >>> 5; int[] result = new int[Math.max(intLength(), ((n + 1) >>> 5) + 1)]; for (int i=0; i<result.length; i++) result[result.length-i-1] = getInt(i); result[result.length-intNum-1] &= ~(1 << (n & 31)); return valueOf(result); }
/** * Returns a BigInteger whose value is equivalent to this BigInteger * with the designated bit set. (Computes {@code (this | (1<<n))}.) * * @param n index of bit to set. * @return {@code this | (1<<n)} * @throws ArithmeticException {@code n} is negative. */ public BigInteger setBit(int n) { if (n<0) throw new ArithmeticException("Negative bit address"); int intNum = n >>> 5; int[] result = new int[Math.max(intLength(), intNum+2)]; for (int i=0; i<result.length; i++) result[result.length-i-1] = getInt(i); result[result.length-intNum-1] |= (1 << (n & 31)); return valueOf(result); }
/** * Returns a BigInteger whose value is equivalent to this BigInteger * with the designated bit cleared. * (Computes {@code (this & ~(1<<n))}.) * * @param n index of bit to clear. * @return {@code this & ~(1<<n)} * @throws ArithmeticException {@code n} is negative. */ public BigInteger clearBit(int n) { if (n<0) throw new ArithmeticException("Negative bit address"); int intNum = n >>> 5; int[] result = new int[Math.max(intLength(), ((n + 1) >>> 5) + 1)]; for (int i=0; i<result.length; i++) result[result.length-i-1] = getInt(i); result[result.length-intNum-1] &= ~(1 << (n & 31)); return valueOf(result); }