Question
use the provided CRC generator program as a starting point to develop a CRC checking function with the following declaration: unsigned int check_crc(unsigned int g,
use the provided CRC generator program as a starting point to develop a CRC checking function with the following declaration:
unsigned int check_crc(unsigned int g, unsigned int r) { // g: generator value, r: received word, possibly containing errors to be detected
// The function returns an unsigned int, zero if no error detected, and a value greater than zero otherwise
// Add function code here
}
Submit the C code for this function. The code must compile and run without errors.
.
.
.
..
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
/* CRC generator -------------
Uses long-hand division to generate codewords at most 32 bits long using data in hexadecimal format read from standard input. */
#include
/* Finds position of most significant 'one' of x (starting count from 1) */ unsigned int find_msop(unsigned int x) { unsigned int position = 0; while (x) { position++; x >>= 1; } return position; }
/* Returns codeword given generator ploynomial (g) and data bits (i) */ unsigned int add_crc(unsigned int g, unsigned int i) { /* registers: must be at least 32 bits long */ unsigned int d, mask; /* regular counters and auxiliary variables */ unsigned int ncrc, j;
ncrc = find_msop(g) - 1; /* Must count from 0 */ d = i << ncrc;
/* Perform long-hand division (but quotient not calculated) */ j = find_msop(d); /* j: index of MSB in d */ while (j > ncrc) { j--; mask = 1 << j; if (d & mask) /* Here we would add a '1' to quotient, else would be a '0' */ d = d ^ (g << (j-ncrc)); }
return (i << ncrc) + d; }
int main() { unsigned int i, g, b; /* Generator polynomial */ g = 0x107; // CRC-8-CCITT
/* read information bits and encode until end of file: data may not have more than 3 bytes or else code fails. Max data 0xffffff */ while (scanf("%x", &i) != EOF) { printf("info = 0x%x", i); b = add_crc(g, i); printf("\t codeword = 0x%x ", b); } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started