示例#1
0
文件: TestCommon.cs 项目: KSLcom/CBOR
 public static void AssertBigIntegersEqual(string a, BigInteger b)
 {
     Assert.AreEqual(a,b.ToString());
       BigInteger a2=BigInteger.fromString(a);
       Assert.AreEqual(a2,b);
       AssertEqualsHashCode(a2,b);
 }
示例#2
0
 public BitShiftAccumulator(BigInteger bigint,
                        int lastDiscarded,
                        int olderDiscarded
                       )
 {
     if (bigint.Sign < 0)
     throw new ArgumentException("bigint is negative");
       shiftedBigInt = bigint;
       discardedBitCount = new FastInteger(0);
       bitsAfterLeftmost = (olderDiscarded != 0) ? 1 : 0;
       bitLeftmost = (lastDiscarded != 0) ? 1 : 0;
 }
示例#3
0
 public DigitShiftAccumulator(BigInteger bigint,
                          int lastDiscarded,
                          int olderDiscarded
                         )
 {
     if(bigint.canFitInInt()){
     shiftedSmall=(int)bigint;
     if(shiftedSmall<0)
       throw new ArgumentException("bigint is negative");
     isSmall=true;
       } else {
     shiftedBigInt = bigint;
     isSmall = false;
       }
       bitsAfterLeftmost = (olderDiscarded != 0) ? 1 : 0;
       bitLeftmost = lastDiscarded;
 }
示例#4
0
 /// <summary> </summary>
 /// <param name='value'>A Decimal object.</param>
 /// <returns>A CBORObject object.</returns>
 public static CBORObject FromObject(decimal value)
 {
     if (Math.Round(value) == value) {
     // This is an integer
     if (value >= 0 && value <= UInt64.MaxValue) {
       return FromObject((ulong)value);
     } else if (value >= Int64.MinValue && value <= Int64.MaxValue) {
       return FromObject((long)value);
     } else {
       return FromObject(DecimalToBigInteger(value));
     }
       } else {
     int[] bits = Decimal.GetBits(value);
     byte[] data=new byte[13];
     data[0]=(byte)((bits[0])&0xFF);
     data[1]=(byte)((bits[0]>>8)&0xFF);
     data[2]=(byte)((bits[0]>>16)&0xFF);
     data[3]=(byte)((bits[0]>>24)&0xFF);
     data[4]=(byte)((bits[1])&0xFF);
     data[5]=(byte)((bits[1]>>8)&0xFF);
     data[6]=(byte)((bits[1]>>16)&0xFF);
     data[7]=(byte)((bits[1]>>24)&0xFF);
     data[8]=(byte)((bits[2])&0xFF);
     data[9]=(byte)((bits[2]>>8)&0xFF);
     data[10]=(byte)((bits[2]>>16)&0xFF);
     data[11]=(byte)((bits[2]>>24)&0xFF);
     data[12]=0;
     BigInteger mantissa=new BigInteger((byte[])data);
     bool negative = (bits[3] >> 31) != 0;
     int scale = (bits[3] >> 16) & 0xFF;
     if (negative) mantissa = -mantissa;
     return FromObjectAndTag(
       new CBORObject[]{
     FromObject(-scale),
     FromObject(mantissa)
       }, 4);
       }
 }
示例#5
0
 public static BigInteger Pow(BigInteger bigValue, int power){
   if((bigValue)==null)throw new ArgumentNullException("bigValue");
   if((power)<0)throw new ArgumentException("power"+" not greater or equal to "+"0"+" ("+Convert.ToString((power),System.Globalization.CultureInfo.InvariantCulture)+")");
   return bigValue.pow(power);
 }
示例#6
0
 /// <summary> </summary>
 /// <param name='bigintFirst'>A BigInteger object.</param>
 /// <param name='bigintSecond'>A BigInteger object.</param>
 /// <returns>A BigInteger object.</returns>
 public static BigInteger GreatestCommonDivisor(BigInteger bigintFirst, BigInteger bigintSecond){
   if((bigintFirst)==null)throw new ArgumentNullException("bigintFirst");
   return bigintFirst.gcd(bigintSecond);
 }
