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!
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.