Package de.tilman_neumann.jml.base
Class UnsignedBigInt
java.lang.Object
de.tilman_neumann.jml.base.UnsignedBigInt
public class UnsignedBigInt
extends java.lang.Object
A very limited unsigned big integer implementation.
Currently the only implemented arithmetic methods are division and modulus of big integers by small integers.
These methods are notably faster than using BigInteger.divide(BigInteger),
like factor 2.5 for BigIntegers with 100 bit, factor 1.8 at 200 bit, factor 1.6 at 300 bit.
-
Constructor Summary
Constructors Constructor Description UnsignedBigInt(int[] buffer)Constructor using the given buffer.UnsignedBigInt(UnsignedBigInt N)Copy constructorUnsignedBigInt(java.math.BigInteger N)Shortcut constructor fornew UnsignedBigInt(); set(N); -
Method Summary
Modifier and Type Method Description intbitLength()intdivideAndRemainder(int divisor, UnsignedBigInt quotient)Divide this by the givendivisor, store the quotient inquotientand return the remainder.intdivideAndRemainder_v1(int divisor, UnsignedBigInt quotient)Deprecated.booleanequals(java.lang.Object o)inthashCode()intintLength()intintValue()booleanisOne()Test for 1.booleanisZero()Test for 0.longlongValue()intmod(int divisor)Compute the remainder of this modulo divisor.voidset(java.math.BigInteger N)Sets this to the given BigInteger N.java.math.BigIntegertoBigInteger()java.lang.StringtoBinaryString()java.lang.StringtoString()Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Constructor Details
-
UnsignedBigInt
Copy constructor- Parameters:
N- original unsigned big int
-
UnsignedBigInt
public UnsignedBigInt(java.math.BigInteger N)Shortcut constructor fornew UnsignedBigInt(); set(N);- Parameters:
N-
-
UnsignedBigInt
public UnsignedBigInt(int[] buffer)Constructor using the given buffer. The numbers to work on must be set using theset(BigInteger)method.- Parameters:
buffer- a buffer big enough to represent all numbers that will be set usingset(BigInteger).
-
-
Method Details
-
set
public void set(java.math.BigInteger N)Sets this to the given BigInteger N. This method must be called before any arithmetic operation. If a buffer has been passed to the constructor then it should be big enough to represent N.- Parameters:
N-
-
isZero
public boolean isZero()Test for 0. The caller must make sure thatset(BigInteger)has been invoked before.- Returns:
- true if this==0, false else
-
isOne
public boolean isOne()Test for 1. The caller must make sure thatset(BigInteger)has been invoked before.- Returns:
- true if this==1, false else
-
intLength
public int intLength() -
bitLength
public int bitLength() -
divideAndRemainder_v1
Deprecated.Divide this by the givendivisor, store the quotient inquotientand return the remainder. The caller must make sure thatset(BigInteger)has been invoked before.- Parameters:
divisor-quotient- output- Returns:
- remainder
-
divideAndRemainder
Divide this by the givendivisor, store the quotient inquotientand return the remainder. The caller must make sure thatset(BigInteger)has been invoked before.- Parameters:
divisor-quotient- output- Returns:
- remainder
-
mod
public int mod(int divisor)Compute the remainder of this modulo divisor. The caller must make sure thatset(BigInteger)has been invoked before. This simple implementation seems to be amazingly fast, like 100 times faster than BigInteger.mod(d), where BigInteger d = BigInteger.valueOf(divisor) has been created before the performance test loop. Here, Barrett reduction has no chance to shine...- Parameters:
divisor-- Returns:
- remainder
-
equals
public boolean equals(java.lang.Object o)- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()- Overrides:
hashCodein classjava.lang.Object
-
intValue
public int intValue() -
longValue
public long longValue() -
toBigInteger
public java.math.BigInteger toBigInteger() -
toString
public java.lang.String toString()- Overrides:
toStringin classjava.lang.Object
-
toBinaryString
public java.lang.String toBinaryString()
-