Sie sind auf Seite 1von 9


DEVOPS (2017-21)

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.

Start with the message to be encoded:


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:

11010011101100 000 <--- input right padded by 3 bits

1011 <--- divisor (4 bits) = x³ + x + 1
01100011101100 000 <--- result

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:

11010011101100 000 <--- input right padded by 3 bits

1011 <--- divisor
01100011101100 000 <--- result (note the first four bits are the XOR with the divisor
beneath, the rest of the bits are unchanged)
1011 <--- divisor ...
00111011101100 000
00010111101100 000
00000001101100 000 <--- note that the divisor moves over to align with the next 1 in the
dividend (since quotient for that step was zero)
1011 (in other words, it doesn't necessarily move one bit per iteration)
00000000110100 000
00000000011000 000
00000000001110 000
00000000000101 000
101 1
00000000000000 100 <--- remainder (3 bits). Division algorithm stops here as dividend is
equal to zero.

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.

11010011101100 100 <--- input with check value

1011 <--- divisor
01100011101100 100 <--- result
1011 <--- divisor ...
00111011101100 100


00000000001110 100
00000000000101 100
101 1
00000000000000 000 <--- remainder

C Program to implement CRC (Cyclic Redundancy Code)

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

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.

General Algorithm of Hamming code –

The Hamming Code is simply the use of extra parity bits to allow the identification of an
1. Write the bit positions starting from 1 in binary form (1, 10, 11, 100, etc.).
2. All the bit positions that are a power of 2 are marked as parity bits (1, 2, 4, 8, etc.).
3. All the other bit positions are marked as data bits.
4. Each data bit is included in a unique set of parity bits, as determined its bit position in
binary form.
a. Parity bit 1 covers all the bits positions whose binary representation includes a 1 in
the least significant
position (1, 3, 5, 7, 9, 11, etc.).
b. Parity bit 2 covers all the bits positions whose binary representation includes a 1 in
the second position from
the least significant bit (2, 3, 6, 7, 10, 11, etc.).
c. Parity bit 4 covers all the bits positions whose binary representation includes a 1 in
the third position from
the least significant bit (4–7, 12–15, 20–23, etc.).
d. Parity bit 8 covers all the bits positions whose binary representation includes a 1 in
the fourth position from
the least significant bit bits (8–15, 24–31, 40–47, etc.).
e. In general each parity bit covers all bits where the bitwise AND of the parity position
and the bit position is
5. Since we check for even parity set a parity bit to 1 if the total number of ones in the
positions it checks is
6. Set a parity bit to 0 if the total number of ones in the positions it checks is even.

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.

C Program to implement Hamming code:


void main() {
int data[10];
int dataatrec[10],c,c1,c2,c3,i;

printf("Enter 4 bits of data one by one\n");


//Calculation of even parity


printf("\nEncoded data is\n");


printf("\n\nEnter received data bits one by one\n");


c=c3*4+c2*2+c1 ;

if(c==0) {
printf("\nNo error while transmission of data\n");
else {
printf("\nError on position %d",c);

printf("\nData sent : ");


printf("\nData received : ");


printf("\nCorrect message is\n");

//if errorneous bit is 0 we complement it else vice versa


for (i=0;i<7;i++) {