I want to know how to obtain the remainder by dividing an integer with another integer (both positive) using bitshift or bitwise operators only. The /
operator or %
operator should not be used.
For example, for obtaining the remainder when divisor is of the form 2^k
the following operation yields the remainder.
m = Remainder
n = The number
d = The divisor
m = n & ( d - 1 )
However this method works only when d
is of the form 2^k
. I want to know a similar method for non-powers of 2
. I am currently working on a problem from programming challenges
and want to employ such a method to reduce program execution time