Skip to content

Instantly share code, notes, and snippets.

@ivanpepelko
Last active July 6, 2016 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanpepelko/b4c290f4645865f8fec581016a96420e to your computer and use it in GitHub Desktop.
Save ivanpepelko/b4c290f4645865f8fec581016a96420e to your computer and use it in GitHub Desktop.
Simple 7 bit hamming code correction
<!DOCTYPE html>
<html>
<head>
<title>Hamming</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form>
<label for="binary">Number:</label>
<input type="text" name="binary" id="binary"/>
<input type="submit" value="Correct" />
</form>
<ul>
<li>Error at bit: <span id="errorBit"></span></li>
<li>Corrected sequence: <span id="correctSequence"></span></li>
</ul>
<script type="text/javascript">
document.querySelector('form').addEventListener('submit', function (e) {
e.preventDefault();
var seq = (new FormData(e.target)).get('binary').trim();
if (seq.match(/[01]{7}/)) {
var errorBit = parseInt(
String((seq[0] + seq[1] + seq[2] + seq[3]).replace(/0/g, "").length % 2) +
String((seq[0] + seq[1] + seq[4] + seq[5]).replace(/0/g, "").length % 2) +
String((seq[0] + seq[2] + seq[4] + seq[6]).replace(/0/g, "").length % 2), 2);
var correctSequence = seq;
if (errorBit > 0) {
var ei = errorBit - 1; // ei = error bit index
var bits = seq.split('').reverse();
bits[ei] = bits[ei] === "0" ? "1" : "0";
correctSequence = bits.reverse().join('');
}
document.getElementById("errorBit").textContent = errorBit;
document.getElementById("correctSequence").textContent = correctSequence;
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment