0% found this document useful (0 votes)
7 views2 pages

CN - Practical 4

Cn

Uploaded by

King Gajera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

CN - Practical 4

Cn

Uploaded by

King Gajera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1

Practical 4

1) Write a C Program for the CRC Generator.


#include <stdio.h>
#include <stdlib.h>

#define DATA_SIZE 100


#define CRC_SIZE 16

unsigned int crc_generator(unsigned int data[], int data_size, unsigned int


generator) {
unsigned int crc = 0;

for (int i = 0; i < data_size; i++) {


crc ^= data[i];
for (int j = 0; j < CRC_SIZE; j++) {
if (crc & (1 << (CRC_SIZE - 1))) {
crc = (crc << 1) ^ generator;
} else {
crc <<= 1;
}
}
}

return crc;
}

int main() {
unsigned int data[DATA_SIZE];
int data_size;
unsigned int generator;

printf("Enter the data size: ");


scanf("%d", &data_size);

printf("Enter the data: ");


for (int i = 0; i < data_size; i++) {
scanf("%x", &data[i]);
}

printf("Enter the generator polynomial: ");


2

scanf("%x", &generator);

unsigned int crc = crc_generator(data, data_size, generator);

printf("CRC: %x\n", crc);

return 0;
}

Possible Outputs:

● If the user enters the following values:


○ Data size: 5
○ Data: 0x12 0x34 0x56 0x78 0x9A
○ Generator polynomial: 0x0007
● The output might be:
CRC: 0x0006

You might also like