The world’s leading publication for data science, AI, and ML professionals.

How Do Computers Actually Compute?

A Budding Data Scientist's Introduction to Computer Hardware

Introductory Stuff

You always hear that computers are number crunchers. That they process data at rates much faster than humans could ever hope to achieve and can make 1000s of logical decisions a second. But how do they physically do that? What is so special about these very expensive boxes of rocks that they can (in some ways) out-think their creators? That’s the question we will aim to address in this article.

Photo by Clément Hélardot on Unsplash
Photo by Clément Hélardot on Unsplash

This is the second installment in my new series, A Budding Data Scientist’s Introduction to Computer Hardware. Without assuming any physics, electrical engineering, or low-level computer science background, this series seeks to provide an introduction to computer hardware to the new (or experienced) data scientist looking to deepen their understanding of the tools of the trade. After all, what good workman doesn’t want to be familiar with their tools?

You shouldn’t need the previous installment in the series to follow along (although you can go read it here if you want: Why do Computers even use Binary?). If you are curious about how computers work, but don’t have much of a background in the area, then this series will be a good place for you to start.

Awesome, the introductory stuff is out of the way. Let’s get into the fun stuff!

The Most Basic Digital Circuit Element: The Transistor

The most basic element in a digital circuit is the transistor. Transistors are semiconductor-based components that can act like a switch. Semiconductors (like silicon) are a class of materials that conduct electricity under certain conditions and block electricity under other conditions. By cleverly exploiting the conditions surrounding semiconductor material, we can turn the circuit component "on" and "off", like a switch. (There are other things you can do with a transistor too, but we won’t get into that in this article). The way we "cleverly exploit" the material is by building a transistor out of it.

A transistor has three I/O locations: the base, the collector, and the emitter. In the below diagram of a transistor, the collector is at the top, the base is in the middle left, and the emitter is at the bottom. This arrangement of a transistor is called a NPN (negative-positive-negative) BJT (Bijunction Transistor). There are a lot of other types of transistors, many of which are more popular than BJTs. In fact, transistors used in digital applications are typically FETs (Field Effect Transistors), not BJTs. But, though the technical details are slightly different, the end result is largely the same – all transistors still act as a switch. Since BJTs are a little simpler, we will just focus on them for now.

Image from Pixabay
Image from Pixabay

If you remember one thing about the transistors, remember that a little bit of electricity at the base controls the flow of a larger amount of electricity from the collector to the emitter. Imagine it like turning on and off a faucet. We turn the faucet lever (apply a little electricity at the base) to allow water (electricity) to flow from the water supply (the collector) out through our spigot (the emitter). In the case of BJTs, we manipulate the current to control current flow. In FETs, we manipulate the voltage to control voltage "flow". But the concept is the same either way.

Great, we have established what a transistor is and covered that it uses semiconductors to make a switch. We control a little electricity at the base to turn on/off the flow of electricity between the collector and the emitter. Believe it or not, this idea – a switch – is the building block for everything that goes on in your computer. We can use transistors to design logic gates.

Logic Gates: How a Computer Computes

Logic gates are (somewhat self-evidently) circuit elements that implement basic logic functions. Examples of logic gates are the AND, OR, and NOT gates. The output of a logic gate turns on when its input conditions are satisfied, and turns off otherwise.

Before we dive in, I want to do a little review with you regarding how we equate electricity to data. When we talk about digital circuitry, there is generally a voltage range that represents 0 and a voltage range that represents 1. In short, this means that when there is only a small amount of electricity at an input/output, we take that to mean the value of that input/output is 0. If it is more than just a little bit of electricity, we take it to be a 1. This allows us to do things in a circuit with binary numbers. (If you read the previous article, Why do Computers even use Binary?, we covered this in a lot more detail). Instead of talking about the amount of electricity at an input or output, we simplify things by only talking about 0s and 1s at input or output. Don’t forget, though, that these numbers are being modeled by amounts of electricity.

(Side note – I am lying to you a tiny bit for the sake of simplicity. It isn’t exactly correct to say that voltage is just the amount of electricity – there’s more to it than that. But, equating the two makes the whole concept a little easier to understand, and won’t hamper your understanding of how digital logic circuitry works.)

Awesome, now you’re caught up, so we can dive into logic gates! As an example, let’s look at an AND Gate.

Image from Pixabay
Image from Pixabay

