Computers are Magic

Pulled from: https://xkcd.com/676/

Earlier in my first week at Galvanize we learned how to use the command line. Along the way we talked a bit about the layers of complexity that make up our computers. We talked about the days when a “terminal” referred to a separate device from a computer that was just used to input and display data.

An early terminal in fron of a Z3 computer.

These days things are a little different. In OSX we actually have a terminal emulator which is an application in OS X which in turn is built on the C programing language which translates things into assembly code which is then translated into machine code which is then translated into binary code on a drive or in a processor.

A more detailed breakdown of the OS X architecture.

I don’t know too much about that part because I’m still early in my programing days. But before I settled on math as a major I considered majoring in electrical engineering or physics, so I know a little bit about what happens underneath the binary code. It’s super cool so I’m going to talk a bit about that today.

Everyone knows that binary code is made up of 1’s and 0’s but what does that really mean?

Binary code just floats around in dark ether of cyberspace right?

At rest (in a disk drive, flash drive, or RAM) I’m not actually sure (womp womp). But in transit the 0’s and 1’s translate to an analog electrical signal that is either a higher or lower voltage. Here is a picture showing what the electrical signal for some binary code looks like on an oscilloscope. It also diagrams how the code gets reversed and padded with a 0 at the start and 1 and the end during transit (for lots of good reasons).

This shows the binary signal for the character A, a carriage return, and then a line feed (or new line).

We can preform operations on this signal (e.g. a math operation, renaming a file, or editing a picture) using logic gates, which take in one signal and return another. Basic logic gates consist of AND, OR, and NOT functions (actually sometimes it’s cheaper to just build all of them out of NAND gates, but that’s another story).

Tables showing the input and output voltage levels for simple logic gates.

Computers are pretty complicated and can consist of billions of logic gates, but bellow is an example of how you can make a very simple binary adder with logic gates. When no switches are on, no lights are on. When only one switch is on, the first light is on. When both switches are on, the second light is on (10 in binary means 2):

You can build this or your own logic gate circuits here: https://logic.ly/demo/

These kinds of logic gates could be built out of anything as long as they route inputs and outputs correctly. In 1842 (before electricity had really taken off), Ada Lovelace wrote the first computer program and it was designed to be run on a mechanical device called an “analytical engine.”

The first computer program, written by Ada Lovelace.

But if a computer can have billions of these logic gates controlling the flow of electricity in a circuit, how can we make them small enough to fit in a desktop, laptop, phone, or watch? Well, thats where we get the “silicon” part of “silicon valley.”

In modern computers logic gates are made out of silicon. Silicon is a poor conductor and does not usually allow electrical current to flow through it. By mixing in other elements like boron (which results in excess electrons and is called “p-doped”) or phosphorus (which results in dearth of electrons and is called “n-doped”), silicon can become a better conductor.

Phosphorus had too many electrons and wanted to give some away and boron had too few and wanted more, but silicon was just right with the electrons it had!

When you sandwich two layers of p-doped silicon around a layer of n-doped silicon (or vice versa) something magic happens. Your silicone sandwich will be a very poor conductor, but if you apply a small voltage to the middle layer, all the sudden the sandwich as a whole become a pretty good conductor! Eureka, we have a simple logic gate!

Here you can see you can see a gate made with two p-type layers separated by an n-type layer.

From that simple logic gate we can create more complex logic gates, and from that you get devices that respond to machine code, and assembly, and objective c, and the rest of the OS X stack and finally you have me sitting here clicking away on a keyboard making pictures and characters miraculously appear on my screen!