I have a machine that only supports 32-bit operations, it has not been working on this computer for a long time. I have one 64-bit quantity represented as two unsigned int 32. The question is how can I execute mod on this 64-bit quantity with a 32-bit divider.
r = a mod b
Where:
a is a 64-bit value, and b is a 32-bit value
I thought I could imagine part of the mod by doing: a = a1 * (2 ^ 32) + a2 (where a1 is the upper bits, a2 is the lower bits)
(a1 * (2 ^ 32) + a2) mod b = ((a1 * 2 ^ 32) mod b + a2 mod b) mod b
((a1 * 2 ^ 32) mod b + a2 mod b) mod b = (a1 mod b * 2 ^ 32 mod b + a2 mod b) mod b
but the problem is that 2 ^ 32 mod b can sometimes be equal to 2 ^ 32, and therefore the multiplication will overflow. I looked at trying to convert multiplication to complement, but it also requires me to use 2 ^ 32, which if my mod gave me 2 ^ 32 again :), so I'm not sure how to execute an unsigned mod of a 64-bit value with 32 bit.
I assume that a simple solution would be to perform the following operations:
but I'm not sure how to combine these two integers together to form a 64-bit value
Just to be complete, here are some links for binary division and subtraction: http://www.exploringbinary.com/binary-division/
and description of the binary division algorithm: http://en.wikipedia.org/wiki/Division_algorithm
source share