As this answer in SOen , you can calculate the and quotient the rest of a split with a single IDIV
operation. That is, each of these 3 code snippets:
// Trecho 1
var quociente = x / y;
// Trecho 2
var resto = x % y;
// Trecho 3
var quociente = x / y;
var resto = x % y;
could at first be executed with only 1 instruction (that is, the two instructions in high-level code in section 3 would be translated into a single low-level instruction).
I say could , because I do not know how it is done in language-to-language practice: it is expected that compilers will be able to do this conversion correctly (perhaps except for part 3, where the difficulty in optimization is greater). But interpreted languages, languages with dynamic data types, etc. can implement this in another way. The only way to know for sure would be to parse the generated low level code (as the linked response did, for C ++).
P.S. I know that "module" ( mod
) is different from "rest" ( rem
) - there are circumstances where they are different, in particular involving negative numbers - but for purposes of that answer I considered them equivalent. >