Understanding behavior of old C++ code

Viewed 428

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;

enter image description here 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?

1 Answers
Related