示例#7
0
 public static BigInteger Pow(BigInteger bigValue, BigInteger power){
   if((bigValue)==null)throw new ArgumentNullException("bigValue");
   if((power)==null)throw new ArgumentNullException("power");
   if((power.Sign)<0)throw new ArgumentException("power.Sign"+" not greater or equal to "+"0"+" ("+Convert.ToString((power.Sign),System.Globalization.CultureInfo.InvariantCulture)+")");
   BigInteger val=BigInteger.One;
   while(power.Sign>0){
     BigInteger p=(power>(BigInteger)5000000) ?
       (BigInteger)5000000 : power;
     val*=bigValue.pow((int)p);
     power-=p;
   }
   return val;
 }
示例#8
0
 /// <summary> Shifts a number until it reaches the given number of bits,
 /// gathering information on whether the last bit discarded is set and
 /// whether the discarded bits to the right of that bit are set. Assumes
 /// that the big integer being shifted is positive. </summary>
 private void ShiftBigToBits(int bits)
 {
     byte[] bytes = shiftedBigInt.ToByteArray();
       knownBitLength = ByteArrayBitLength(bytes);
       // Shift by the difference in bit length
       if (knownBitLength.CompareToInt(bits) > 0) {
     FastInteger bitShift = FastInteger.Copy(knownBitLength).SubtractInt(bits);
     FastInteger tmpBitShift = FastInteger.Copy(bitShift);
     while (tmpBitShift.Sign > 0 && !shiftedBigInt.IsZero) {
       int bs = tmpBitShift.MinInt32(1000000);
       shiftedBigInt >>= bs;
       tmpBitShift.SubtractInt(bs);
     }
     knownBitLength.SetInt(bits);
     if (bits < SmallBitLength) {
       // Shifting to small number of bits,
       // convert to small integer
       isSmall = true;
       shiftedSmall = (int)shiftedBigInt;
     }
     bitsAfterLeftmost |= bitLeftmost;
     discardedBitCount.Add(bitShift);
     for (int i = 0; i < bytes.Length; i++) {
       if (bitShift.CompareToInt(8)>0) {
     // Discard all the bits, they come
     // after the leftmost bit
     bitsAfterLeftmost |= bytes[i];
     bitShift.SubtractInt(8);
       } else {
     // 8 or fewer bits left.
     // Get the bottommost bitShift minus 1 bits
     bitsAfterLeftmost |= ((bytes[i] << (9 - bitShift.AsInt32())) & 0xFF);
     // Get the bit just above those bits
     bitLeftmost = (bytes[i] >> ((bitShift.AsInt32()) - 1)) & 0x01;
     break;
       }
     }
     bitsAfterLeftmost = (bitsAfterLeftmost != 0) ? 1 : 0;
       }
 }
示例#9
0
 /// <summary> </summary>
 /// <param name='dividend'>A BigInteger object.</param>
 /// <param name='divisor'>A BigInteger object.</param>
 /// <param name='remainder'>A BigInteger object.</param>
 /// <returns>A BigInteger object.</returns>
 public static BigInteger DivRem(BigInteger dividend, BigInteger divisor, out BigInteger remainder)
 {
   if((dividend)==null)throw new ArgumentNullException("dividend");
   BigInteger[] result=dividend.divideAndRemainder(divisor);
   remainder=result[1];
   return result[0];
 }
示例#10
0
 /// <summary> Does an OR operation between two BigInteger instances.
 /// </summary>
 /// <remarks>Each BigInteger instance is treated as a two's complement
 /// representation for the purposes of this operator.</remarks>
 /// <returns>A BigInteger object.</returns>
 /// <param name='first'>A BigInteger object.</param>
 /// <param name='second'>A BigInteger object.</param>
 public static BigInteger Or(BigInteger first, BigInteger second){
   if((first)==null)throw new ArgumentNullException("first");
   if((second)==null)throw new ArgumentNullException("second");
   BigInteger xa=new BigInteger().Allocate(first.wordCount);
   Array.Copy(first.reg,xa.reg,xa.reg.Length);
   BigInteger xb=new BigInteger().Allocate(second.wordCount);
   Array.Copy(second.reg,xb.reg,xb.reg.Length);
   xa.negative=first.negative;
   xa.wordCount=first.wordCount;
   xb.negative=second.negative;
   xb.wordCount=second.wordCount;
   xa.reg=CleanGrow(xa.reg,Math.Max(xa.reg.Length,xb.reg.Length));
   xb.reg=CleanGrow(xb.reg,Math.Max(xa.reg.Length,xb.reg.Length));
   if(xa.Sign<0){ TwosComplement(xa.reg,0,(int)xa.reg.Length); }
   if(xb.Sign<0){ TwosComplement(xb.reg,0,(int)xb.reg.Length); }
   xa.negative|=(xb.Sign<0);
   OrWords((xa.reg),(xa.reg),(xb.reg),(int)xa.reg.Length);
   if(xa.Sign<0){ TwosComplement(xa.reg,0,(int)xa.reg.Length); }
   xa.wordCount=xa.CalcWordCount();
   if(xa.wordCount==0)xa.negative=false;
   return xa;
 }
