Enter binary data (e.g., 1011) to generate the Hamming Code with parity bits.
2. Decode & Detect Errors
Enter a received Hamming Code to check for single-bit errors and correct them.
What is Hamming Code?
Hamming code is a set of error-correction codes that can be used to detect and correct single-bit errors that can occur when binary data is transmitted or stored. Developed by Richard Hamming in 1950, it introduces redundant "parity bits" into the data stream at specific positions.
How It Works
The core logic of Hamming Code relies on the positions of bits. Parity bits are placed at positions that are powers of 2 (1, 2, 4, 8, 16…). Each parity bit is responsible for checking the parity of a specific subset of bits in the total code word.
Data Bits (m): The actual information you want to send.
Parity Bits (p): Redundant bits added for error checking.
Constraint: The number of parity bits must satisfy the formula: 2p ≥ m + p + 1.
Example: (7, 4) Hamming Code
If we want to send 4 bits of data (1011), we need 3 parity bits. The total length is 7 bits.
Bit Position
1
2
3
4
5
6
7
Type
P1
P2
D1
P3
D2
D3
D4
By calculating the XOR sum of specific bit positions, we can determine the value of P1, P2, and P3. If the receiver performs the same check and finds a non-zero value (the syndrome), that value points directly to the index of the flipped bit.
function generateHamming() {
var data = document.getElementById('dataInput').value;
var resDiv = document.getElementById('encodeResult');
if (data.length === 0) {
resDiv.className = "result-box error";
resDiv.innerHTML = "Please enter binary data bits (0s and 1s).";
return;
}
var m = data.length;
var p = 0;
while (Math.pow(2, p) < (m + p + 1)) {
p++;
}
var hamming = [];
var j = 0;
var k = 0;
// Position bits, use -1 as placeholder for parity
for (var i = 1; i <= m + p; i++) {
if ((i & (i – 1)) === 0) {
hamming[i] = -1;
} else {
hamming[i] = parseInt(data[j]);
j++;
}
}
// Calculate parity bits
for (var i = 0; i < p; i++) {
var pos = Math.pow(2, i);
var parityValue = 0;
for (var n = 1; n > i) & 1) === 1) {
if (hamming[n] !== -1) {
parityValue ^= hamming[n];
}
}
}
hamming[pos] = parityValue;
}
var resultStr = "";
var displayStr = "";
for (var i = 1; i < hamming.length; i++) {
resultStr += hamming[i];
if ((i & (i – 1)) === 0) {
displayStr += '' + hamming[i] + '';
} else {
displayStr += hamming[i];
}
}
resDiv.className = "result-box success";
resDiv.innerHTML = "Encoded Hamming Code:
" + displayStr + "
" +
"Underlined red bits are parity bits." +
"Total Bits: " + (m + p) + " (Data: " + m + ", Parity: " + p + ")";
}
function decodeHamming() {
var code = document.getElementById('codeInput').value;
var resDiv = document.getElementById('decodeResult');
if (code.length === 0) {
resDiv.className = "result-box error";
resDiv.innerHTML = "Please enter a Hamming code string.";
return;
}
var n = code.length;
var p = Math.floor(Math.log2(n)) + 1;
var errorPos = 0;
// Calculate syndrome
for (var i = 0; i < p; i++) {
var paritySum = 0;
var bitWeight = Math.pow(2, i);
for (var j = 1; j > i) & 1) === 1) {
paritySum ^= parseInt(code[j – 1]);
}
}
if (paritySum !== 0) {
errorPos += bitWeight;
}
}
if (errorPos === 0) {
resDiv.className = "result-box success";
resDiv.innerHTML = "No errors detected!The code is valid.";
} else if (errorPos > n) {
resDiv.className = "result-box error";
resDiv.innerHTML = "Multiple errors detected!Hamming code can only reliably correct a single-bit error. The calculated error position (" + errorPos + ") is out of bounds.";
} else {
var correctedCode = code.split(");
var bitAtError = correctedCode[errorPos – 1];
correctedCode[errorPos – 1] = (bitAtError === '0') ? '1' : '0';
var dataBits = "";
for (var k = 1; k <= n; k++) {
if ((k & (k – 1)) !== 0) {
dataBits += correctedCode[k – 1];
}
}
resDiv.className = "result-box error";
resDiv.innerHTML = "Error detected!" +
"Error found at bit position: " + errorPos + "" +
"Corrected Code: " + correctedCode.join(") + "" +
"Extracted Data: " + dataBits + "";
}
}