ctapobep / blog

My personal blog on IT topics in the form of GitHub issues.
6 stars 0 forks source link

How does electricity add numbers? #1

Open ctapobep opened 3 years ago

ctapobep commented 3 years ago

Inspired by the book Code that I've been reading I decided to explain how programs work at the very bottom and we talked about logic gates, electricity and Boolean Algebra. It seems like to explain it we need to cover only these 3 topics:

  1. Using electricity it's possible to create conditional logic (AND, OR, NOT operators, etc).
  2. While we can draw electrical circuits it's often easier to operate using Boolean Algebra to build complex expressions. Then it's very easy to transform it all into a circuit. Thankfully my students were already familiar with Boolean Algebra, though they didn't realize that it can be applied to building electrical circuits. As well as..
  3. Using Boolean Algebra we can build arithmetics (which can then be turned into a circuit). E.g. let's say we'd like to add 2 bits (a and b), the result should be:
0 + 0 = 00
0 + 1 = 01
1 + 1 = 10

Second bit of the result is a sum bit, while the 1st bit is a carry over bit. Here's a Boolean expression that achieves it (' is a negation/inversion):

sum bit    = (a + b) * (a * b)' = a*b' + a'*b
carry over = a * b

Which in many programming languages can be written as:

sumBit    = (a | b) & ~(a & b) = a ^ b
carryOver = a & b

Such circuit is called a Half Adder. I'd like them to realize that computers don't have a notion of a letter or a digit. It's all electricity and bits and numbers are the next level of abstraction. And only after that we get text, graphics and everything else. Eventually we got to this schematic picture:

image

Hopefully this kind of thinking will help to explain a lot of magic with time. And I wish someone had explained it to me when I was starting to write code.

oatkachenko commented 3 years ago

Nice article, keep going! BTW, why don't you use https://pages.github.com/ instead?

ctapobep commented 3 years ago

Seems like Github Issues has everything needed for this purpose - comments, reactions, Markdown, pictures. Anyway, this is an experiment - we'll see how it turns out :)