Beruflich Dokumente
Kultur Dokumente
BALARAM PRATAP
DEVOPS (2017-21)
R171217014
500061550
Aim: Study & implementation of the concept of CRC and Hamming code.
Apparatus: Any programming language.
Theory: A cyclic redundancy check (CRC) is an error-detecting code commonly used in
digital networks and storage devices to detect accidental changes to raw data. Blocks of data
entering these systems get a short check value attached, based on the remainder of
a polynomial division of their contents. On retrieval, the calculation is repeated and, in the
event the check values do not match, corrective action can be taken against data corruption.
CRCs can be used for error correction.
CRCs are so called because the check (data verification) value is a redundancy (it expands
the message without adding information) and the algorithm is based on cyclic codes. CRCs
are popular because they are simple to implement in binary hardware, easy to analyse
mathematically, and particularly good at detecting common errors caused by noise in
transmission channels.
Computation: To compute an n-bit binary CRC, line the bits representing the input in a row, and
position the (n + 1)-bit pattern representing the CRC's divisor (called a "polynomial") underneath the left-
hand end of the row.
11010011101100
This is first padded with zeros corresponding to the bit length n of the CRC. Here is the first
calculation for computing a 3-bit CRC:
The algorithm acts on the bits directly above the divisor in each step. The result for that
iteration is the bitwise XOR of the polynomial divisor with the bits above it. The bits not
above the divisor are simply copied directly below for that step. The divisor is then shifted
one bit to the right, and the process is repeated until the divisor reaches the right-hand end of
the input row. Here is the entire calculation:
Since the leftmost divisor bit zeroed every input bit it touched, when this process ends the
only bits in the input row that can be nonzero are the n bits at the right-hand end of the row.
These n bits are the remainder of the division step, and will also be the value of the CRC
function (unless the chosen CRC specification calls for some postprocessing).
The validity of a received message can easily be verified by performing the above calculation
again, this time with the check value added instead of zeroes. The remainder should equal
zero if there are no detectable errors.
......
00000000001110 100
1011
00000000000101 100
101 1
------------------
00000000000000 000 <--- remainder
1. #include <stdio.h>
2. #include <conio.h>
3. #include <string.h>
4. void main() {
5. int i,j,keylen,msglen;
6. char input[100], key[30],temp[30],quot[100],rem[30],key1[30];
7. clrscr();
8. printf("Enter Data: ");
9. gets(input);
10. printf("Enter Key: ");
11. gets(key);
12. keylen=strlen(key);
13. msglen=strlen(input);
14. strcpy(key1,key);
15. for (i=0;i<keylen-1;i++) {
16. input[msglen+i]='0';
17. }
18. for (i=0;i<keylen;i++)
19. temp[i]=input[i];
20. for (i=0;i<msglen;i++) {
21. quot[i]=temp[0];
22. if(quot[i]=='0')
23. for (j=0;j<keylen;j++)
24. key[j]='0'; else
25. for (j=0;j<keylen;j++)
26. key[j]=key1[j];
27. for (j=keylen-1;j>0;j--) {
28. if(temp[j]==key[j])
29. rem[j-1]='0'; else
30. rem[j-1]='1';
31. }
32. rem[keylen-1]=input[i+keylen];
33. strcpy(temp,rem);
34. }
35. strcpy(rem,temp);
36. printf("\nQuotient is ");
37. for (i=0;i<msglen;i++)
38. printf("%c",quot[i]);
39. printf("\nRemainder is ");
40. for (i=0;i<keylen-1;i++)
41. printf("%c",rem[i]);
42. printf("\nFinal data is: ");
43. for (i=0;i<msglen;i++)
44. printf("%c",input[i]);
45. for (i=0;i<keylen-1;i++)
46. printf("%c",rem[i]);
47. getch();
48. }
Output:
Hamming Code
Hamming code is a set of error-correction codes that can be used to detect and correct the
errors that can occur when the data is moved or stored from the sender to the receiver. It is technique
developed by R.W. Hamming for error correction.
Hamming code uses redundant bits (extra bits) which are calculated according to the below formula: -
2r ≥ m+r+1
Where r is the number of redundant bits required and m is the number of data bits.
R is calculated by putting r = 1, 2, 3 … until the above equation becomes true.
R1 bit is appended at position 20
R2 bit is appended at position 21
R3 bit is appended at position 22 and so on.
These redundant bits are then added to the original data for the calculation of error at receiver’s end.
At receiver’s end with the help of even parity (generally) the erroneous bit position is identified and
since data is in binary we take complement of the erroneous bit position to correct received data.
Respective index parity is calculated for r1, r2, r3, r4 and so on.
Error detection and correction –
Suppose in the above example the 6th bit is changed from 0 to 1 during data transmission,
then it gives new parity values in the binary number:
The bits give the binary number as 0110 whose decimal representation is 6. Thus, the bit
6 contains an error. To correct the error, the 6th bit is changed from 1 to 0.
void main() {
int data[10];
int dataatrec[10],c,c1,c2,c3,i;
c1=dataatrec[6]^dataatrec[4]^dataatrec[2]^dataatrec[0];
c2=dataatrec[5]^dataatrec[4]^dataatrec[1]^dataatrec[0];
c3=dataatrec[3]^dataatrec[2]^dataatrec[1]^dataatrec[0];
c=c3*4+c2*2+c1 ;
if(c==0) {
printf("\nNo error while transmission of data\n");
}
else {
printf("\nError on position %d",c);
for (i=0;i<7;i++) {
printf("%d",dataatrec[i]);
}
}
}
Output: