Division Algorithms: Vikas Maheshkar MNNIT, Allahabad
Division Algorithms: Vikas Maheshkar MNNIT, Allahabad
Vikas Maheshkar
MNNIT, Allahabad
Rules for Division
2
Algorithms for Division
3
Algorithms for Division
4
A restoring-division example
7
Signed Division
• Simplest solution
– Negate the quotient if the signs of divisor and
dividend disagree
– Remainder and dividend must have the same signs
• Remainder = (Dividend – Quotient * Divisor)
(+7) / (+3): Q = 2; R = 1
(-7) / (+3): Q = -2; R = -1
(+7) / (-3): Q = -2; R = 1
(-7) / (-3): Q = 2; R = -1
Signed Division Algorithm
START A, Q Dividend
M Divisor, Count n
Shift left: A, Q
S A31, Count Count - 1
No Yes
A A+M M31 = A31? A A-M
No S = A31? Yes
No Yes Quotient in Q
Count = END Remainder in A
0?
Examples (1/2)
A Q M = 0011 A Q M = 1101
0000 0111 Initial values 0000 0111 Initial values
Shift Shift
0000 1110 Subtract 1 0000 1110 Add 1
1101 Restore 1101 Restore
0000 1110 Shift 0000 1110 Shift
0001 1100 Subtract 2 0001 1100 Add 2
1110 Restore 1110 Restore
0001 1100 0001 1100
Shift Shift
0011 1000 Subtract 3 0011 1000 Add 3
0000 Q0 = 1 0000 Q0 = 1
0000 1001 0000 1001
Shift Shift
0001 0010 Subtract 4 0001 0010 Add 4
1110 Restore 1110 Restore
0001 0010 0001 0010
(7) / (3) (7) / (-3)
Examples (2/2)
A Q M = 0011 A Q M = 1101
1111 1001 Initial values 1111 1001 Initial values
Shift Shift
1111 0010 Add 1 1111 0010 Subtract 1
0010 Restore 0010 Restore
1111 0010 Shift 1111 0010 Shift
1110 0100 Add 2 1110 0100 Subtract 2
0001 Restore 0001 Restore
1110 0100 1110 0100
Shift Shift
1100 1000 Add 3 1100 1000 Subtract 3
1111 Q0 = 1 1111 Q0 = 1
1111 1001 1111 1001
Shift Shift
1111 0010 Add 4 1111 0010 Subtract 4
0010 Restore 0010 Restore
1111 0010 1111 0010
(-7) / (3) (-7) / (-3)