{"id":4003,"date":"2022-12-20T08:58:34","date_gmt":"2022-12-20T13:58:34","guid":{"rendered":"http:\/\/www.goodmath.org\/blog\/?p=4003"},"modified":"2022-12-20T08:58:34","modified_gmt":"2022-12-20T13:58:34","slug":"how-computers-work-logic-gates","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2022\/12\/20\/how-computers-work-logic-gates\/","title":{"rendered":"How Computers Work: Logic Gates"},"content":{"rendered":"\n<p>At this point, we&#8217;ve gotten through a very basic introduction to how the electronic components of a computer work. The next step is understanding how a computer can <em>compute<\/em> anything.<\/p>\n\n\n\n<p>There are a bunch of parts to this.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>How do single operations work? That is, if you&#8217;ve got a couple of numbers represented as high\/low electrical signals, how can you string together transistors in a way that produces something like the sum of those two numbers?<\/li>\n\n\n\n<li>How can you <em>store<\/em> values? And once they&#8217;re stored, how can you read them back?<\/li>\n\n\n\n<li>How does the whole thing <em>run<\/em>? It&#8217;s a load of transistors strung together &#8211; how does that turn into something that can do things in sequence? How can it &#8220;read&#8221; a value from memory, interpret that as an instruction to perform an operation, and then select the right operation?<\/li>\n<\/ol>\n\n\n\n<p>In this post, we&#8217;ll start looking at the first of those: how are individual operations implemented using transistors?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Boolean Algebra and Logic Gates<\/h2>\n\n\n\n<p>The mathematical basis is something called <em>boolean algebra<\/em>. Boolean algebra is a simple mathematical system with two values: true and false (or 0 and 1, or high and low, or A and B\u2026 it doesn&#8217;t really matter, as long as there are two, and only two, distinct values).<\/p>\n\n\n\n<p>Boolean algebra looks at the ways that you can combine those true and false values. For example, if you&#8217;ve got exactly one value (a <em>bit<\/em>) that&#8217;s either true or false, there are four operations you can perform on it.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Yes<\/strong>: this operation <em>ignores<\/em> the input, and always outputs True.<\/li>\n\n\n\n<li><strong>No<\/strong>: like Yes, this ignores its input, but in No, it always outputs False.<\/li>\n\n\n\n<li><strong>Id<\/strong>: this outputs the same value as its input. So if its input is true, then it will output true; if its input is false, then it will output false.<\/li>\n\n\n\n<li><strong>Not<\/strong>: this reads its input, and outputs the opposite value. So if the input is true, it will output false; and if the input is false, it will output True.<\/li>\n<\/ol>\n\n\n\n<p>The beauty of boolean algebra is that it can be physically realized by transistor circuits. Any simple, atomic operation that can be described in boolean algebra can be turned into a simple transistor circuit called a <em>gate<\/em>. For most of understanding how a computer works, once we understand gates, we can almost ignore the fact that there are transistors behind the scenes: the gates become our building blocks.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Not Gate<\/h2>\n\n\n\n<figure class=\"wp-block-table alignright is-style-regular has-medium-font-size\"><table><thead><tr><th>Input<\/th><th>Output<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>0<\/td><\/tr><tr><td>0<\/td><td>1<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">The truth table for boolean NOT<\/figcaption><\/figure>\n\n\n\n<p>We&#8217;ll start with the simplest gate: a not gate. A not gate implements the Not operation from boolean algebra that we described above. In a physical circuit, we&#8217;ll interpret a voltage on a wire (&#8220;high&#8221;) as a 1, and no voltage on the wire (&#8220;low&#8221;) as a 0. So a not gate should output low (no voltage) when its input is high, and it should output high (a voltage) when its input is low. We usually write that as something called a <em>truth table<\/em>, which shows all of the possible inputs, and all of the possible outputs. In the truth table, we usually write 0s and 1s: 0 for low (or no current), and 1 for high. For the NOT gate, the truth table has one input column, and one output column.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"alignright size-full\"><a href=\"https:\/\/i0.wp.com\/www.goodmath.org\/blog\/wp-content\/uploads\/2022\/12\/not-gate-1.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"440\" height=\"228\" src=\"https:\/\/i0.wp.com\/www.goodmath.org\/blog\/wp-content\/uploads\/2022\/12\/not-gate-1.png?resize=440%2C228\" alt=\"\" class=\"wp-image-4005\" srcset=\"https:\/\/i0.wp.com\/www.goodmath.org\/blog\/wp-content\/uploads\/2022\/12\/not-gate-1.png?w=440 440w, https:\/\/i0.wp.com\/www.goodmath.org\/blog\/wp-content\/uploads\/2022\/12\/not-gate-1.png?resize=300%2C155 300w\" sizes=\"auto, (max-width: 440px) 100vw, 440px\" \/><\/a><figcaption class=\"wp-element-caption\">Circuit diagram of a NOT gate<\/figcaption><\/figure><\/div>\n\n\n<p>I&#8217;ve got a sketch of a not gate in the image to the side. It consists of two transistors: a standard (default-off) transistor, which is labelled &#8220;B&#8221;, and a complementary transistor (default-on) labeled A. A power supply is provided on the the emitter of transistor A, and then the collector of A is connected to the emitter of B, and the collector of B is connected to ground. Finally, the input is split and connected to the bases of both transistors, and the output is connected to the wire that connects the collector of A and the emitter of B.<\/p>\n\n\n\n<p>That all sounds complicated, but the way that it works is simple. In an electric circuit, the current will always follow the easiest path. If there&#8217;s a short path to ground, the current will always follow that path. And ground is always low (off\/0). Knowing that, let&#8217;s look at what this will do with its inputs.<\/p>\n\n\n\n<p>Suppose that the input is 0 (low). In that case, transistor A will be on,  and transistor B will be off. Since B is off, there&#8217;s no path from the power to ground; and since A is on, cif there&#8217;s any voltage at the input, then current will flow through A to the output.<\/p>\n\n\n\n<p>Now suppose that the input is 1 (high). In that case, A turns off, and B turns on. Since A is off, there&#8217;s no path from the power line to the output. And since B is on, the circuit has connected the output to ground, making it low.<\/p>\n\n\n\n<p>Our not gate is, basically, a switch. If its input is high, then the switch attaches the output to ground; if its input is low, then the switch attaches the output to power.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The NAND gate<\/h2>\n\n\n\n<p>Let&#8217;s try moving on to something more interesting: a NAND gate. A NAND gate takes two inputs, and outputs high when any of its inputs is low. Engineers love NAND gates, because you can create <em>any<\/em> boolean operation by combining NAND gates. We&#8217;ll look at that in a bit more detail later.<\/p>\n\n\n\n<figure class=\"wp-block-table alignright is-style-stripes\"><table><thead><tr><th>Input X<\/th><th>Input Y<\/th><th>Output<\/th><\/tr><\/thead><tbody><tr><td>0<\/td><td>0<\/td><td>1<\/td><\/tr><tr><td>0<\/td><td>1<\/td><td>1<\/td><\/tr><tr><td>1<\/td><td>0<\/td><td>1<\/td><\/tr><tr><td>1<\/td><td>1<\/td><td>0<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">The truth table for NAND<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/i0.wp.com\/www.goodmath.org\/blog\/wp-content\/uploads\/2022\/12\/nand-gate.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"625\" height=\"551\" src=\"https:\/\/i0.wp.com\/www.goodmath.org\/blog\/wp-content\/uploads\/2022\/12\/nand-gate.png?resize=625%2C551\" alt=\"\" class=\"wp-image-4006\" srcset=\"https:\/\/i0.wp.com\/www.goodmath.org\/blog\/wp-content\/uploads\/2022\/12\/nand-gate.png?w=672 672w, https:\/\/i0.wp.com\/www.goodmath.org\/blog\/wp-content\/uploads\/2022\/12\/nand-gate.png?resize=300%2C264 300w, https:\/\/i0.wp.com\/www.goodmath.org\/blog\/wp-content\/uploads\/2022\/12\/nand-gate.png?resize=624%2C550 624w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><\/figure>\n\n\n\n<p>Here&#8217;s a diagram of a NAND gate. Since there&#8217;s a lots of wires running around and crossing each other, I&#8217;ve labeled the transistors, and made each of the wires a different color:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Connections from the power source are drawn in green.<\/li>\n\n\n\n<li>Connections from input X are drawn in blue.<\/li>\n\n\n\n<li>Connections from input Y are drown in red.<\/li>\n\n\n\n<li>The complementary transistors are labelled C1 and C2.<\/li>\n\n\n\n<li>The output of the two complementary transistors is labelled &#8220;cout&#8221;, and drawn in purple.<\/li>\n\n\n\n<li>The two default-off transistors are labelled T1 and T2.<\/li>\n\n\n\n<li>The output from the gate is drawn in brown.<\/li>\n\n\n\n<li>Connections to ground are drawn in black.<\/li>\n<\/ul>\n\n\n\n<p>Let&#8217;s break down how this works: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In the top section, we&#8217;ve got the two complimentary (default-on) transistors. If either of the inputs is 0 (low), then they&#8217;ll stay on, and pass a 1 to the cout line.  There&#8217;s no connection to ground, and there is a connection to power via one (or both) on transistors, so the output of the circuit will be 1 (high).<\/li>\n\n\n\n<li>If neither of the inputs is low, then both C1 and C2 turn off. Cout is then not getting any voltage, and it&#8217;s 0. You might think that this is enough &#8211; but we want to force the output all the way to 0, and there could be some residual electrons in C1 and C2 from the last time they were active. So we need to provide a path to drain that, instead of allowing it to possibly affect the output of the gate. That&#8217;s what T and T2 are for on the bottom. If both X and Y are high, then both T1 and T2 will be on &#8211; and that will provide an open path to ground, draining the system, so that the output is 0 (low).<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Combining Gates<\/h2>\n\n\n\n<p>There are ways of building gates for each of the other basic binary operators in boolean algebra: AND, OR, NOR, XOR, and XNOR. But in fact, we don&#8217;t need to know how to do those &#8211; because in practice,all we need is a NAND gate. You can combine NAND gates to produce any other gate that you want. (Similarly, you can do the same with NOR gates. NAND and NOR are called <em>universal gates<\/em> for this reason.)<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"alignleft size-full is-resized\"><a href=\"https:\/\/i0.wp.com\/www.goodmath.org\/blog\/wp-content\/uploads\/2022\/12\/basic-gates.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.goodmath.org\/blog\/wp-content\/uploads\/2022\/12\/basic-gates.png?resize=313%2C536\" alt=\"\" class=\"wp-image-4007\" width=\"313\" height=\"536\" srcset=\"https:\/\/i0.wp.com\/www.goodmath.org\/blog\/wp-content\/uploads\/2022\/12\/basic-gates.png?w=417 417w, https:\/\/i0.wp.com\/www.goodmath.org\/blog\/wp-content\/uploads\/2022\/12\/basic-gates.png?resize=175%2C300 175w\" sizes=\"auto, (max-width: 313px) 100vw, 313px\" \/><\/a><\/figure><\/div>\n\n\n<p>Let&#8217;s look at how that works. First, we need to know how to draw gates in a schematic form, and what each of the basic operations do. So here&#8217;s a chart of each operation, its name, its standard drawing in a schematic, and its truth table.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"alignright size-full\"><a href=\"https:\/\/i0.wp.com\/www.goodmath.org\/blog\/wp-content\/uploads\/2022\/12\/not-from-nand.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"129\" height=\"42\" src=\"https:\/\/i0.wp.com\/www.goodmath.org\/blog\/wp-content\/uploads\/2022\/12\/not-from-nand.png?resize=129%2C42\" alt=\"\" class=\"wp-image-4009\"\/><\/a><\/figure><\/div>\n\n\n<p class=\"has-text-align-left\">Just like we did with the basic gates above, we&#8217;ll start with NOT. Using boolean logic identities, we can easily derive that <img src='http:\/\/l.wordpress.com\/latex.php?latex=%5Clnot%20A%20%3D%20A%20%5Clnot%5Cland%20A&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='\\lnot A = A \\lnot\\land A' style='vertical-align:1%' class='tex' alt='\\lnot A = A \\lnot\\land A' \/>; or in english, &#8220;not A&#8221; is the same thing as &#8220;not(A nand A)&#8221;. In gates, that&#8217;s easy to build: it&#8217;s a NAND gate with both of its inputs coming from the same place:<\/p>\n\n\n\n<p>For a more interesting one, let&#8217;s look at AND, and see how we can build that using just NAND gates. We can go right back to boolean algebra, and play with identities. We want <img src='http:\/\/l.wordpress.com\/latex.php?latex=A%20%5Cland%20B&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='A \\land B' style='vertical-align:1%' class='tex' alt='A \\land B' \/>. It&#8217;s pretty straightforward in terms of logic: &#8220;<img src='http:\/\/l.wordpress.com\/latex.php?latex=A%20%5Cand%20B&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='A \\and B' style='vertical-align:1%' class='tex' alt='A \\and B' \/>&#8221; is the same as <img src='http:\/\/l.wordpress.com\/latex.php?latex=%5Clnot%20%28A%20%5Clnot%5Cland%20B%29&#038;bg=FFFFFF&#038;fg=000000&#038;s=0' title='\\lnot (A \\lnot\\land B)' style='vertical-align:1%' class='tex' alt='\\lnot (A \\lnot\\land B)' \/>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"alignright size-full\"><a href=\"https:\/\/i0.wp.com\/www.goodmath.org\/blog\/wp-content\/uploads\/2022\/12\/and-from.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"208\" height=\"59\" src=\"https:\/\/i0.wp.com\/www.goodmath.org\/blog\/wp-content\/uploads\/2022\/12\/and-from.png?resize=208%2C59\" alt=\"\" class=\"wp-image-4010\"\/><\/a><\/figure><\/div>\n\n\n<p>That&#8217;s just two NAND gates strung together, like this:<\/p>\n\n\n\n<p>We can do the same basic thing with all of the other basic boolean operations. We start with boolean algebra to figure out equivalences, and then translate those into chains of gates.<\/p>\n\n\n\n<p>With that, we&#8217;ve got the basics of boolean algebra working in transistors. But we still aren&#8217;t doing interesting computations. The next step is building up: combining collections of gates together to do more complicated things. In the next post, we&#8217;ll look at an example of that, by building an <em>adder<\/em>: a network of gates that performs addition!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At this point, we&#8217;ve gotten through a very basic introduction to how the electronic components of a computer work. The next step is understanding how a computer can compute anything. There are a bunch of parts to this. In this post, we&#8217;ll start looking at the first of those: how are individual operations implemented using [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-4003","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-12z","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/4003","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/comments?post=4003"}],"version-history":[{"count":3,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/4003\/revisions"}],"predecessor-version":[{"id":4012,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/4003\/revisions\/4012"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=4003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=4003"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=4003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}