LEARN Logic

Welcome!

Fertile Questions for the Logic chapter of “How to Learn Computer Science”.

Thank you for buying my book! This page discusses the content in the “Logic” chapter and answers the “Fertile Questions” I asked there. There are no perfect answers, however: you may even disagree, but the point of a fertile question is to make you think.

Here are the questions, and my suggested answers. Do you agree?

Advertisements
Advertisements
Why do computers use binary?

Computers use electronic switches called transistors to turn voltages on and off. This can be done at high speed and on a huge scale, with billions of transistors inside modern CPUs.

The transistor allows a 5V signal to be turned on and off electronically at lightning speed, without relays or valves. Representing “True” or “1” as a high voltage, and “False” or “0” as a low voltage, humble transistors can be combined to make logic gates, and those logic gates can be arranged to perform arithmetic.

“How to Learn Computer Science” page 138
Advertisements
What’s inside the ALU?

Lots of Logic Gates! But a logic gate is made up of electronic components like transistors. Here is an AND gate made up of two transistors in series. This AND gate is being used to decide the output of the boolean logic expression “party = tidy_room AND homework” !

Advertisements
How is selection performed inside a computer?

We just need to write our condition as a boolean expression, and use logic gates to evaluate it. For example “IF tidy_room == True AND homework == True” is a selection statement using one AND gate, so we can set it up like the image above and let the AND gate decide the result.

All selection statements (IF-ELIF-ELSE and SWITCH/CASE) are just a series of boolean expressions, rhe result of which decides which instruction to execute next. So when we have the result, we just jump to the appropriate instruction in memory.

Advertisements
Why don’t we use analogue computers anymore?

Computer scientists created analogue computers in the first half of this century. Vannevar Bush created the room-sized “Differential Analyzer” in 1931 which had gears and pulleys to calculate shell trajectories for the military. But electronic analogue computers were unreliable, it’s hard to get a stable voltage because of unstable mains power feeds, interference and component failure.

It’s easy to create a two-voltage machine, and modern computers operate between 0 and 5 volts. Any voltage over about 3.5v is read as a high voltage, meaning True or 1, anything lower is False or 0. We can make circuits that very reliably switch between these voltages and perform binary arithmetic and boolean logic on them.

How does a computer perform arithmetic?

We can write the rules of binary arithmetic in a truth table, then construct a boolean logic circuit to perform it. Here is a simple circuit that will add two bits together, called a “half-adder”. We can easily combine lots of these, so we can add long binary numbers.

Using a method of representing negative numbers called “two’s complement” we can also negate a number, turn it into it’s negative value. Now we can do subtraction too, because a-b is the same as a + (-b).

So, we have addition and subtraction but what of multiplication and division? Well, multiplication is just repeated addition. For example, 3 x 4 is simply 4 + 4 + 4. We just need to repeatedly add 4 while counting up to three or, better still, counting down from 3 to zero. We have all the tools we need for this already: addition, subtraction and compare to zero. Likewise, division is just repeated subtraction: to perform 12 / 3 we just repeatedly subtract 3 from 12, counting the number of times we can do it until we reach zero. A few simple logic gates have given us the power to do really useful mathematics.

“How to Learn Computer Science” page 140
If you are grateful for my blog, please buy my books here or buy me a coffee at ko-fi.com/mraharrisoncs, thanks!
Advertisements