Hamming Code Calculator

.hamming-container { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif; max-width: 800px; margin: 20px auto; padding: 25px; border: 1px solid #e1e1e1; border-radius: 12px; background-color: #ffffff; box-shadow: 0 4px 6px rgba(0,0,0,0.05); color: #333; } .hamming-container h2, .hamming-container h3 { color: #1a73e8; margin-top: 0; } .calc-section { background: #f8f9fa; padding: 20px; border-radius: 8px; margin-bottom: 25px; border-left: 5px solid #1a73e8; } .input-group { margin-bottom: 15px; } .input-group label { display: block; font-weight: 600; margin-bottom: 8px; font-size: 14px; } .input-group input { width: 100%; padding: 12px; border: 2px solid #ddd; border-radius: 6px; font-family: monospace; font-size: 16px; box-sizing: border-box; } .input-group input:focus { border-color: #1a73e8; outline: none; } .calc-btn { background-color: #1a73e8; color: white; border: none; padding: 12px 24px; border-radius: 6px; cursor: pointer; font-weight: 600; font-size: 16px; transition: background 0.3s; } .calc-btn:hover { background-color: #1557b0; } .result-box { margin-top: 20px; padding: 15px; border-radius: 6px; display: none; line-height: 1.6; } .result-box.success { background-color: #e6f4ea; border: 1px solid #34a853; display: block; } .result-box.error { background-color: #fce8e6; border: 1px solid #ea4335; display: block; } .bit-display { font-family: monospace; font-size: 1.2em; font-weight: bold; letter-spacing: 2px; background: #fff; padding: 5px 10px; border-radius: 4px; border: 1px inset #ccc; } .parity-bit { color: #d93025; text-decoration: underline; } .article-content { line-height: 1.7; font-size: 16px; } .article-content table { width: 100%; border-collapse: collapse; margin: 20px 0; } .article-content table th, .article-content table td { border: 1px solid #ddd; padding: 10px; text-align: center; } .article-content table th { background-color: #f2f2f2; }

Hamming Code Generator & Error Detector

1. Encode Data Bits

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 + ""; } }

Leave a Reply

Your email address will not be published. Required fields are marked *