Public
Edited
Feb 11, 2021
Importers
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
/* Inverts a given number n using the base of its place value system */
// faster than binInvNfromBase64()
invNfromBase64 = (nStr,base) => {
const n = BigInt((base-1)+nStr);
const maxSetBits = BigInt((base-1).toString().repeat(nStr.length+1));
const result = maxSetBits - n;
return result.toString().padStart(nStr.length,'0');
}
Insert cell
Insert cell
binInvNfromBase64 = (nStr) => {
/* Inverts a number from a given base with leading 0s (expected in string format)
Modified from: https://www.geeksforgeeks.org/program-to-invert-bits-of-a-number-efficiently/ */
// !only for quaternary numbers!
// slower than invNfromBase64() when inverting many numbers, because no straight parsing of quaternary numbers as BigInt(?)
// significantly faster than invNfromBase64() when inverting huge numbers, very close but not quite as fast as invQuat()
const binN = quatToBin('3'+nStr);
const n = BigInt(binN);
const m = 1n << BigInt( binN.substr(2).length-1 ); // 1{0…len} from length of binary n
const result = (n ^ (m | m - 1n)).toString(4); // XOR with 1-series
return result.padStart(nStr.length,'0');
}
Insert cell
BigInt(quatToBin(n)).toString().length
Insert cell
Math.log2(quatToBin('3'+n))
Insert cell
quatToBin('3'+'0013').substr(2);
Insert cell
a === b
Insert cell
a === c
Insert cell
c = binInvNfromBase64(n)
Insert cell
b = invQuat(n)
Insert cell
a = invNfromBase64(n,4)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
digitxN = (d,n) => {
/* Builds a number from n x d digits (very slow!) */
let num = 0;
while (n > -1) num += d * (10 ** n), n--;
return num;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more