Published
Edited
Apr 27, 2021
1 star
Insert cell
Insert cell
Insert cell
m = 9
Insert cell
n = 2
Insert cell
Insert cell
divide = (a, b) => {
let isNegative = false;
let dividend = a;
let divisor = b;
// check for signed bit of dividend
if (AND(dividend, LSHIFT(1, 31)) == LSHIFT(1, 31)) {
isNegative = !isNegative;
// Negation
dividend = addOne(NOT(dividend));
}
// check for signed bit of divisor
if (AND(divisor, LSHIFT(1, 31)) === LSHIFT(1, 31)) {
isNegative = !isNegative;
divisor = addOne(NOT(divisor));
}
let quotient = 0;
let r = null;
for (let i = 30; i >= 0; i = subtractOne(i)) {
// left shift divisor until it's smaller than dividend
r = LSHIFT(divisor, i);
if (r < Number.MAX_VALUE && r >= 0) {
if (r <= dividend) {
quotient = OR(quotient, LSHIFT(1, i));
dividend = subtract(dividend, r);
}
}
}
if (isNegative) {
quotient = addOne(NOT(quotient));
}
return quotient;
}
Insert cell
divideExample = divide(m, n)
Insert cell
divideProof = parseInt(m / n) === divide(m, n)
Insert cell
Insert cell
Insert cell
add = (a, b) => {
let x = a;
let y = b;
do {
const partialSum = XOR(x, y);
const carry = LSHIFT(AND(x, y), 1);
x = partialSum;
y = carry;
} while (!!y);
return x;
}
Insert cell
addExample = add(m, n)
Insert cell
addProof = (m + n) === add(m, n)
Insert cell
md`## \`addOne(a)\`
Adds \`1\` to \`a\``
Insert cell
addOne = a => add(a, 1)
Insert cell
add1example = addOne(m)
Insert cell
add1proof = (m + 1) === addOne(m)
Insert cell
Insert cell
subtract = (a,b) => add(a, addOne(NOT(b)))
Insert cell
subtractExample = subtract(m, n)
Insert cell
subtractProof = (m - n) === subtract(m,n)
Insert cell
Insert cell
subtractOne = a => subtract(a, 1)
Insert cell
subtractOneExample = subtractOne(m)
Insert cell
subtractOneProof = (m - 1) === subtractOne(m)
Insert cell
Insert cell
Insert cell
NOT = a => ~a
Insert cell
NOTexample = NOT(m)
Insert cell
NOTproof = (~m) === NOT(m)
Insert cell
Insert cell
AND = (a, b) => a & b
Insert cell
ANDexample = AND(m, n)
Insert cell
ANDproof = (m & n) === AND(m, n)
Insert cell
Insert cell
OR = (a, b) => a | b
Insert cell
ORexample = OR(m,n)
Insert cell
ORproof = (m | n) === OR(m,n)
Insert cell
Insert cell
XOR = (a,b) => a ^ b
Insert cell
XORexample = XOR(m, n)
Insert cell
XORproof = (m ^ n) === XOR(m, n)
Insert cell
Insert cell
LSHIFT = (a,b) => a << b
Insert cell
LSHIFTexample = LSHIFT(m, n)
Insert cell
LSHIFTproof = m << n === LSHIFT(m, n)
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more