示例#11
0
 /// <summary> Calculates the remainder when a BigInteger raised to a
 /// certain power is divided by another BigInteger. </summary>
 /// <param name='bigintValue'>A BigInteger object.</param>
 /// <param name='pow'>A BigInteger object.</param>
 /// <param name='mod'>A BigInteger object.</param>
 /// <returns>(bigintValue^pow)%mod</returns>
 public static BigInteger ModPow(BigInteger bigintValue, BigInteger pow, BigInteger mod) {
   if ((bigintValue) == null) throw new ArgumentNullException("bigintValue");
   return bigintValue.ModPow(pow, mod);
 }
示例#12
0
 private static decimal BigIntegerToDecimal(BigInteger bi)
 {
     if(bi.Sign<0){
     if(bi.CompareTo(DecimalMinValue)<0)
       throw new OverflowException();
     bi=-bi;
     return EncodeDecimal(bi,0,true);
       }
       if(bi.CompareTo(DecimalMaxValue)>0)
     throw new OverflowException();
       return EncodeDecimal(bi,0,false);
 }
示例#13
0
 private static BigInteger DecimalToBigInteger(decimal dec)
 {
     int[] bits=Decimal.GetBits(dec);
       byte[] data=new byte[13];
       data[0]=(byte)((bits[0])&0xFF);
       data[1]=(byte)((bits[0]>>8)&0xFF);
       data[2]=(byte)((bits[0]>>16)&0xFF);
       data[3]=(byte)((bits[0]>>24)&0xFF);
       data[4]=(byte)((bits[1])&0xFF);
       data[5]=(byte)((bits[1]>>8)&0xFF);
       data[6]=(byte)((bits[1]>>16)&0xFF);
       data[7]=(byte)((bits[1]>>24)&0xFF);
       data[8]=(byte)((bits[2])&0xFF);
       data[9]=(byte)((bits[2]>>8)&0xFF);
       data[10]=(byte)((bits[2]>>16)&0xFF);
       data[11]=(byte)((bits[2]>>24)&0xFF);
       data[12]=0;
       int scale=(bits[3]>>16)&0xFF;
       BigInteger bigint=new BigInteger((byte[])data);
       for(int i=0;i<scale;i++){
     bigint/=(BigInteger)10;
       }
       if((bits[3]>>31)!=0){
     bigint=-bigint;
       }
       return bigint;
 }
示例#14
0
文件: CBORTest.cs 项目: KSLcom/CBOR
 public void TestBigInteger()
 {
     BigInteger bi = (BigInteger)3;
       AssertBigIntString("3", bi);
       BigInteger negseven = (BigInteger)(-7);
       AssertBigIntString("-7", negseven);
       BigInteger other = (BigInteger)(-898989);
       AssertBigIntString("-898989", other);
       other = (BigInteger)898989;
       AssertBigIntString("898989", other);
       for (int i = 0; i < 500; i++) {
     TestCommon.AssertSer(
       CBORObject.FromObject(bi),
       String.Format(CultureInfo.InvariantCulture, "{0}", bi));
     bi *= (BigInteger)negseven;
       }
       BigInteger[] ranges = new BigInteger[]{
     (BigInteger)Int64.MinValue-(BigInteger)512,
     (BigInteger)Int64.MinValue+(BigInteger)512,
     BigInteger.Zero-(BigInteger)512,
     BigInteger.Zero+(BigInteger)512,
     (BigInteger)Int64.MaxValue-(BigInteger)512,
     (BigInteger)Int64.MaxValue+(BigInteger)512,
     ((BigInteger.One<<64)-BigInteger.One)-(BigInteger)512,
     ((BigInteger.One<<64)-BigInteger.One)+(BigInteger)512,
       };
       for (int i = 0; i < ranges.Length; i += 2) {
     BigInteger bigintTemp = ranges[i];
     while (true) {
       TestCommon.AssertSer(
     CBORObject.FromObject(bigintTemp),
     String.Format(CultureInfo.InvariantCulture, "{0}", bigintTemp));
       if (bigintTemp.Equals(ranges[i + 1])) break;
       bigintTemp = bigintTemp + BigInteger.One;
     }
       }
 }
