Jump to content
chuckj

Incorrect Xor Result?

Recommended Posts

Bug description:

Getting incorrect(I believe!) result from complex XOR

Result should be 0x87FE, am getting 0x7AFE.

Same result using -O0 option on compiler.

 

Steps to reproduce:

Run this code:

#include <system.h>

 

int

main( void )

{

unsigned short iNum;

unsigned short pNum;

unsigned short usCRC;

 

usCRC = 0xFFFF;

iNum = 0x00F4;

pNum = 0xC001;

usCRC = (((usCRC>>8) ^ pNum) ^ (iNum<<6)) ^ (iNum<<7);

}

 

Got same result with parens only around the '<<' terms.

 

Expected behaviour:

I expect the ending usCRC value to be 0x87FE.

Here is a breakdown of the last line XOR:

 

0000 0000 1111 1111 //This is usCRC(0xFFFF) >> 8

1100 0000 0000 0001 //0xC001

0111 1010 0000 0000 // iNum << 7

0011 1101 0000 0000 // iNum << 6

 

1000 0111 1111 1110 //Result of 0x87FE from XOR of 4 16 bit values above

 

 

Is the problem 100% reproduceable:

Yes

 

IDE version: 6.95

Compiler: BoostC

Compiler version: 6.95

Target device: PIC18F2420

OS: Window XP SP2

 

Comments:

Please let me know if I am doing something wrong here.

Share this post


Link to post
Share on other sites

I get the correct result as long as the (usCRC>>8) term is not the first term in the last line.

These last lines all give the correct result:

 

  usCRC =	(iNum<<1) ^ (usCRC>>8) ^ pNum ^ iNum;
 usCRC =	(iNum<<1) ) ^ pNum^ (usCRC>>8 ^ iNum;
 usCRC =	(iNum<<1)  ^ pNum ^ iNum^ (usCRC>>8);

 

Sorry for not using code tags in my bug report!

Share this post


Link to post
Share on other sites

Slip in a little cast seems to help.

 

usCRC = (((unsigned short)(usCRC>>8) ^ pNum) ^ (iNum<<6)) ^ (iNum<<7);

 

Cheers

 

Reynard

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...