The two prongs on the left are inputs. The prong on the right is the output. Inside the AND gate are a bunch of transistors arranged cleverly, but we will cover that in a second. Here’s how AND gates work: when there is a 1 (a lot of electricity) at both of the inputs, the output is 1. Else, the output is 0. It works just like the "and" keyword in Python! In fact, under the hood, Python (and any programming language, really) use logic gates to actually perform functions like "and", "or" and "not". Isn’t that cool? When you call "and" in Python you are quite literally using a single logic gate within your computer’s CPU! You’ve been using these hardware components (almost) directly the whole time and didn’t know it!

Truth Tables

When we talk about digital circuitry, we want to have a precise way to describe their inputs and outputs. To do this, we use something called a truth table. Here’s the truth table for AND.

Image by Author
Image by Author

Since we have 2 inputs with 2 states, there are 2² = 4 different arrangements for the inputs. So, the truth table has 4 rows. We wanted a circuit that turns on (is at 1) if both inputs are 1, else 0, so that’s what we see on our truth table.

For AND, the truth table is fairly obvious, but for more complicated circuits, truth tables are very useful tools to keep track of how a circuit maps inputs to outputs.

Under the Hood of a Logic Gate

Great, now you know logic gates work at a high level. And you understand the basics of a transistor. Let’s bring the concepts together now – how can we use transistors to build an AND gate? Let’s look at the circuit below.

Image by Author
Image by Author

You have an electricity source at the top. Remember, transistors work like switches, so electricity can’t flow from the source until there is electricity present at the base of the transistor it is connected to. Look at what happens when we put a 1 at both inputs – electricity can flow from the source all the way to the output, so the output is 1. But, if either of the transistors is off (that is, if there is a 0 at either input), then electricity is blocked and the output is 0. That matches our truth table for AND! Now we have a physical component that implements the logic of AND! You can follow a similar design process for OR and NOT as well.

(Side note again – this circuit is a little more simple than a real-life circuit for AND. In reality, you would need a few electrical components called resistors and an electrical ground to make the electricity behave the way you want. But this simplified version represents the bulk of what is happening and gets the point across.)

Combining Logic Gates: Complex Computation

So, we know how logic gates work from the transistor level. We can implement functions like AND, OR, and NOT. But how do we go from there to something more complicated? After all, logic is far from the only computation a computer can do. We do addition, multiplication, division, etc all the time when doing Data Science, so there has to be more to it.

It turns out that many of these higher-order functions can be implemented using logic gates. As an example, let’s look at addition. It only takes two gates: AND and XOR. XOR stands for eXclusive OR. If either of the inputs is 1, then XOR’s output is on. But if both or neither are 1, then the output is 0. Here’s the truth table for XOR.

Image by Author
Image by Author

Take a look at the below circuit:

Image by Author
Image by Author

If you are feeling up to it, try to fill in the below truth table yourself (in your head is fine). See what you come up with.

Image by Author
Image by Author

What happens when both inputs are 0? Then, the AND and XOR are off, so Output and Carry are both off.

Image by Author
Image by Author

What about 0 and 1? Then XOR is on and AND is off.

Image by Author
Image by Author

1 and 0 would be the same, then.

Image by Author
Image by Author

What about 1 and 1? The XOR is off and AND is on!

Image by Author
Image by Author

Do you see how this is a truth table for a binary-adding circuit? The Carry represents carrying a 1 to the next place (like carrying a 1 from the 1’s place to the 10’s place in regular addition) and the output represents the value for that place value! You can check it manually: 1 + 1 = 2. 2 in binary is 0b10. The 1 is represented by the carry and the 0 by the output.

The circuit for a 1-bit adder can be represented by two simple gates. To add more bits, you simply append more of the same adder circuits (this is called cascading). You can build a lot of important circuits using logic gates and truth tables.

Concluding Remarks

In this article, we covered how transistors are used to make logic gates and we covered how logic gates can be combined to create a lot of the computational functions our computer does. You may still have lingering questions – if so, please ask in the comments! One thing we did gloss over is how you can go from the truth table to actually designing the circuit. Because this article was focused more on how the hardware works instead of the design process, it didn’t really fit here. But, I’ll go ahead and give you the answer – you use Boolean Algebra. We will talk about Boolean Algebra in a later section of this series.

That just about wraps it up! This article is the second in my A Budding Data Scientist’s Introduction to Computer Hardware series. Next, we will talk about How Computers Actually Remember!

Like this series? Something you think could be improved? Have an idea for a hardware topic you’d like to see covered? See something I should go deeper on? Anything I went too deep on? Please let me know in the comments. Feedback can really help me make sure that these articles are written at an appropriate level for an audience without hardware background, but still interesting, engaging, and educational. Thank you for reading, and I’ll see you next time!


Related Articles