示例#15
0
 public bool Equals(BigInteger other)
 {
     return this.Equals((object)other);
 }
示例#16
0
文件: CBORTest.cs 项目: KSLcom/CBOR
 public void TestTags()
 {
     BigInteger maxuint = ((BigInteger.One << 64) - BigInteger.One);
       BigInteger[] ranges = new BigInteger[]{
     (BigInteger)6,
     (BigInteger)65539,
     (BigInteger)Int32.MaxValue-(BigInteger)500,
     (BigInteger)Int32.MaxValue+(BigInteger)500,
     (BigInteger)Int64.MaxValue-(BigInteger)500,
     (BigInteger)Int64.MaxValue+(BigInteger)500,
     ((BigInteger.One<<64)-BigInteger.One)-(BigInteger)500,
     maxuint,
       };
       for (int i = 0; i < ranges.Length; i += 2) {
     BigInteger bigintTemp = ranges[i];
     while (true) {
       CBORObject obj = CBORObject.FromObjectAndTag(0, bigintTemp);
       Assert.IsTrue(obj.IsTagged, "obj not tagged");
       BigInteger[] tags = obj.GetTags();
       Assert.AreEqual(1, tags.Length);
       Assert.AreEqual(bigintTemp, tags[0]);
       if (!obj.InnermostTag.Equals(bigintTemp))
     Assert.AreEqual(bigintTemp, obj.InnermostTag,
                     String.Format(CultureInfo.InvariantCulture,
                                   "obj tag doesn't match: {0}", obj));
       TestCommon.AssertSer(
     obj,
     String.Format(CultureInfo.InvariantCulture, "{0}(0)", bigintTemp));
       if (!(bigintTemp.Equals(maxuint))) {
     // Test multiple tags
     CBORObject obj2 = CBORObject.FromObjectAndTag(obj, bigintTemp + BigInteger.One);
     BigInteger[] bi = obj2.GetTags();
     if (bi.Length != 2)
       Assert.AreEqual(2, bi.Length,
                       String.Format(CultureInfo.InvariantCulture,
                                     "Expected 2 tags: {0}", obj2));
     if (!bi[0].Equals((BigInteger)bigintTemp + BigInteger.One))
       Assert.AreEqual(bigintTemp + BigInteger.One, bi[0],
                       String.Format(CultureInfo.InvariantCulture,
                                     "Outer tag doesn't match: {0}", obj2));
     if (!(bi[1].Equals((BigInteger)bigintTemp)))
       Assert.AreEqual(bigintTemp, bi[1],
                       String.Format(CultureInfo.InvariantCulture,
                                     "Inner tag doesn't match: {0}", obj2));
     if (!(obj2.InnermostTag.Equals((BigInteger)bigintTemp)))
       Assert.AreEqual(bigintTemp, obj2.InnermostTag,
                       String.Format(CultureInfo.InvariantCulture,
                                     "Innermost tag doesn't match: {0}", obj2));
     TestCommon.AssertSer(
       obj2,
       String.Format(CultureInfo.InvariantCulture, "{0}({1}(0))",
                     bigintTemp + BigInteger.One, bigintTemp));
       }
       if (bigintTemp.Equals(ranges[i + 1])) break;
       bigintTemp = bigintTemp + BigInteger.One;
     }
       }
 }
示例#17
0
文件: CBORTest.cs 项目: KSLcom/CBOR
 public void AssertBigIntString(string s, BigInteger bi)
 {
     Assert.AreEqual(s, bi.ToString());
 }
