CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

XOR and data types question

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
dgoldman



Joined: 24 Aug 2010
Posts: 7

View user's profile Send private message

XOR and data types question
PostPosted: Wed Sep 01, 2010 9:58 am     Reply with quote

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
dgoldman



Joined: 24 Aug 2010
Posts: 7

View user's profile Send private message

PostPosted: Wed Sep 01, 2010 1:56 pm     Reply with quote

By changing c from an unsigned int1 to an unsigned int32, all now works.
No casting of k, just c ^= k.

Still doesn't make a whole lotta sense but things are working.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group