It is told that modulo operator “%” and divide operator “/” are very inefficient in embedded C++.
How can I alternatively achieve the following expression:
a = b % c;
I understand that this can be achieved using the following logic:
a = b - c;
while (a >= c) {
a = a - c;
}
But my question is, is this code involving while loops efficient enough, compared to % operator?
Thanks,
Kirti
Nothing is going to be considerably more efficient than the
%operator. If there was a better way to do it, then any reasonable compiler would automatically convert it. When you’re told that%and/are inefficient, that’s just because those are difficult operations – if you need to perform a modulo, then do that.There may be special cases when there are better ways – for example, mod a power of two can be written as a binary or – but those are probably optimized by your compiler.