0% found this document useful (0 votes)
40 views6 pages

121ax007 CN Exp 08

1. The document discusses implementing error detection techniques like CRC and Hamming codes in Java. It provides detailed explanations of how CRC and Hamming codes work. 2. The Java code provided calculates a Hamming code from input data bits by determining the number of parity bits needed, positioning the parity bits, calculating the parity bits, and appending them to generate the codeword. 3. The code flips a bit in the Hamming code to simulate an error, displays the code with error, and includes code to correct errors in the Hamming code by determining the error position.

Uploaded by

rrr
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)
40 views6 pages

121ax007 CN Exp 08

1. The document discusses implementing error detection techniques like CRC and Hamming codes in Java. It provides detailed explanations of how CRC and Hamming codes work. 2. The Java code provided calculates a Hamming code from input data bits by determining the number of parity bits needed, positioning the parity bits, calculating the parity bits, and appending them to generate the codeword. 3. The code flips a bit in the Hamming code to simulate an error, displays the code with error, and includes code to correct errors in the Hamming code by determining the error position.

Uploaded by

rrr
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/ 6

NAME: OM SANJAY BHONGALE

PRN: 121AX007
SUBJECT: COMPUTER NETWORKS.

EXPERIMENT NO: 08

AIM: Implement hamming code and CRC techniques for error detection and error
correction.

TOOLS: JAVA 20 under WINDOWS 11.

THEORY:

Cyclic Redundancy Check (CRC) Technique:

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.

Properties of CRC Generator:

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:

Error detection using CRC technique involves the following steps:

1. Choose a generator polynomial: A generator polynomial is a fixed polynomial that is used


to generate the CRC. The degree of the polynomial determines the number of bits used to
represent the CRC.
2. Initialize the CRC: The CRC register is initialized to a fixed value, typically all zeros or all
ones, depending on the application.
3. Append the message: The message is appended to the CRC register.
4. Divide the message by the generator polynomial: The CRC generator performs a
polynomial division of the message and the generator polynomial to obtain the remainder.
The remainder is the CRC value.
5. Append the CRC to the message: The CRC value is appended to the end of the message.
6. Transmit the message: The entire message, including the appended CRC, is transmitted to
the receiver.
7. Verify the CRC at the receiver: The receiver receives the message and verifies the CRC.
The receiver performs the same polynomial division on the received message using the same
generator polynomial. If the remainder obtained matches the CRC value appended to the
message, the message is considered to be error-free.

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.

CODE & EXECUTION:

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.

30. for (int k = 0; k < hamming.length; k++) {


31. if (((k + 1) & (1 << i)) != 0) {
32. sum ^= hamming[k];
33. }
34. }
35. hamming[(int) Math.pow(2, i) - 1] = sum;
36. }
37.
38. // Display Hamming code
39. System.out.print("Hamming code: ");
40. for (int i = hamming.length - 1; i >= 0; i--) {
41. System.out.print(hamming[i]);
42. }
43. System.out.println();
44.
45. System.out.print("Enter position of bit to flip (0-indexed): ");
46. int position = input.nextInt();
47. hamming[position] ^= 1;
48. System.out.print("Hamming code with error: ");
49. for (int i = hamming.length - 1; i >= 0; i--) {
50. System.out.print(hamming[i]);
51. }
52. System.out.println();
53.
54. // Correct error in Hamming code
55. int errorPosition = 0;
56. for (int i = 0; i < r; i++) {
57. int sum = 0;
58. for (int k = 0; k < hamming.length; k++) {
59. if (((k + 1) & (1 << i)) != 0) {
60. sum ^= hamming[k];
61. }
62. }
63. errorPosition += sum * Math.pow(2, i);
64. }
65.
66. if (errorPosition != 0) {
67. System.out.println("Error detected at position " + errorPosition);
68. hamming[errorPosition - 1] ^= 1;
69. System.out.print("Corrected Hamming code: ");
70. for (int i = hamming.length - 1; i >= 0; i--) {
71. System.out.print(hamming[i]);
72. }
73. System.out.println();
74. } else {
75. System.out.println("No error detected");
76. }
77. }
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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy