EDIT: The answer is, bitwise operations on signed values does weird things!
During a debugging process, I noticed a weird discrepancy, and I have translated it into an easy to read example below.
It would seem to me that var1 and var2 should be identical: after carefully stepping through this on a debugger, it seems that var1 and var2 are identical for the first iteration, but diverge for the second. I discovered this bug while trying to convert the expression for "var2" into assembly, and noticed that my logical translation (which I've displayed with "var1") was giving different results. The calculation for "var1" is, to me, an identical unpicking of the complex expression for "var2" - where am I going wrong?
This was compiled with Visual Community 2019, x64, debug.
// x is an unsigned char, equivalent to the length of the string
// taking the null terminator into account
unsigned char var1 = x;
unsigned char var2 = x;
for (int i = 0; i < x; ++i) {
unsigned char temp1 = string[i];
unsigned char temp2 = var1 ^ temp1;
unsigned char temp3 = table[temp2];
var1 ^= temp3;
var2 ^= table[var2 ^ string[i]];
}
question from:https://stackoverflow.com/questions/65892253/two-logically-identical-c-instructions-give-different-results