title: Overflow and Underflow BigIntegers in C#
body: <p>I have the following piece of code which i'm trying to comprehend.</p>
  
  <p>The Big Integer class (custom, not the standard class) has the following members:</p>
  
  <pre><code>    private const int maxLength = 2000;
      private uint[] data = null;             // stores bytes from the Big Integer
      public int dataLength;                 // number of actual chars used
  </code></pre>
  
  <p>The constructor is as follows:</p>
  
  <pre><code>public BigInteger(long value)
      {
              data = new uint[maxLength];
              long tempVal = value;
  
              // copy bytes from long to BigInteger without any assumption of
              // the length of the long datatype
  
              dataLength = 0;
              while(value != 0 &amp;&amp; dataLength &lt; maxLength)
              {
                      data[dataLength] = (uint)(value &amp; 0xFFFFFFFF);
                      value &gt;&gt;= 32;
                      dataLength++;
              }
  
              if(tempVal &gt; 0)         // overflow check for +ve value
              {
                      if(value != 0 || (data[maxLength-1] &amp; 0x80000000) != 0)
                              throw(new ArithmeticException("Positive overflow in constructor."));
              }
              else if(tempVal &lt; 0)    // underflow check for -ve value
              {
                      if(value != -1 || (data[dataLength-1] &amp; 0x80000000) == 0)
                              throw(new ArithmeticException("Negative underflow in constructor."));
              }
  </code></pre>
  
  <p>Essentially, the BigInteger class aids in manipulating large integers. The constructor is used to assign a long value to a Big Integer object. I understand that the variable named value is copied to the uint[] data array (the maximum number of digits allowed being maxLength). </p>
  
  <p>I'm not able to completely understand how the check for underflow and overflow has been made. I understand the conditions if(tempVal > 0) and else if(tempVal &lt; 0) but why is the highest array index of data anded with 0x80000000 in case of both underflow and overflow? (why not 0xFFFFFFFF in the case of overflow/underflow? If the number is even 1 more/less than the maximum number allowed, it will result in an overflow/underflow)</p>
  
  <p>Also, doesn't passing a long value to the constructor limit the overall size of the BigInteger (whose maximum value can then be the same as the maximum value of a long variable)?</p>