Chapter 05 of 22 Part I: Foundations of Digital Information

Recovering the Impossible

5 Verified Book Snippets

Every .js file below is byte-identical (SHA-256 verified) to the printed code in Volume 1. Runnable blocks execute inside an isolated sandboxed iframe and Web Worker with a 2-second timeout and zero network access.

Download Chapter ZIP

block-01.js

Printed book snippet (byte-identical) · 494 bytes · SHA-256: 0870459a1121…
Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions/data; runs cleanly in the sandbox (add a console.log call in your own test to inspect return values).

function encode4(d) {
  if (!/^[01]{4}$/.test(d)) throw Error('Enter four bits');
  const [a,b,c,e] = [...d].map(Number);
  return [a^b^e, a^c^e, a, b^c^e, b, c, e];
}
function syndrome(w) {
  const s1 = w[0]^w[2]^w[4]^w[6];
  const s2 = w[1]^w[2]^w[5]^w[6];
  const s4 = w[3]^w[4]^w[5]^w[6];
  return s1 + 2*s2 + 4*s4;
}
function decode7(received) {
  const w = [...received];
  const s = syndrome(w);
  if (s) w[s-1] ^= 1;
  return {position:s, word:w, data:[w[2],w[4],w[5],w[6]].join('')};
}
Download .js

block-02.js

Printed book snippet (byte-identical) · 406 bytes · SHA-256: 29eb99b0b8c0…
Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions/data; runs cleanly in the sandbox (add a console.log call in your own test to inspect return values).

function xorAll(values) {
  return values.reduce((p,v)=>p^v,0);
}
function makeStripe(a,b,parityDisk) {
  const stripe = [a,b];
  stripe.splice(parityDisk,0,a^b);
  return stripe;
}
function rebuild(stripe) {
  const missing = stripe.filter(v=>v===null).length;
  if (missing !== 1) throw Error('Exactly one missing block required');
  return stripe.map(v=>v===null?xorAll(stripe.filter(x=>x!==null)):v);
}
Download .js

block-03.js

Printed book snippet (byte-identical) · 216 bytes · SHA-256: 036162ebb2f1…
Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Runs directly in Node.js or the isolated browser sandbox and logs output.

const stripes = [makeStripe(12,7,0),
                 makeStripe(9,5,1),
                 makeStripe(8,3,2)];
const damaged = stripes.map(row =>
  row.map((v,i)=>i===1 ? null : v));
console.log(damaged.map(rebuild));
Download .js

block-04.js

Printed book snippet (byte-identical) · 576 bytes · SHA-256: 303f0a04db25…
Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions/data; runs cleanly in the sandbox (add a console.log call in your own test to inspect return values).

function encodeMessage(text) {
  return Array.from(new TextEncoder().encode(text), byte => {
    const s=byte.toString(2).padStart(8,'0');
    return [encode4(s.slice(0,4)),encode4(s.slice(4))];
  }).flat();
}
function decodeMessage(words) {
  if(words.length%2) throw Error('Incomplete byte');
  const results=words.map(decode7);
  const bytes=[];
  for(let i=0;i<results.length;i+=2)
    bytes.push(parseInt(results[i].data+results[i+1].data,2));
  return {text:new TextDecoder().decode(Uint8Array.from(bytes)),
          corrections:results.filter(r=>r.position).length};
}
Download .js

block-05.js

Printed book snippet (byte-identical) · 144 bytes · SHA-256: 8f29c251666c…
Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Runs directly in Node.js or the isolated browser sandbox and logs output.

const words = encodeMessage("MARS");
words.forEach((word,i)=>word[i%7]^=1);
console.log(decodeMessage(words));
// {text: "MARS", corrections: 8}
Download .js