I've been trying to recreate a binary full subtractor from a logic circuit diagram, but I can't seem to get a output that is accurate. I've rewritten it a few ways and ran it without a loop and expressing the logic differently. Looking at some examples I really don't see what is wrong. The idea is to get 2 n digit numbers as input and mask each bit then preform the subtraction logic. I have a working adder that I could reuse by inverting the second value, but I'm doing this for educational purposes to familiarize myself with the logic of ALUs.
class Alu {
constructor(b = 8) {
this.bits = b;
}
sub(y, z) {
let m = 1; //mask
let d = 0; //diff
let b = 0; //borrow
let w = 10; //base
for (let i = 0; i < this.bits; i++) {
let yM = y & m;
let zM = z & m;
d |= b ^ (yM ^ zM);
b = (~yM & zM) | (~yM & b) | (~zM & b);
m <<= 1;
}
console.log(y.toString(w));
console.log(z.toString(w));
console.log(d.toString(w));
}
}
Here are the reference images I'm using: