dgoldman
Joined: 24 Aug 2010 Posts: 7
|
XOR and data types question |
Posted: Wed Sep 01, 2010 9:58 am |
|
|
I am using PCWH v3.218 for a project on an 18f452 and all is good.
Same code breaks on v4.112. I have found the issue to be related to xor and data types.
Test #1 is standard xor truth table.
Test #2 was what my code was doing (ok in 3.218 and not in 4.112).
Why are there differences in Tests 3 and 4?
Why doesn't test 2 work in the new compiler?
Anyone got clues? It would be appreciated.
Am I am missing something simple here?
Code: |
int1 check()
{
unsigned int1 c;
unsigned int1 q;
unsigned int32 k; // this data type is needed for other purposes and cannot easily be changed.
signed int32 j; // this data type is needed for other purposes and cannot easily be changed.
fprintf(DEBUG_STREAM, "XOR test 1 - works\r");
c = 0; c ^= 0; fprintf(DEBUG_STREAM, "0 ^ 0 = 0X%X\r", c);
c = 1; c ^= 0; fprintf(DEBUG_STREAM, "1 ^ 0 = 0X%X\r", c);
c = 0; c ^= 1; fprintf(DEBUG_STREAM, "0 ^ 1 = 0X%X\r", c);
c = 1; c ^= 1; fprintf(DEBUG_STREAM, "1 ^ 1 = 0X%X\r", c);
fprintf(DEBUG_STREAM, “\r");
fprintf(DEBUG_STREAM, "XOR test 2 - doesn't work\r");
k = 0; c = 0; c ^= ((int1)k); fprintf(DEBUG_STREAM, "0 ^ 0 = 0X%X\r", c);
k = 0; c = 1; c ^= ((int1)k); fprintf(DEBUG_STREAM, "1 ^ 0 = 0X%X\r", c);
k = 1; c = 0; c ^= ((int1)k); fprintf(DEBUG_STREAM, "0 ^ 1 = 0X%X\r", c);
k = 1; c = 1; c ^= ((int1)k); fprintf(DEBUG_STREAM, "1 ^ 1 = 0X%X\r", c);
fprintf(DEBUG_STREAM, “\r");
fprintf(DEBUG_STREAM, "XOR test 3 - doesn't work\r");
k = 0; q=k; c = 0; c ^= q; fprintf(DEBUG_STREAM, "0 ^ 0 = 0X%X\r", c);
k = 0; q=k; c = 1; c ^= q; fprintf(DEBUG_STREAM, "1 ^ 0 = 0X%X\r", c);
k = 1; q=k; c = 0; c ^= q; fprintf(DEBUG_STREAM, "0 ^ 1 = 0X%X\r", c);
k = 1; q=k; c = 1; c ^= q; fprintf(DEBUG_STREAM, "1 ^ 1 = 0X%X\r", c);
fprintf(DEBUG_STREAM, “\r");
fprintf(DEBUG_STREAM, "XOR test 4 - works\r");
k = 0; q=k; c = 0; c = c ^ q; fprintf(DEBUG_STREAM, "0 ^ 0 = 0X%X\r", c);
k = 0; q=k; c = 1; c = c ^ q; fprintf(DEBUG_STREAM, "1 ^ 0 = 0X%X\r", c);
k = 1; q=k; c = 0; c = c ^ q; fprintf(DEBUG_STREAM, "0 ^ 1 = 0X%X\r", c);
k = 1; q=k; c = 1; c = c ^ q; fprintf(DEBUG_STREAM, "1 ^ 1 = 0X%X\r", c);
fprintf(DEBUG_STREAM, “\r");
fprintf(DEBUG_STREAM, "XOR test 5 - doesn't work\r");
k = 0; c = 0; c = c ^ k; fprintf(DEBUG_STREAM, "0 ^ 0 = 0X%X\r", c);
k = 0; c = 1; c = c ^ k; fprintf(DEBUG_STREAM, "1 ^ 0 = 0X%X\r", c);
k = 1; c = 0; c = c ^ k; fprintf(DEBUG_STREAM, "0 ^ 1 = 0X%X\r", c);
k = 1; c = 1; c = c ^ k; fprintf(DEBUG_STREAM, "1 ^ 1 = 0X%X\r", c);
fprintf(DEBUG_STREAM, “\r");
fprintf(DEBUG_STREAM, "XOR test 6 - works\r");
k = 0; c = 0; c = (c != k); fprintf(DEBUG_STREAM, "0 ^ 0 = 0X%X\r", c);
k = 0; c = 1; c = (c != k); fprintf(DEBUG_STREAM, "1 ^ 0 = 0X%X\r", c);
k = 1; c = 0; c = (c != k); fprintf(DEBUG_STREAM, "0 ^ 1 = 0X%X\r", c);
k = 1; c = 1; c = (c != k); fprintf(DEBUG_STREAM, "1 ^ 1 = 0X%X\r", c);
fprintf(DEBUG_STREAM, “\r");
return 0;
}
|
This results in:
XOR test 1 - works
0 ^ 0 = 0X00
1 ^ 0 = 0X01
0 ^ 1 = 0X01
1 ^ 1 = 0X00
XOR test 2 - doesn't work
0 ^ 0 = 0X00
1 ^ 0 = 0X00
0 ^ 1 = 0X01
1 ^ 1 = 0X01
XOR test 3 - doesn't work
0 ^ 0 = 0X00
1 ^ 0 = 0X00
0 ^ 1 = 0X01
1 ^ 1 = 0X01
XOR test 4 - works
0 ^ 0 = 0X00
1 ^ 0 = 0X01
0 ^ 1 = 0X01
1 ^ 1 = 0X00
XOR test 5 - doesn't work
0 ^ 0 = 0X00
1 ^ 0 = 0X00
0 ^ 1 = 0X00
1 ^ 1 = 0X00
XOR test 6 - works
0 ^ 0 = 0X00
1 ^ 0 = 0X01
0 ^ 1 = 0X01
1 ^ 1 = 0X00 |
|