Mega Code Archive

 
Categories / Delphi / Algorithm Math
 

Using Big Integers (32, 64, 128, 256... bit)

Title: Using Big Integers (32, 64, 128, 256... bit) Question: How can I use very large (32 bit or more) integers? Answer: Check out the Big Integer units: http://home.student.utwente.nl/e.j.molendijk/index.html I will not list the source here, since it is way to big. But a few of the included functions: procedure BIntAdd(Var Dest : TBInt; Src : TBInt); overload; procedure BIntAdd(Var Dest : TBInt; Const Src : Cardinal=1); overload; {================================================================ BIntSub: Dest := Dest - Src; 1. BIntSub makes sure that Dest and Src have the same memory size 2. If the result is negative. A Two's Complement notation is returned. 3. Src can also be an unsigned integer, optimezed routines will be used in this case. Calling BIntAdd without the Src parameter results in Dest := Dest - 1 You can/should call BSub or BDec directly when you are sure that the conditions specified at step 1 are met. } procedure BIntSub(Var Dest : TBInt; Src : TBInt); overload; procedure BIntSub(Var Dest : TBInt; Src : Cardinal=1); overload; {================================================================ BIntMul: Dest := Src1 * Src2; 1. BIntAdd makes sure that Dest can is big enough to hold the result; And that Dest, Src1 and Src2 are all 32 bit alligned. 2. Dest will never overflow You can/should call BMul directly when you are sure that the conditions specified at step 1 are met. } procedure BIntMul(Var Dest : TBInt; Src1, Src2 : TBInt); overload; procedure BIntMul(Var Dest : TBInt; Src1 : TBInt; Src2 : Cardinal); overload; procedure BIntDivMod(x,y : TBInt; Var q,r : TBInt); {================================================================ BIntAbs: A := Abs(A); Returns the absolute value of A } Procedure BIntAbs(Var A : TBInt); {================================================================ BIntCmp: Compare Src1 and Src2. return: Equal, Smaller or Greater. 1. BIntCmp makes sure that Src1 and Src2 are of same size and both 32 bit alligned. You can/should call BCmp directly when you are sure that the conditions specified at step 1 are met. } Function BIntCmp(Src1, Src2 : TBInt) : TBIntEq; overload; Function BIntCmp(Src1 : TBInt; Src2 : Integer) : TBIntEq; overload; {================================================================ BIntCopy: Dest := Src; 1. Makes sure Dest is big enough to hold Src } procedure BIntCopy(Var Dst : TBInt; Const Src : TBInt); {================================================================ BIntSetBit: Dest := Dest or (1 shl b); or BIntGetBit: Result := Bit at position b in A. Sets bit at position b to a one. Bit position b must be withing the memory size of Dest, otherwise it will not be set. 0 } procedure BIntSetBit(Var a : TBInt; b : Integer); Function BIntGetBit(Var a : TBInt; b : Integer) : Integer; {================================================================ See comment in implementation for more details about the following internal routines } procedure BIntReduce(Var A : TBInt); Procedure BIntBurn(Var A : TBInt); procedure BIntChk(Var A, B : TBInt); Procedure BIntChkIncOverflow(Var A : TBInt); Procedure BIntChkSumOverflow(Var A,B : TBInt); Procedure BIntChkMulOverflow(Var A,B,C : TBInt); Function BIntGetBitLength(BInt : TBInt) : Integer; Procedure BIntSetBitLength(Var BInt : TBInt; Bits : Integer); Function BIntNegative(Const A : TBInt) : Boolean;