I have minimize cost of calculating modulus in C. say I have a number x and n is the number which will divide x
when n == 65536 (which happens to be 2^16):
m
If the constant with which you want to take the modulo is known at compile time and you have a decent compiler (e.g. gcc), tis usually best to let the compiler work its magic. Just declare the modulo const.
If you don't know the constant at compile time, but you are going to take - say - a billion modulos with the same number, then use this http://libdivide.com/