I need a division algorithm that can handle large integers (128 bits). I already asked how to do this using bit shift operators. However, my current implementation seems to require a better approach.
Basically, I save the numbers as two long long unsigned int in the format
A * 2 ^ 64 + B with B < 2 ^ 64 .
This number is divided by 24 , and I want to divide it by 24 .
My current approach is to convert it like
A * 2 ^ 64 + BAB -------------- = ---- * 2^64 + ---- 24 24 24 AA mod 24 BB mod 24 = floor( ---- ) * 2^64 + ---------- * 2^64 + floor( ---- ) + ---------- 24 24.0 24 24.0
However, this is a mistake.
(Note that the word A / 24 and mod is A % 24 Normal divisions are stored in long double , integers are stored in long long unsigned int .
Since 24 is equal to the binary value of 11000 , the second term should not change anything in the range of the fourth term, since it is shifted 64 bits to the left.
So, if A * 2 ^ 64 + B is divisible by 24, but B is not, it easily shows that it is erroneous, since it returns some non-integer number.
What is the error in my implementation?
source share