I am migrating some parts of old C++ code, originally compiled with CodeGear C++Builder® 2009 Version 12.0.3170.16989
The following code - minimal version of a bigger piece - outputs -34
with any modern compiler. Although, in the original platform it outputs 84
:
char Key[4];
Key[0] = 0x1F;
Key[1] = 0x01;
Key[2] = 0x8B;
Key[3] = 0x55;
for(int i = 0; i < 2; i++) {
Key[i] = Key[2*i] ^ Key[2*i + 1];
}
std::cout << (int) Key[1] << std::endl;
The following code outputs -34
with both old and new compilers:
for(int i = 0; i < 2; i++) {
char a = Key[2*i];
char b = Key[2*i + 1];
char c = a ^ b;
Key[i] = c;
}
Also, manually unrolling the loop seems to work with both compilers:
Key[0] = Key[0] ^ Key[1];
Key[1] = Key[2] ^ Key[3];
It is important that I match the behavior of the old code. Can anyone please help me understand why the original compiler produces those results?
char
tounsigned char
. (which makes sense since that's the 2's complement usigned value of an 8 bit -34) – Heptagonalground-truth
available, other than the hash codes already generated. I should keep generating the hash codes with the same algorithm, but I cannot match the behavior with any modern compiler. – LanthornKey[1] = Key[1] ^ Key[3]
, since that gives a result of 84. – Interknit2*
), as @Interknit suggested, but I really don't understand what's going on here. – Lanthornchar
withsigned char
and thenunsigned char
and see if that's what's making the difference. – Egarton