Listing 3: Multiplication of BigNums


//multiply two bignums
BigNum BigNum::operator*(const BigNum & b){
  BigNum product;
  product.exp = this->exp + b.exp + 1;
  delete[] product.x;  product.x = new int[product.exp+1];
  product.positive = (this->positive==b.positive);
  for (int j = 0; j<=product.exp; j++) product.x[j] = 0;

  long carry;
  for(j=0;j<=b.exp;j++){
     carry = 0;
     for(int i=0;i<=this->exp;i++){
        long t = this->x[i];
        t = t * long(b.x[j]) + product.x[i+j] + carry;
        product.x[i+j] = t % BASE; carry = t/BASE;
     }
     product.x[i+j] = carry;
  }
  while(!product.x[product.exp] && product.exp) product.exp--;
  return product;
}
//End of File