As the title suggests, I want to know how to perform the division operation and modular operation without any overflow.
Currently, I'm calculate it by dividing 2^63 by an random integer N, then doubling the value, and adding 1 to the value if (2^63 % N)<<1 is larger than the N.
int64 div, mod, N;
...
div = ((1<<63) / N) << 1;
mod = ((1<<63) % N) << 1;
if (mod >= N){
div++;
mod -= N;
}
As you can see, this process is very complicated, and it doesn't seem to be the optimal way.
Is there some more effective way to calculate it?
(I am a student living in Korea, so I am asking questions with the help of a translator. Please understand even if my English is not good enough.)