CN LAB REPORT
SUBMITTED BY : J.S.ABHINAV
REG NO : 23BCE7615
SLOT : L31+L32
SUBMITTED TO : Prof. MOHINDER SINGH.B
Aim
To implement a Java program that generates the Hamming code for a 4-bit
dataword, calculates the parity bits, and displays the final 7-bit codeword
for error detection and correction.
Components
Java Compiler (javac): Compiles the Java source code into bytecode.
Programiz (or any Java IDE/online compiler): Platform to write, compile,
and execute Java code online.
Theory
Hamming Code is an error-detecting and error-correcting code that uses
parity bits placed at positions that are powers of two (1, 2, 4, ...) in the
codeword. In a 7-bit Hamming code, 3 parity bits are placed at the 1st,
2nd, and 4th positions, and 4 data bits fill the rest. Parity bits are computed
by XOR operations on specific positions of bits, ensuring the detection and
correction of single-bit errors during data transmission. This helps enhance
data integrity.
Procedure
1)Open a web browser.
2)Go to Programiz Java Compiler at https://www.programiz.com/java-
programming/online-compiler
3)Compile the code ,and check for errors
4)Click the "Run" button to compile and execute the program.
5)When prompted in the console, enter a 4-bit binary dataword (e.g.,
1010).
6)The program calculates the number of parity bits required (3 for 4-bit
data).
7)The program then arranges data bits and calculates parity bits using
XOR.
8)Finally, the program prints
:
9)Positions of parity bits and data bits.
10)Calculated parity bits values.
11)The final 7-bit Hamming codeword.
Observe the output and verify correctness.
#CODE
import java.util.Scanner;
public class HammingCodeDetailed {
public static int calculateParityBits(int m) {
int r = 0;
while (Math.pow(2, r) < m + r + 1) {
r++;
}
return r;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter dataword 4 bits:");
String input = sc.nextLine();
if (input.length() != 4 || !input.matches("[01]+")) {
System.out.println("Invalid input!");
return;
}
int parityBits = calculateParityBits(4);
System.out.println("Number of parity bits needed: " + parityBits);
int[] data = new int[4];
for (int i = 0; i < 4; i++) {
data[i] = input.charAt(i) - '0';
}
int[] codeword = new int[7];
codeword[2] = data[0];
codeword[4] = data[1];
codeword[5] = data[2];
codeword[6] = data[3];
int p1 = codeword[0] ^ codeword[2] ^ codeword[4] ^ codeword[6];
int p2 = codeword[1] ^ codeword[2] ^ codeword[5] ^ codeword[6];
int p3 = codeword[3] ^ codeword[4] ^ codeword[5] ^ codeword[6];
codeword[0] = p1;
codeword[1] = p2;
codeword[3] = p3;
System.out.println("Positions in 7-bit codeword:");
System.out.println("Parity bits: positions 1, 2, 4");
System.out.println("Data bits: positions 3, 5, 6, 7");
System.out.println("Calculated parity bits:");
System.out.println("p1 (position 1) = " + p1);
System.out.println("p2 (position 2) = " + p2);
System.out.println("p3 (position 4) = " + p3);
System.out.print("Final codeword: ");
for (int bit : codeword) {
System.out.print(bit);
}
}
}
INPUT
OUTPUT
Result
The Java program successfully generates the Hamming code for a 4-bit
input dataword, calculates correct parity bits, and displays the 7-bit
codeword. This codeword can be used for error detection and correction in
communication systems.
This structured format helps understand the aim, components involved,
theoretical background, step-by-step procedure to execute the program on
Programiz, and interpret the output and result of the program.