/* reduces this DBIG mod a BIG, and returns the BIG */ public virtual BIG Mod(BIG c) { int k = 0; Norm(); DBIG m = new DBIG(c); DBIG r = new DBIG(0); if (Comp(this, m) < 0) { return(new BIG(this)); } do { m.Shl(1); k++; } while (Comp(this, m) >= 0); while (k > 0) { m.Shr(1); r.Copy(this); r.Sub(m); r.Norm(); CMove(r, (int)(1 - ((r.w[BIG.DNLEN - 1] >> (BIG.CHUNK - 1)) & 1))); k--; } return(new BIG(this)); }
/* return this/c */ public virtual BIG Div(BIG c) { int d, k = 0; DBIG m = new DBIG(c); DBIG dr = new DBIG(0); BIG r = new BIG(0); BIG a = new BIG(0); BIG e = new BIG(1); Norm(); while (Comp(this, m) >= 0) { e.FShl(1); m.Shl(1); k++; } while (k > 0) { m.Shr(1); e.Shr(1); dr.Copy(this); dr.Sub(m); dr.Norm(); d = (int)(1 - ((dr.w[BIG.DNLEN - 1] >> (BIG.CHUNK - 1)) & 1)); CMove(dr, d); r.Copy(a); r.Add(e); r.Norm(); a.CMove(r, d); k--; } return(a); }
/* convert this to string */ public override string ToString() { DBIG b; string s = ""; int len = NBits(); if (len % 4 == 0) { len >>= 2; //len/=4; } else { len >>= 2; len++; } for (int i = len - 1; i >= 0; i--) { b = new DBIG(this); b.Shr(i * 4); s += ((int)(b.w[0] & 15)).ToString("x"); } return(s); }