0
votes

I am writing a code to calculate a CRC16 using 32 bit unsigned integers. When trying to print the return value out from the XOR function that performs the CRC operation it always prints 0. I've tried a variety of debug methods such as print statements, however, I can't seem to figure it out!

Here's my XOR function:

 uint32_t XOR(uint32_t divisor, uint32_t dividend)

{
  uint32_t divRemainder = dividend;
  uint32_t currentBit;

  for(currentBit = 32; currentBit > 0; --currentBit)
  {
    if(dividend && 0x32)
    {
      divRemainder = divRemainder ^ divisor;
    }
    divRemainder = divRemainder << 1;
  }
  return (divRemainder >> 8);
}

The function that calls the above method:

  void crcCalculation(char *text, FILE *input, char *POLYNOMIAL)
    {
      int i = strlen(text);
      uint32_t dividend = atoi(POLYNOMIAL);
      uint32_t  result;


      readInput(text, input);
      printText(text);


      printf("CRC 16 calculation progress:\n");


      if(i < 504)
      {
        for(; i!=504; i++)
        {
          text[i] = '.';
        }
      }

  result = XOR((uintptr_t)POLYNOMIAL, dividend);

      printf(" - %d", result);

}

The constant polynomial (I hope I calculated this correctly for CRC 16:

#define POLYNOMIAL A053

I'd appreciate a nudge in the correct direction!

1
Do you know the difference between logical AND and bitwise AND? - user694733
@user694733 I do not, I thought they were the same thing. I'll read up on that. - starlight
You don't make a checksum of whatever data that POLYNOMIAL is pointing to, but of the pointer itself. That doesn't seem very useful. You also don't calculate any checksum for the text string (which might not be zero-terminated). And do you really define that macro? In combination with the code you show it doesn't make much sense either. - Some programmer dude
@MartinR The macro replacement happens to be a valid symbol for a variable, and in the function shown it's a pointer so the cast works fine (unfortunately). The macro and its use doesn't make any sense though. - Some programmer dude
"debug methods such as print statements" That is the slow and cumbersome way to do it. Better to learn how the step through in debugger, and watch variables that way. - user694733

1 Answers

3
votes

The code if(dividend && 0x32) makes no sense at all and will evaluate to 1. This is the reason why nothing works.

Perhaps you meant if(dividend & 32) or similar? As in bitwise AND instead of logical AND. And hex 0x20 decimal 32 (which could make sense... probably not?) instead of hex 0x32 decimal 50 (which doesn't make any sense at all).

Overall this CRC algorithm looks very fishy. You only iterate over 31 bits, for example.