integer division without divide functionality

Division with integers in C, for testing 32 bit max as dividend.

Iโ€™ve found a code in some forum, but it had a bug. Nevertheless, the function implemented in C:

int DIV ( int dividend , int divisor ) {
  int q = 0;

  while (dividend >= divisor) {
    dividend -= divisor;
    q++;
  }

  return q;
}

Although, if time is critical and large numbers are plausible, this will be SLOW. A more sophisticated one:

tUI32 DIV_tester_UI ( tUI32 dividend, tUI32 divisor )
{
  tUI32 q = 0;
  tUI16 cnt = 0;
  tUI32 tmp = 0;
  tUI32 sft = 1;
  if (divisor != 0 && dividend != 0 && dividend >= divisor )
  {
    if (dividend == divisor)
    {
      q = 1;
    }
    else
    {
      while ( dividend > divisor )
      {
        tmp = dividend;

        while (tmp > divisor)
        {
          tmp = tmp >> 1;
          sft=sft < < 1;
          cnt++;
        }

        if ( tmp != divisor )
        {
          cnt--;
          sft = sft>>1;
        }

        q += sft;
        dividend = dividend - (divisor<<cnt);

        cnt = 0;
        sft = 1;
      }

      if ( dividend == divisor )
      {
        q += 1;
      }

    }
  }

  return q;
}


Encourage creation!

If this entry helped you, or you simply liked it, leave a tip.