Binary division of large numbers

This is a discussion on Binary division of large numbers within the C Programming forums, part of the General Programming Boards category; Hi,
As a newbie to this forum can I first say that I'm far from an expert programmer. I write ...

Binary division of large numbers

Hi,

As a newbie to this forum can I first say that I'm far from an expert programmer. I write in C using gcc on a Linux machine, and I'm always surprised when something actually works! Please could anyone help me with a problem I can't see any way to resolve?

Following the instructions on the USPS website I have got as far as the correct calculation of a 102-digit binary number which I have stored in a short int array (not the best use of memory, but it works). In order to get this far I have already written functions which handle binary addition, subtraction and multiplication. The next step is that I need to divide. As an example (in hex):

016907B2A24ABC16A2E5C004B1 divided by 27C.

I will need both the quotient and the remainder. I have written code that will recursively take away until I get a negative number, and from it I can get the correct values, but it takes forever to do the calculation this way. I've looked at the 'long division' method of binary division but can't figure out how to put it into code.

Otherwise, I once implemented division thus, which should be quicker than your way:
1) Find the smallest number of digits that can contain the answer. For base-10, this is: (number_of_digits_in_numerator) - (number_of_digits_in_demoninator - 1) + 1 (for safety/rounding) This is the max. number of digits possible in your answer. (But may be less)
2) Start with all digits 0.
3) Take the most significant digit, increment it by one.
4) Multiply the number by the denominator.
5) If the result of the multiplication is larger than the numerator, reduce the digit that was incremented by 1, and move down to the next less significant place. Repeat to step 3 using this digit. If there is no less significant digit, you're done.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell

I'm afraid the Wikipedia article may as well be in Russian for all I understand of it.

I think you're on to something with that division method, however. It wouldn't cut down any processing time as it is because it would still have to work out 1000 * 5 as 5+5+5+5...., but if I combine it with a lookup table to take shortcuts (like "1000 * 5 makes 5000, so don't bother to work it out!") it might be just what I'm looking for. I'll play with it a bit and see if I can make it work.

Using this algorithm, all the multiplications should be simple (for base-10 or base-2) and not require bignumber work. After that, your bignum adder is called only as many times as there are digits in the smaller operand, minus 1. (No addition is needed in 1000 * 5.)

Again, try wiki. This article has info on multiplying. The method I've attempted to demonstrate is the first one, long multiplication. The "Peasant or binary multiplication" may be more useful to you. (I read that part, and it's easy enough to understand.)

And if it requires this much work with large numbers, perhaps you should think about libgmp, or some other library. No point in reinventing the wheel, and their work is likely going to be faster and less prone to bugs.