My platform is x86_64, and assume there are 3 variables whose types are all uint16_t:
uint16_t a, b, c;
And for the following two code snippets:
(1)
uint16_t tmp = b - a;
uint16_t result1 = c - tmp;
and (2):
uint16_t result2 = c - (b - a);
For code snippet (1), the tmp value can be overflow result of b - a, also result1 value can be another overflow result of c - tmp.
For code snippet (2), if I understand correctly, for c - (b - a), a, b and c will all be promoted to integer first, so only c - (b - a) can be overflow when assigned to result2.
I wrote a small program to test whether the code snippet (1) is equivalent to (2):
#include <inttypes.h>
#include <stdio.h>
int main(void) {
uint16_t a, b, c;
for (a = 1; a <= 3; a++) {
for (b = 1; b <= 3; b++) {
for (c = 1; c <=3; c++) {
uint16_t tmp = b - a;
uint16_t result1 = c - tmp;
uint16_t result2 = c - (b - a);
if (result1 != result2) {
printf("error\n");
}
}
}
}
}
And the test result shows me the code snippet (1) is equivalent to (2). But I can't understand the mathematical rationale behind it, anyone can give an explanation of it?
a1is congruent toa2modulop(in this case 2^16), andb1is congruent tob2modulop, thena1+b1is congruent toa2+b2modulop, anda1-b1is congruent toa2-b2modulop.x = y mod ab => x = y mod a"? Thanks!