Adding Things - Nand2Tetris

Adding Things - Nand2Tetris

Wait... Everything is an Addition? It always has been.

Some of the quotes and information are part of the Nand2Tetris course available on Coursera.

Representing decimal in binary

A decimal can be represented as a sum of powers of 2. In this way,

99 = 64 + 32 + 2 + 1 = 1100011

A word, please.

A word is a unit of data with a fixed number of bits. It tells us, how big of a number a computer can or particularly a processor can evaluate. Now we have 0 or 1 that we can represent using just a single bit. What happens if we want to represent maybe the number 2 or 3? We use 2 bits. So, the length is varying depending on the decimal number. We don't want a computer to guess if 110 is a 12 or if it's a 6. A word defines that length. Now, if we have a 4-bit word, we represent 110 as 0100.

Creating an ALU

The most fundamental thing in an ALU is the Addition functionality.

We build the basic arithmetic this way:

Addition -> Negation -> Subtraction -> Comparision -> (Multiplication -> Division)

Multiplication and Division are implemented in software rather than in the hardware as they are easy to build that way. Multiplication is built using Addition and Division is built using Subtraction. That's the reason why Multiplication is less expensive than a Division operation. So, why is subtraction more expensive you ask. Good question, cause we subtract using a method like this.

y - x = y + (-x)

So, how do we represent Negative Numbers?

Initially, we used something called a sign bit, but it's kinda not elegant.

Usually if you don't have something elegant, it is going to bite you.

So, now we use 2's complement to do this stuff, which is elegant. In 2's complement we represent all numbers as 2^n - x.