示例#18
0
文件: CBORTest.cs 项目: KSLcom/CBOR
 public void AssertAdd(BigInteger bi, BigInteger bi2, string s)
 {
     AssertBigIntString(s, bi + (BigInteger)bi2);
       AssertBigIntString(s, bi2 + (BigInteger)bi);
       BigInteger negbi = BigInteger.Zero-(BigInteger)bi;
       BigInteger negbi2 = BigInteger.Zero-(BigInteger)bi2;
       AssertBigIntString(s, bi - (BigInteger)negbi2);
       AssertBigIntString(s, bi2 - (BigInteger)negbi);
 }
示例#19
0
 /// <summary> </summary>
 /// <param name='other'>A BigInteger object.</param>
 /// <returns>A Boolean object.</returns>
 public bool Equals(BigInteger other) {
   if (other == null) return false;
   return this.CompareTo(other) == 0;
 }
示例#20
0
 internal static BigInteger FindPowerOfTenFromBig(BigInteger bigintExponent)
 {
     int sign=bigintExponent.Sign;
       if(sign<0)return BigInteger.Zero;
       if (sign == 0) return BigInteger.One;
       if (bigintExponent.CompareTo(BigInt36) <= 0) {
     return FindPowerOfTen((int)bigintExponent);
       }
       FastInteger intcurexp = FastInteger.FromBig(bigintExponent);
       BigInteger mantissa = BigInteger.One;
       BigInteger bigpow = BigInteger.Zero;
       while (intcurexp.Sign > 0) {
     if (intcurexp.CompareToInt(18) <= 0) {
       bigpow = FindPowerOfTen(intcurexp.AsInt32());
       mantissa *= (BigInteger)bigpow;
       break;
     } else if (intcurexp.CompareToInt(9999999) <= 0) {
       int val = intcurexp.AsInt32();
       bigpow = FindPowerOfFive(val);
       bigpow <<= val;
       mantissa *= (BigInteger)bigpow;
       break;
     } else {
       if (bigpow.IsZero) {
     bigpow = FindPowerOfFive(9999999);
     bigpow <<= 9999999;
       }
       mantissa *= bigpow;
       intcurexp.AddInt(-9999999);
     }
       }
       return mantissa;
 }
示例#21
0
 /// <summary> Returns a BigInteger with every bit flipped. </summary>
 /// <param name='a'>A BigInteger object.</param>
 /// <returns>A BigInteger object.</returns>
 public static BigInteger Not(BigInteger a){
   if((a)==null)throw new ArgumentNullException("a");
   BigInteger xa=new BigInteger().Allocate(a.wordCount);
   Array.Copy(a.reg,xa.reg,xa.reg.Length);
   xa.negative=a.negative;
   xa.wordCount=a.wordCount;
   if(xa.Sign<0){ TwosComplement(xa.reg,0,(int)xa.reg.Length); }
   xa.negative=!(xa.Sign<0);
   NotWords((xa.reg),(int)xa.reg.Length);
   if(xa.Sign<0){ TwosComplement(xa.reg,0,(int)xa.reg.Length); }
   xa.wordCount=xa.CalcWordCount();
   if(xa.wordCount==0)xa.negative=false;
   return xa;
 }
示例#22
0
 public BigInteger[] FindCachedPowerOrSmaller(BigInteger bi)
 {
     BigInteger[] ret=null;
     BigInteger minValue=BigInteger.Zero;
     lock(outputs){
       for(int i=0;i<size;i++){
     if(inputs[i].CompareTo(bi)<=0 && inputs[i].CompareTo(minValue)>=0){
       //Console.WriteLine("Have cached power ({0}, {1})",inputs[i],bi);
       ret=new BigInteger[]{inputs[i],outputs[i]};
       minValue=inputs[i];
     }
       }
     }
     return ret;
 }
