Silicon to Scripting 8
Now that we know the algorithm for creating the Two’s Complement of a number, lets implement it in hardware.
- Start with the absolute value of the number
- Compute the bitwise inversion (invert all bits)
- Add 1 to the number, ignoring overflows
Increment
Just because it’s easier, lets start with INC
.
We need to add to a 16 bit number, ignoring overflow (ignore the carry).
We will need an ADD 16
.
Now all we need is a , but how do we get it? Well we could invert a 0.
Note: I could have left out the “0” component because the default state of an inlet is disconnected, which is 0. I think it’s more clear the way I did it.
Subtraction
We need to compute , which is , which means we need to complute the Two’s Complement of B.
- Start with the absolute value of the number: which is just B
- Compute the bitwise inversion:
inv 16
- Add 1 to the number, ignoring overflows:
inc 16
Now we can add them.
One thing to note is that even though adding to a very large number produces a negative number and vice versa, this is how computers today work. It’s a nasty bug that often gets caught in debug builds via explicit checking. — https://en.wikipedia.org/wiki/Integer_overflow