Full Version : Notes on 16 by 8 Binary Division
avr >>COMPUTER MATH >>Notes on 16 by 8 Binary Division


Admin3- 04-18-2006
NOTES ON 16x8 BINARY DIVISION:

QUOTE

I'm looking for code to divide a 16 bit unsigned value by a 8 bit unsigned value, giving me the result and the remainder. I've been searching the
Internet for this, but I can't seem to find one that works.


You could simply implement the definition of division where
the quotient is the number of times the divisor can be subtracted from the
divident, and the remainder is whatever's left over from those subtractions.
Works every time though it's a bit slow.


BINARY LONG DIVISION:

Also it's fairly easy to implement long division and save quite a bit of time
over the above method. Essentially you count and subtract multiples of the
divisor from the dividend so that each subtraction removes a significant
number of divisor subtractions at a time. Note that in digital computers
that multiples of powers of two are cheaply gained by shifting the divisor
a number of bits to the left.

Let take for example 60001/6. By the first method you'd subtract 6 from
60001 10000 times giving a quotient of 10000 and a remainder of 1.

Now let's take a stab at the second method. First we initialize a mask with the
value 1, which represents the multiple of the divisor we're going to subtract.

mask = 1;

Now we shift the divisor and mask each one bit to the left until either
the divisor is larger than the dividend, or until the mask or
divisor overflows.

CODE
divisor   mask
--------------
   12      2
   24      4
   48      8
   96     16
  192     32
  384     64
  768    128
 1536    256
 3072    512
 6144   1024
12288   2048
24576   4096
49152   8192
32768  16384


Divisor overflows so rotate back to 49152.

So 49152 is the largest power of 2 multiple of the divisor that goes into
60001. So next we mark in the quotient that we're subtracting 8192 divisors
from the dividend by marking the current mask bit (8192) in the quotient.

And do the actual subtract. 60001-49152 -> 10849

Now we work in the opposite direction shifting the divisor and mask to the
right one bit and checking if that value can be subtracted from the remaining
dividend. Note that 24576 and 12288 cannot be subtracted, but that 6144 can.
So we mark the 1024 bit in the quotient and subtract the 6144 from 10848
10849 - 6144 -> 4705.

3072 can be substracted from 4704. We mark the 512 bit in the quotient and
subtract the 3072 4705-3072 -> 1633.

Same for the 1536 1632-1536 -> 97. Marking the 256 bit in the process.

Next up is 96. 97-96 -> 1. So mark the 16 bit in the quotient and do the
subtraction.

Finally the mask will underflow and become 0 (shifting the 1 bit out into
the carry). This completes the operation.

The quotient is the sum of the marked bits: 8192+1024+512+256+16 -> 10000
The remainder is what's left over in the dividend: 1.

Note that instead of 10000 subtracts, the second process required 28 shifts
and 28 subtracts along with some other misc. operations, This represents
a significant speedup over the brute force method.

But it's just regular ordinary long division that just happens to be done
in binary.


Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.