示例#23
0
 /// <summary> </summary>
 /// <param name='a'>A BigInteger instance.</param>
 /// <param name='b'>Another BigInteger instance.</param>
 /// <remarks>Each BigInteger instance is treated as a two's complement
 /// representation for the purposes of this operator.</remarks>
 /// <returns>A BigInteger object.</returns>
 public static BigInteger Xor(BigInteger a, BigInteger b){
   if((a)==null)throw new ArgumentNullException("a");
   if((b)==null)throw new ArgumentNullException("b");
   BigInteger xa=new BigInteger().Allocate(a.wordCount);
   Array.Copy(a.reg,xa.reg,xa.reg.Length);
   BigInteger xb=new BigInteger().Allocate(b.wordCount);
   Array.Copy(b.reg,xb.reg,xb.reg.Length);
   xa.negative=a.negative;
   xa.wordCount=a.wordCount;
   xb.negative=b.negative;
   xb.wordCount=b.wordCount;
   xa.reg=CleanGrow(xa.reg,Math.Max(xa.reg.Length,xb.reg.Length));
   xb.reg=CleanGrow(xb.reg,Math.Max(xa.reg.Length,xb.reg.Length));
   if(xa.Sign<0){ TwosComplement(xa.reg,0,(int)xa.reg.Length); }
   if(xb.Sign<0){ TwosComplement(xb.reg,0,(int)xb.reg.Length); }
   xa.negative^=(xb.Sign<0);
   XorWords((xa.reg),(xa.reg),(xb.reg),(int)xa.reg.Length);
   if(xa.Sign<0){ TwosComplement(xa.reg,0,(int)xa.reg.Length); }
   xa.wordCount=xa.CalcWordCount();
   if(xa.wordCount==0)xa.negative=false;
   return xa;
 }
示例#24
0
 public static string BigIntToString(BigInteger bigint)
 {
     return bigint.ToString();
 }
示例#25
0
 internal static BigInteger FindPowerOfFiveFromBig(BigInteger diff)
 {
     int sign=diff.Sign;
       if(sign<0)return BigInteger.Zero;
       if (sign == 0) return BigInteger.One;
       FastInteger intcurexp = FastInteger.FromBig(diff);
       if (intcurexp.CompareToInt(54) <= 0) {
     return FindPowerOfFive(intcurexp.AsInt32());
       }
       BigInteger mantissa = BigInteger.One;
       BigInteger bigpow;
       BigInteger origdiff=diff;
       bigpow=powerOfFiveCache.GetCachedPower(origdiff);
       if(bigpow!=null)return bigpow;
       BigInteger[] otherPower=powerOfFiveCache.FindCachedPowerOrSmaller(origdiff);
       if(otherPower!=null){
     intcurexp.SubtractBig(otherPower[0]);
     bigpow=otherPower[1];
     mantissa=bigpow;
       } else {
     bigpow = BigInteger.Zero;
       }
       while (intcurexp.Sign > 0) {
     if (intcurexp.CompareToInt(27) <= 0) {
       bigpow = FindPowerOfFive(intcurexp.AsInt32());
       mantissa *= (BigInteger)bigpow;
       break;
     } else if (intcurexp.CompareToInt(9999999) <= 0) {
       bigpow = BigInteger.Pow(FindPowerOfFive(1), intcurexp.AsInt32());
       mantissa *= (BigInteger)bigpow;
       break;
     } else {
       if (bigpow.IsZero)
     bigpow = BigInteger.Pow(FindPowerOfFive(1), 9999999);
       mantissa *= bigpow;
       intcurexp.AddInt(-9999999);
     }
       }
       powerOfFiveCache.AddPower(origdiff,mantissa);
       return mantissa;
 }
示例#26
0
 private void ShiftRightBig(int bits)
 {
     if (bits <= 0) return;
       if (shiftedBigInt.IsZero) {
     discardedBitCount.AddInt(bits);
     bitsAfterLeftmost |= bitLeftmost;
     bitLeftmost = 0;
     knownBitLength=new FastInteger(1);
     return;
       }
       byte[] bytes = shiftedBigInt.ToByteArray();
       knownBitLength = ByteArrayBitLength(bytes);
       FastInteger bitDiff = new FastInteger(0);
       FastInteger bitShift=null;
       if (knownBitLength.CompareToInt(bits)<0) {
     bitDiff = new FastInteger(bits).Subtract(knownBitLength);
     bitShift=FastInteger.Copy(knownBitLength);
       } else {
     bitShift=new FastInteger(bits);
       }
       if (knownBitLength.CompareToInt(bits)<=0) {
     isSmall = true;
     shiftedSmall = 0;
     knownBitLength.SetInt(1);
       } else {
     FastInteger tmpBitShift = FastInteger.Copy(bitShift);
     while (tmpBitShift.Sign > 0 && !shiftedBigInt.IsZero) {
       int bs = tmpBitShift.MinInt32(1000000);
       shiftedBigInt >>= bs;
       tmpBitShift.SubtractInt(bs);
     }
     knownBitLength.Subtract(bitShift);
       }
       discardedBitCount.AddInt(bits);
       bitsAfterLeftmost |= bitLeftmost;
       for (int i = 0; i < bytes.Length; i++) {
     if (bitShift.CompareToInt(8)>0) {
       // Discard all the bits, they come
       // after the leftmost bit
       bitsAfterLeftmost |= bytes[i];
       bitShift.SubtractInt(8);
     } else {
       // 8 or fewer bits left.
       // Get the bottommost bitShift minus 1 bits
       bitsAfterLeftmost |= ((bytes[i] << (9 - bitShift.AsInt32())) & 0xFF);
       // Get the bit just above those bits
       bitLeftmost = (bytes[i] >> ((bitShift.AsInt32()) - 1)) & 0x01;
       break;
     }
       }
       bitsAfterLeftmost = (bitsAfterLeftmost != 0) ? 1 : 0;
       if (bitDiff.Sign > 0) {
     // Shifted more bits than the bit length
     bitsAfterLeftmost |= bitLeftmost;
     bitLeftmost = 0;
       }
 }
