121ax007 CN Exp 08
121ax007 CN Exp 08
PRN: 121AX007
SUBJECT: COMPUTER NETWORKS.
EXPERIMENT NO: 08
AIM: Implement hamming code and CRC techniques for error detection and error
correction.
THEORY:
1. CRC (Cyclic Redundancy Check) is a technique used for detecting errors in digital data
transmission. It is a widely used error-detection method that is capable of detecting most
of the common errors that can occur during data transmission.
2. The CRC technique involves adding extra bits to the original data in a way that makes it
possible to check for errors. These extra bits are calculated using a mathematical formula
based on the original data. The formula generates a fixed-length check value (CRC value)
that is appended to the original data. When the data is received, the CRC value is
recalculated and compared to the original CRC value. If the two values match, the data is
considered to be error-free. If the values do not match, an error is detected, and the data
must be retransmitted.
3. There are several variations of CRC techniques, with different polynomial functions used
to generate the CRC value. The most commonly used polynomial is the CRC-32, which
uses a 32-bit polynomial function to generate the CRC value.
4. CRC is a simple and efficient error-detection method that is widely used in computer
networks, communication systems, and storage devices. Its popularity stems from its
simplicity, low overhead, and high reliability.
1. Polynomial degree: The degree of the polynomial determines the number of bits used to
represent the CRC. For example, a polynomial of degree 16 generates a 16-bit CRC.
2. Generator polynomial: The generator polynomial is a fixed polynomial used to generate the
CRC. It is represented as a binary number and is chosen based on the specific application.
3. CRC width: The width of the CRC is the number of bits that make up the CRC value. It is
determined by the polynomial degree and can be any even number.
4. Bit order: The bits in the input data and the generated CRC can be transmitted in either little-
endian or big-endian bit order.
5. Initialization value: The initialization value is the starting value used to calculate the CRC.
It is usually set to all ones or all zeros, depending on the application.
6. Final XOR value: The final XOR value is a fixed value that is XORed with the CRC before
transmission. It is used to prevent systematic errors.
7. Linearity: The CRC algorithm is linear, which means that the sum of two CRCs is equal to
the CRC of the sum of the data.
8. Error detection capability: The CRC algorithm is capable of detecting any error that
involves an odd number of bit flips, including all single-bit errors, most multi-bit errors,
and some burst errors.
9. Error correction capability: The CRC algorithm does not provide error correction, only error
detection. If an error is detected, the data must be retransmitted.
NAME: OM SANJAY BHONGALE
PRN: 121AX007
SUBJECT: COMPUTER NETWORKS.
Steps Involved:
Hamming Code:
Hamming code is an error-correcting code used to detect and correct errors in digital data
transmission. It was developed by Richard W. Hamming in the 1940s and is widely used in
computer memory systems, communication systems, and other digital systems.
The Hamming code works by adding redundant bits to the data being transmitted. The
redundant bits are calculated using a mathematical formula based on the original data. The
formula generates a fixed number of check bits that are added to the original data to form the
Hamming code.
The Hamming code is designed to detect and correct single-bit errors in the transmitted data.
The code is based on the concept of parity, which is a simple error-detection technique that
involves adding a single extra bit to the data to indicate whether the number of 1 bit in the data
is odd or even.
The Hamming code extends the concept of parity to detect and correct errors in multiple bits.
The code adds multiple redundant bits to the data to form a codeword that can detect and correct
errors in up to a specified number of bits.
Steps Involved:
1. Determine the number of redundant bits needed: The number of redundant bits needed
depends on the number of data bits being transmitted. The number of redundant bits is
calculated using the formula 2^r ≥ m + r + 1, where r is the number of redundant bits and
m is the number of data bits.
2. Position the redundant bits: The redundant bits are positioned at the positions that are
powers of 2. For example, if there are 7 data bits and 4 redundant bits, the redundant bits
are positioned at bits 1, 2, 4, and 8.
NAME: OM SANJAY BHONGALE
PRN: 121AX007
SUBJECT: COMPUTER NETWORKS.
3. Calculate the redundant bits: The redundant bits are calculated using a mathematical
formula based on the data bits. The formula involves performing exclusive-OR (XOR)
operations on the data bits that correspond to each redundant bit position.
4. Append the redundant bits: The redundant bits are appended to the data bits to form the
Hamming code.
5. Transmit the Hamming code: The entire Hamming code, including the data bits and the
redundant bits, is transmitted to the receiver.
Verify and correct errors at the receiver: The receiver receives the Hamming code and performs
a similar calculation of the redundant bits using the same formula. If an error is detected, the
receiver can determine the position of the error and correct it by flipping the bit at the position.
Hamming Code:
Code:
1. import java.util.*;
2.
3. public class Hamming {
4. public static void main(String[] args) {
5. Scanner input = new Scanner(System.in);
6. System.out.print("Enter the data bits:");
7. String data = input.nextLine();
8.
9. // Calculate number of parity bits required
10. int r = 0;
11. while (Math.pow(2, r) < data.length() + r + 1) {
12. r++;
13. }
14.
15. // Create Hamming code array with data and parity bits
16. int[] hamming = new int[data.length() + r];
17. int j = 0;
18. for (int i = 0; i < hamming.length; i++) {
19. if (i == Math.pow(2, j) - 1) {
20. // Parity bit position
21. } else {
22. j++;
23. hamming[i] =
Integer.parseInt(Character.toString(data.charAt(data.length() - 1 - (i -
j))));
24. }
25. }
26.
27. // Calculate parity bits and update Hamming code array
28. for (int i = 0; i < r; i++) {
29. int sum = 0;
NAME: OM SANJAY BHONGALE
PRN: 121AX007
SUBJECT: COMPUTER NETWORKS.
78. }
Output:
CRC:
Code:
1. import java.util.*;
2.
3. public class CRC {
4. public static void main(String[] args) {
5. Scanner scanner = new Scanner(System.in);
6. System.out.print("Enter the data bits:");
7. String data = scanner.nextLine();
8. System.out.print("Enter the generator bits:");
9. String gen = scanner.nextLine();
10. System.out.println("The data bits are:" + data);
11. System.out.println("The generator bits are:" + gen);
12.
13. data += "0".repeat(Math.max(0, gen.length() - 1));
14. char[] dataList = data.toCharArray();
15. char[] genList = gen.toCharArray();
16.
17. for (int i = 0; i <= dataList.length - genList.length; i++) {
18. if (dataList[i] == '1') {
19. for (int j = 0; j < genList.length; j++) {
20. dataList[i + j] = String.valueOf((int) dataList[i + j] ^
(int) genList[j]).charAt(0);
21. }
22. }
23. }
24.
25. String rem = new String(dataList).substring(dataList.length -
genList.length + 1);
26. System.out.println("The remainder bits are:" + rem);
NAME: OM SANJAY BHONGALE
PRN: 121AX007
SUBJECT: COMPUTER NETWORKS.
27.
28. String transmittedData = data.substring(0, data.length() -
genList.length + 1) + rem;
29. System.out.println("The transmitted data bits are:" +
transmittedData);
30.
31. System.out.print("Enter the received data bits:");
32. String receivedData = scanner.nextLine();
33. char[] receivedList = receivedData.toCharArray();
34.
35. for (int i = 0; i <= receivedList.length - genList.length; i++) {
36. if (receivedList[i] == '1') {
37. for (int j = 0; j < genList.length; j++) {
38. receivedList[i + j] = String.valueOf((int) receivedList[i
+ j] ^ (int) genList[j]).charAt(0);
39. }
40. }
41. }
42.
43. String remReceived = new
String(receivedList).substring(receivedList.length - genList.length + 1);
44. System.out.println("The remainder bits are:" + remReceived);
45.
46. if (receivedData.contains("1")) {
47. System.out.println("The received data bits are incorrect");
48. } else {
49. System.out.println("The received data bits are correct");
50. }
51. }
52. }
Output:
CONCLUSION: Successfully implemented hamming code and CRC techniques for error
detection and error correction with JAVA 20 under Windows 11.