Chapter 01 of 22Part I: Foundations of Digital Information
Magic or Mathematics?
4 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.
Printed book snippet (byte-identical) · 370 bytes · SHA-256: 4ce90ec3c776…
Fragment / Lab-dependent
Dependencies: Browser DOM or chapter Interactive Lab environment. Interacts with browser UI elements or dialogs; inspect alongside the chapter Interactive Lab.
let answer = 0;
for (let weight = 1; weight <= 32; weight *= 2) {
let card = [];
for (let n = 1; n <= 63; n++) {
if (Math.floor(n / weight) % 2 === 1) {
card.push(n);
}
}
let yes = confirm(card.join(", ") +
"\nIs your number on this card?");
if (yes) answer += weight;
}
alert(answer === 0 ? "No valid number" :
"Your number is " + answer);
Printed book snippet (byte-identical) · 823 bytes · SHA-256: dc5930d7755a…
Standalone runnable
Dependencies: None (standard ES2022 JavaScript). Runs directly in Node.js or the isolated browser sandbox and logs output.
let board = [
[1,0,1,0], [0,1,1,1],
[1,1,0,0], [0,0,1,0]
];
function parity(bits) {
let sum = 0;
for (let bit of bits) sum += bit;
return sum % 2;
}
// Add the check column and row.
for (let row of board) row.push(parity(row));
let bottom = [];
for (let c = 0; c < 5; c++) {
bottom.push(parity(board.map(row => row[c])));
}
board.push(bottom);
// Simulate one changed card.
board[2][1] ^= 1;
let badRows = [], badCols = [];
for (let i = 0; i < 5; i++) {
if (parity(board[i])) badRows.push(i);
if (parity(board.map(row => row[i]))) badCols.push(i);
}
if (badRows.length === 1 && badCols.length === 1) {
console.log("Candidate:", badRows[0]+1, badCols[0]+1);
} else if (!badRows.length && !badCols.length) {
console.log("No error detected");
} else {
console.log("Error detected; location uncertain");
}
Printed book snippet (byte-identical) · 137 bytes · SHA-256: 817052f71f8f…
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).
// Only when exactly one error is guaranteed:
if (badRows.length === 1 && badCols.length === 1) {
board[badRows[0]][badCols[0]] ^= 1;
}
Printed book snippet (byte-identical) · 448 bytes · SHA-256: db1620e30aba…
Standalone runnable
Dependencies: None (standard ES2022 JavaScript). Runs directly in Node.js or the isolated browser sandbox and logs output.
function countOnes(bits) {
let count = 0;
for (let bit of bits) {
if (bit === 1) count++;
}
return count;
}
let data = [1,0,1,1,0,0,1,0];
let sent = [...data, countOnes(data) % 2];
let received = [...sent];
received[2] ^= 1;
console.log("Sent:", sent.join(""));
console.log("Received:", received.join(""));
if (countOnes(received) % 2 === 0) {
console.log("No error detected");
} else {
console.log("Transmission error detected");
}