r/AskComputerScience • u/Successful_Box_1007 • Sep 11 '25
Optimizing Division Algorithm
Hi everyone,
I just began learning about how algorithms work and programming works and I was just exposed to how we can have computers use bit shifting right to speed up division that we would otherwise need it to do by repeated subtraction method. But what happens if instead of dividing two integers that can be represented as powers of two, we instead have both integers not being powers of 2? How would a computer avoid having to use Euclidean repeated subtraction here if it cannot use the super fast right bit shift method?
Thanks so much!
7
u/tavianator Sep 11 '25
You can do long division! I wrote something about it once: https://tavianator.com/2022/long_division.html
5
u/teraflop Sep 11 '25
Well, you can just use the long division algorithm that you probably learned in grade school. This algorithm works just as well in binary as it does in base 10.
Suppose you're trying to divide 300 million by 3. You don't want to just repeatedly subtract 3 and count how many iterations it takes, because that would require 100 million subtractions.
Instead, you subtract the largest multiple of 3 by a power of 2 that you can. In other words, you shift the divisor 3 leftward by as many bits as you can without exceeding 300 million. That gives you one digit of the result, and you're left with a remainder. Keep going until the remainder is reduced to zero.
This is much faster than division by repeated subtraction, because no matter how large the dividend is, the amount of work you have to do is bounded by its number of digits, not the magnitude of the number itself.
Now, the actual division algorithm that a CPU uses internally when you give it a "divide" instruction may be more complicated. If you read the Wikipedia article on division algorithms you'll see that there are a lot of different approaches you can take. Some of these are more amenable than others to being implemented in low-level hardware using logic gates. I'm not a CPU design expert so that's as far as I'll speculate.
3
u/jeffbell Sep 11 '25
If you look at how the division hardware does it, it actually uses trinary bits. At each shift position you subtract the divisor from the dividend. If the result is result is positive you stick a +1 in the quotient. If it's negative you put a -1 value in that bit. Once you are done you normalize the bits back into a regular binary number in constant time.
This is an example where the speedup from the hardware is greater than what you can do by algorithm.
2
u/Falcon731 Sep 11 '25
For some numbers you can calculate a modulo reciprocal value - so a division becomes a multiply (For example in decimal you can do a divide by 2 by multiplying by 5 and dropping the last digit).
But in the general case you can't avoid doing long division with trial subtractions. There are some tricks you can do to avoid having to do the whole subtraction - but they get rather complex for a relatively modest gain.
2
u/TemporaryAbalone1171 Oct 18 '25
I mean if you want fast division, you can just use Newton-Raphson reciprocal division