1

I have this code:

public BigInteger getNum()
{
    BigInteger rtrnVal = 0;
    for (int a = _arr.Count; a > 0; a--)
    {
        rtrnVal = rtrnVal + (_arr[a] ? BigInteger.Pow(2, a) : 0);
    }
    return rtrnVal;
}

_arr is a List<bool>. In the specific testing circumstances, _arr is:

1001011100001010001100101111010010101100010011000101100000101100010111101001001101010100001001000001100001010010000011001110 with 1 being true and 0 being false respectively. It should return 12547898989848949849191494989874798798.

Why am I getting this overflow exception?

To be exact: I get the error on this line:

rtrnVal = rtrnVal + (_arr[a] ? BigInteger.Pow(2, a) : 0);

Edit: Here is the code that calls it:

Stopwatch st = new Stopwatch();
BigInteger x = 0;
numToBin a = new numToBin();

st.Start();

for(x = 0; x< 1000; x++)
{
    a = new numToBin(x);
    bool[] b = a.getBits();
}

long t1 = st.ElapsedMilliseconds;

st.Restart();

a = new numToBin(BigInteger.Parse("12547898989848949849191494989874798798"));
bool[] y = a.getBits();
foreach(bool z in y)
{
    Console.Write(z ? "1" : "0");
}
Console.WriteLine();

long t2 = st.ElapsedMilliseconds;

st.Restart();

a = new numToBin(y);
Console.WriteLine(a.getNum());

long t3 = st.ElapsedMilliseconds;

And so this leads me to think that the error is in the constructor:

 public numToBin(BigInteger n)
 {
      _arr = new List<bool>();
      int remainder;
      while (n != 0)
      {
           remainder = (int)(n % 2);
           n = n / 2;
           _arr.Add(remainder == 1 ? true : false);
      }
 }

But even there I think that the code is ok...

Edit2: Here is the stack trace:

 System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
 Parameter name: index
 at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument 
 argument, ExceptionResource resource)
 at System.Collections.Generic.List`1.get_Item(Int32 index)
 at Testing_app.Program.numToBin.getNum() in 
 C:\Users\MYNAME\Source\Repos\First project\Testing app\Program.cs:line 108
6
  • Can you share the stack trace of the exception? Commented Jun 11, 2017 at 2:50
  • Stack trace of the exception? Please tell me what that is. Commented Jun 11, 2017 at 2:55
  • Just looked it up... ok, I'm not at my computer rn so it'll have to wait till tmrw. Commented Jun 11, 2017 at 3:01
  • You seem to have an endian-ness problem... I assume the last digit of your binary number should be the smallest - if this is the case then rtrnVal = rtrnVal + (_arr[a] ? BigInteger.Pow(2, a) : 0); is doing the wrong thing. In the first iteration of your loop a = 124 giving you the last digit but you are then adding 2^a to your running total rather than just 1. Commented Jun 11, 2017 at 16:07
  • @Chris ok.. so then should a be equal to 124 - 1 on the iteration? Because 2^0 = 1. Commented Jun 11, 2017 at 17:17

1 Answer 1

1

I think the problem is elsewhere because I tried this code which will generate a greater number than the one you are expecting and is working fine:

class Program
{
    static void Main(string[] args)
    {
        Random r = new Random(1);
        bool[] _arr = new bool[1500];
        for (int i = 0; i < 1500; i++)
        {
            var x = true;//r.Next() == 1 ? true : false;
            _arr[i] = x;
        }

        BigInteger rtrnVal = 0;
        for (int a = _arr.Count() - 1; a >= 0; a--)
        {
            rtrnVal = rtrnVal + (_arr[a] ? BigInteger.Pow(2, a) : 0);
        }
        Console.WriteLine(rtrnVal);
        Console.ReadLine();
    }
}

As far as I can see the problem is in the constructor because you are initializing a new list for each operation inside for loop without mentioning here the while loop in constructor so there must be something going bad by creating all those objects. Try to simplify the code and remove it from constructor because that is not the place for bussines logic. Try to make a method to fill the data in the list and not by using constructor for that purpose. And make sure you are not creating a new list in each operation in for loop. Initialize the list in the header of the class rather than in while loop.

Sign up to request clarification or add additional context in comments.

2 Comments

Just out of curiosity/ why did you put for (int a = _arr.Count() - 1 ... )?
Ah just realized why... my bad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.