- Notifications
You must be signed in to change notification settings - Fork 363
Expand file tree
/
Copy pathhammingCode.java
More file actions
Latest commit
87 lines (73 loc) · 3.21 KB
/
Copy pathhammingCode.java
File metadata and controls
87 lines (73 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
importjava.util.ArrayList;
importjava.util.List;
publicclasshammingCode {
publicstaticintcalculateParityBits(intm, intlow, inthigh) {
// Binary algorithm to find the required number of bits
if (high == 0) {
high = m + 1;
}
intr = (low + high) / 2;
if (low == high) {
returnlow;
} elseif (Math.pow(2, r) >= r + m + 1) { // r = redundant bit, m = data bit
returncalculateParityBits(m, low, r);
} else {
returncalculateParityBits(m, r + 1, high);
}
}
publicstaticbooleanisPowerOfTwo(intnum) {
// Performs a bitwise AND operation between num and num - 1.
// This operation turns off the rightmost set bit in num (i.e., the least significant bit).
// If the result of the bitwise AND operation is zero, it means that num had only one set bit (power of 2)
// before the bitwise operation, and that bit was turned off. In this case, the number is considered a power of 2.
return (num & (num - 1)) == 0;
}
publicstaticintcalculateParityValue(List<Character> encodedBlock, intpos) {
// Count the number of ones corresponding to the parity bit
intcount = 0;
for (inti = 0; i < encodedBlock.size(); i++) {
intbit = encodedBlock.get(i);
if (bit == '1' && (((i + 1) & pos) != 0)) {
count++;
}
}
returncount % 2;
}
publicstaticStringhammingEncode(Stringmessage) {
intm = message.length(); // Length of the message
intr = calculateParityBits(m, 0, 0); // Calculate the number of parity bits
intblockLength = m + r; // Total length of the encoded block
// Create a list to store the encoded bits
List<Character> encodedBlock = newArrayList<>();
intdataBit = 0;
// Iterate over the block and copy the data
for (intposition = 1; position <= blockLength; position++) {
if (isPowerOfTwo(position)) {
encodedBlock.add('P'); // Mark as parity bit
} else {
encodedBlock.add(message.charAt(dataBit)); // Data bit, copy from the message
dataBit++;
}
}
// Iterate over the parity bits in the encoded block
for (intposition = 1; position <= blockLength; position++) {
if (encodedBlock.get(position - 1) == 'P') {
// Calculate and assign the required parity value
intparity = calculateParityValue(encodedBlock, position);
encodedBlock.set(position - 1, (parity % 2 == 0) ? '0' : '1');
}
}
// Convert the list of characters to a string
StringBuilderencodedMessage = newStringBuilder();
for (charbit : encodedBlock) {
encodedMessage.append(bit);
}
returnencodedMessage.toString();
}
publicstaticvoidmain(String[] args) {
Stringmessage = "11110010010"; // Example message
StringencodedMessage = hammingEncode(message);
System.out.println("Message entered: " + message);
System.out.println("Encoded Message: " + encodedMessage);
}
}