Silicon to Scripting 4
Up until now, we have been building simple logic gates with abstract meaning. Our next goal is to make a gate that can add numbers. That sounds like a large jump, but as you will soon see, it is nothing new. It all comes down to interpretation.
Meaning and Interpretation
On and off, true
and false
, 1 and 0.
They are basically the same thing, but they mean different things.
We don’t say “can you turn the light true
?”
We can interpret a light being on as true
and off as false
, but we have to do that mapping in our head.
However, we can make more complex mappings from binary 0
s and 1
s to anything.
The meaning isn’t held in the 1
s and 0
s, it’s held in the mapping itself.
By creating and agreeing on a mapping, we can extract meaning from binary.
Look at this sequence
11
11
10
11
01
11
00
It doesn’t mean anything to you… yet.
I could come up with an arbitrary binary mapping, or encoding, for directions, and as long as we both know the meaning, we can communicate in binary.
11 => Go Forward
10 => Turn Left
01 => Turn Right
00 => Stop
Given that, I could direct you to go somewhere with the same sequence.
11
11
10
11
01
11
00
What you just did was interpret 1
s and 0
s as directions, extracting meaning that wasn’t there initially.
Representing Numbers in Binary
Before we make something that can add numbers, we need to be able to represent numbers with 1
s and 0
s.
We need to be able to interpret them as numbers.
For now, we will only be dealing with the Natural Numbers (0, 1, 2, 3, …).
Try and come up with a scheme yourself, it’s a good exercise.
No, really, try it.
If you just saw this problem, you might come up with something along the lines of: count the number of 1
s.
0000 => 0
1000 => 1
1100 => 2
1110 => 3
1111 => 4
This works, but it is inefficient. The maximum number we can represent with 4 binary numbers, also called bits, is 4. If we choose a smarter encoding, we can represent much higher numbers. We could also have the mapping assist us with arethemetic operations, but we’ll see that later.
To get understand how computers actually represent numbers, let’s look at our base 10 system. The number 1337 really means:
This is because it is the base 10 system. Starting at 0, the rightmost digit is multiplied by and so on. We have the 1s place, 10s place, 100s place, …, because , , …
Instead of , , , we will use , , , because we only have 2 possible options for each place (1 or 0). Now if we take a number like 13, in base 10 it would be
but in base 2 it would be
We can just take the coefficients, and form the base 2 number 1101.
Note: To not get confused between different bases, there is some popular notation. Mathimiticians subscript the number with the base: , and programmers prefix the number with a 0b
: 0b1101
.
Comparing this to our previous encoding, our new maximum number is which is also . The maximum number increases exponentially. With 8 bits, our previous system could only represent a maximum of 8, while this one can represent .