示例#27
0
 /// <summary> </summary>
 /// <param name='input'>A BigInteger object.</param>
 /// <param name='output'>A BigInteger object.</param>
 /// <returns></returns>
 public void AddPower(BigInteger input, BigInteger output)
 {
     lock(outputs){
       if(size<MaxSize){
     // Shift newer entries down
     for(int i=size;i>0;i--){
       inputs[i]=inputs[i-1];
       outputs[i]=outputs[i-1];
     }
     inputs[0]=input;
     outputs[0]=output;
     size++;
       } else {
     // Shift newer entries down
     for(int i=MaxSize-1;i>0;i--){
       inputs[i]=inputs[i-1];
       outputs[i]=outputs[i-1];
     }
     inputs[0]=input;
     outputs[0]=output;
       }
     }
 }
示例#28
0
 private static decimal EncodeDecimal(BigInteger bigmant,
                                  int scale, bool neg)
 {
     if((scale)<0)throw new ArgumentException("scale"+" not greater or equal to "+"0"+" ("+Convert.ToString((scale),System.Globalization.CultureInfo.InvariantCulture)+")");
     if((scale)>28)throw new ArgumentException("scale"+" not less or equal to "+"28"+" ("+Convert.ToString((scale),System.Globalization.CultureInfo.InvariantCulture)+")");
       byte[] data=bigmant.ToByteArray();
       int a=0;
       int b=0;
       int c=0;
       for(int i=0;i<Math.Min(4,data.Length);i++){
     a|=(((int)data[i])&0xFF)<<((i)*8);
       }
       for(int i=4;i<Math.Min(8,data.Length);i++){
     b|=(((int)data[i])&0xFF)<<((i-4)*8);
       }
       for(int i=8;i<Math.Min(12,data.Length);i++){
     c|=(((int)data[i])&0xFF)<<((i-8)*8);
       }
       int d = (scale << 16);
       if (neg) d |= (1 << 31);
       return new Decimal(new int[] { a, b, c, d });
 }
示例#29
0
 /// <summary> </summary>
 /// <param name='bi'>A BigInteger object.</param>
 /// <returns>A BigInteger object.</returns>
 public BigInteger GetCachedPower(BigInteger bi)
 {
     lock(outputs){
       for(int i=0;i<size;i++){
     if(bi.Equals(inputs[i])){
       if(i!=0){
     BigInteger tmp;
     // Move to head of cache if it isn't already
     tmp=inputs[i];inputs[i]=inputs[0];inputs[0]=tmp;
     tmp=outputs[i];outputs[i]=outputs[0];outputs[0]=tmp;
     // Move formerly newest to next newest
     if(i!=1){
       tmp=inputs[i];inputs[i]=inputs[1];inputs[1]=tmp;
       tmp=outputs[i];outputs[i]=outputs[1];outputs[1]=tmp;
     }
       }
       return outputs[0];
     }
       }
     }
     return null;
 }
示例#30
0
 public static BigInteger Abs(BigInteger thisValue){
   if((thisValue)==null)throw new ArgumentNullException("thisValue");
   return thisValue.abs();
 }