<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>EN on Jefferson Oliveira</title><link>https://jeffersonmourak.com/tags/en/</link><description>Recent content in EN on Jefferson Oliveira</description><generator>Hugo -- gohugo.io</generator><language>en</language><copyright>All content provided by this website are my own opinons and beliefs and are distrubuted under the &lt;br/></copyright><lastBuildDate>Tue, 12 May 2026 08:27:04 -0201</lastBuildDate><atom:link href="https://jeffersonmourak.com/tags/en/index.xml" rel="self" type="application/rss+xml"/><item><title>Pleasure to meet you, circ!</title><link>https://jeffersonmourak.com/blog/circ-en/</link><pubDate>Tue, 12 May 2026 08:27:04 -0201</pubDate><guid>https://jeffersonmourak.com/blog/circ-en/</guid><category domain="https://jeffersonmourak.com/tags/compilers/">Compilers</category><category domain="https://jeffersonmourak.com/tags/languages/">Languages</category><category domain="https://jeffersonmourak.com/tags/webassembly/">WebAssembly</category><category domain="https://jeffersonmourak.com/tags/dsl/">DSL</category><category>Compiler</category><category>Zig</category><description>&lt;p>Hey y&amp;rsquo;all! If you&amp;rsquo;ve been following the fundamentals of computing series here, you&amp;rsquo;ve noticed those little widgets. You can click them, toggle the switches, watch the LEDs light up, peek at the circuit topology. Today I want to tell you what&amp;rsquo;s actually running in them, because I made it.&lt;/p>
&lt;p>It&amp;rsquo;s called &lt;code>circ&lt;/code>. It&amp;rsquo;s a small language for describing digital circuits, and a compiler that turns the description into something your browser can run. First of all I want you to understand this isn&amp;rsquo;t a serious systems language (&lt;em>yet&lt;/em>), and I believe it&amp;rsquo;s great for this blog but not ready for production. For now it exists for the sole purpose of having the circuits here &lt;em>actually work&lt;/em>, instead of being static drawings or handcrafted simulators.&lt;/p></description><content:encoded><![CDATA[<p>Hey y&rsquo;all! If you&rsquo;ve been following the fundamentals of computing series here, you&rsquo;ve noticed those little widgets. You can click them, toggle the switches, watch the LEDs light up, peek at the circuit topology. Today I want to tell you what&rsquo;s actually running in them, because I made it.</p>
<p>It&rsquo;s called <code>circ</code>. It&rsquo;s a small language for describing digital circuits, and a compiler that turns the description into something your browser can run. First of all I want you to understand this isn&rsquo;t a serious systems language (<em>yet</em>), and I believe it&rsquo;s great for this blog but not ready for production. For now it exists for the sole purpose of having the circuits here <em>actually work</em>, instead of being static drawings or handcrafted simulators.</p>
<h2 id="how-it-reads">How it reads</h2>
<p>I think the best way to introduce a language is to let you look at it. Here&rsquo;s a half-adder written in <code>.circ</code>:</p>
<pre tabindex="0"><code>input a, b

xor s(a=a, b=b)
and c(a=a, b=b)

output sum(in=s.out)
output carry(in=c.out)
</code></pre><p>That&rsquo;s the whole file. You declare your inputs, you instantiate gates and give them names, you wire each gate&rsquo;s ports to the signals that drive them, and you declare which signals leave through the outputs. Doesn&rsquo;t it read a little like a recipe? <em>Take two inputs. Combine <code>a</code> and <code>b</code> with an XOR, call it <code>s</code>. Combine the same two with an AND, call it <code>c</code>. Send <code>s.out</code> to the <code>sum</code> output, <code>c.out</code> to the <code>carry</code>.</em> That&rsquo;s it.</p>
<p>The primitives are deliberately tiny: <code>input</code>, <code>output</code>, <code>and</code>, <code>not</code>, <code>wire</code>, <code>led</code>. Six things. Everything else is built from those.</p>
<p>And here&rsquo;s a detail I&rsquo;m a little smug about: the built-in gates <code>or</code>, <code>nand</code>, <code>nor</code>, <code>xor</code>, <code>xnor</code> are themselves written in <code>.circ</code>. The compiler&rsquo;s entire &ldquo;standard library&rdquo; is five tiny files that look exactly like the one above. When you write <code>xor s(a=a, b=b)</code>, the compiler quietly pulls in a five-line file that defines XOR in terms of AND, OR, and NAND. The language is small enough to be its own standard library, and that makes me unreasonably happy.</p>
<h2 id="where-it-came-from">Where it came from</h2>
<p><code>circ</code> didn&rsquo;t start as a language. It started as me wanting to draw circuits.</p>
<p>I&rsquo;d been writing this series for a while, and every circuit in every post was a static SVG I&rsquo;d hand-drawn or coaxed out of a TypeScript renderer I kept patching. The drawings worked, but they were <em>just drawings</em>. You couldn&rsquo;t toggle a switch and watch the LED change. You couldn&rsquo;t ask &ldquo;what does this circuit <em>do</em>, exactly?&rdquo; and get an answer. They were illustrations of computers, not computers.</p>
<p>Around the same time I bought <a href="https://www.amazon.com/Elements-Computing-Systems-Building-Principles/dp/0262640686/ref=ed_oe_p"><em>The Elements of Computing Systems</em></a>, and this book hexed me (pun intended) with the curse of curiosity, which led me right into <a href="https://www.nand2tetris.org/"><em>Nand to Tetris</em></a>. In it, you build everything from a single NAND gate, in a tiny language called HDL, and every component you write actually runs. The combination of &ldquo;describe a circuit in a few lines&rdquo; and &ldquo;now press play and watch it work&rdquo; is how circuits should feel when you&rsquo;re learning about them. So I wanted that, for this blog, for you. The widgets in the posts you&rsquo;ve already read are the result.</p>
<p>The name is the most boring part of the story: <code>circ</code> is short for circuit (<em>I am not a poet, sorry.</em>)</p>
<h2 id="the-two-things-i-love-most">The two things I love most</h2>
<p>Once the compiler started working, two things fell out of it that I didn&rsquo;t expect to love as much as I do.</p>
<p>The first is that <code>circ</code> can draw itself. Hand it a circuit and it&rsquo;ll produce an ASCII schematic, complete with rounded corners, fan-out dots, and wires that detour politely around gates that sit in their way. Here&rsquo;s the half-adder from earlier, rendered by the compiler itself:</p>
<pre tabindex="0"><code>╭───╮     ╭───╮         ╭───────╮
│ a ├○─●─▶┤   │ ╭──────▶┤ carry │
╰───╯  │  │AND├○╯       ╰───────╯
      ╭┼─▶┤   │
      ││  ╰───╯
      ││
╭───╮ ││  ╭───────╮     ╭─────╮
│ b ├○●╰─▶┤       │ ╭──▶┤ sum │
╰───╯ │   │[xor:s]├○╯   ╰─────╯
      ╰──▶┤       │
          ╰───────╯
</code></pre><p>The second is that <code>circ</code> can tabulate itself. Hand it the same file and ask for <code>--truth-table</code>, and you get:</p>
<table>
  <thead>
      <tr>
          <th>a</th>
          <th>b</th>
          <th>sum</th>
          <th>cout</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
      </tr>
      <tr>
          <td>1</td>
          <td>0</td>
          <td>1</td>
          <td>0</td>
      </tr>
      <tr>
          <td>0</td>
          <td>1</td>
          <td>1</td>
          <td>0</td>
      </tr>
      <tr>
          <td>1</td>
          <td>1</td>
          <td>0</td>
          <td>1</td>
      </tr>
  </tbody>
</table>
<p>That&rsquo;s the beauty of it: using only 5 lines you have a circuit description, a picture of itself, and proof that it does what it claims. Every interactive widget in every post of this series has been <code>circ</code> doing this quietly in the background, three times over. Once to render the schematic you see, once to run the simulation when you toggle a switch, once to prove the gates behave the way I said they would.</p>
<h2 id="until-we-meet-again">Until we meet again</h2>
<p>That&rsquo;s just the surface of it. There&rsquo;s a parser, a resolver, a validator with stable error codes, a tiny WebAssembly runtime that the compiler embeds inside every artifact so the whole thing runs in your browser without needing any toolchain on your machine. But those are stories for other days.</p>
<p>If you want to dig deeper, the language has a home of its own at <a href="https://circ-lang.org/">circ-lang.org</a>, with the full reference and more examples than I could fit here.</p>
<p>For now, the next time you click on a widget in one of these posts, you&rsquo;ll know what you&rsquo;re looking at. It&rsquo;s <code>circ</code>. Pleasure to meet you.</p>
<h3 id="special-thanks">Special thanks</h3>
<p>Before we say goodbye, I want to give a special shoutout to <a href="https://clarete.li/">@clarete</a>. He is the brain behind <a href="https://clarete.li/langlang/">langlang</a>, the parser generator that powers this language. He has been hammering my ears about langlang for years and I couldn&rsquo;t be more proud of what he has done.</p>
<h2 id="references">References</h2>
<ul>
<li><a href="https://github.com/jeffersonmourak/circ-compiler"><code>circ-compiler</code> on GitHub</a></li>
<li><a href="https://circ-lang.org/">circ-lang.org</a>, the language reference and examples</li>
<li><a href="https://ziglang.org/">Zig</a></li>
<li><a href="https://clarete.li/langlang/">langlang</a>, the PEG parser generator that builds the <code>.circ</code> grammar</li>
<li><a href="https://www.nand2tetris.org/">From Nand to Tetris</a></li>
</ul>
]]></content:encoded></item><item><title>The Evolution of Digital Writing - From ASCII to the Wonders of UTF-8</title><link>https://jeffersonmourak.com/blog/unicode-en/</link><pubDate>Thu, 07 Aug 2025 10:29:04 -0200</pubDate><guid>https://jeffersonmourak.com/blog/unicode-en/</guid><category domain="https://jeffersonmourak.com/tags/unicode/">Unicode</category><category domain="https://jeffersonmourak.com/tags/ascii/">ASCII</category><category domain="https://jeffersonmourak.com/tags/utf-8/">UTF-8</category><description>&lt;blockquote>
&lt;p>Hey Y&amp;rsquo;all! This is a translation of my blog post originally written in Portuguese.&lt;/p>&lt;/blockquote>
&lt;p>Have you ever wondered how computers transform letters, numbers, and emojis into zeros and ones that they can understand? Just like us humans assign meanings to letters of the alphabet, the computer does the same. Let&amp;rsquo;s explore here two of the most popular text encoding standards.&lt;/p>
&lt;h2 id="ascii">ASCII&lt;/h2>
&lt;p>Developed in the 1960s, ASCII (American Standard Code for Information Interchange) has a very simple premise: using only 7 bits, you can represent 127 numbers, reserving the first 32 numbers in the sequence for important writing commands. The rest is filled with letters, numbers, and some punctuation marks.&lt;/p></description><content:encoded><![CDATA[<blockquote>
<p>Hey Y&rsquo;all! This is a translation of my blog post originally written in Portuguese.</p></blockquote>
<p>Have you ever wondered how computers transform letters, numbers, and emojis into zeros and ones that they can understand? Just like us humans assign meanings to letters of the alphabet, the computer does the same. Let&rsquo;s explore here two of the most popular text encoding standards.</p>
<h2 id="ascii">ASCII</h2>
<p>Developed in the 1960s, ASCII (American Standard Code for Information Interchange) has a very simple premise: using only 7 bits, you can represent 127 numbers, reserving the first 32 numbers in the sequence for important writing commands. The rest is filled with letters, numbers, and some punctuation marks.</p>
<p>The people involved in developing the standard did it in such a way that the alphabet sequence could help with decoding.</p>
<p>For example, the character for the number &ldquo;0&rdquo; is <strong>48</strong>, which, represented in 7 bits, becomes <code>011 0000</code>.
Just like:</p>
<p>1 → <code>011 0001</code></p>
<p>2 → <code>011 0010</code></p>
<p>3 → <code>011 0011</code></p>
<p>If you notice, the last 4 bits are in sequence. So, to find out what the integer is in ASCII, you just subtract 011 0000 (decimal: 48).</p>
<p>In the same way, the letters of the alphabet: &ldquo;A&rdquo; is <strong>65</strong> → 100 0001 and &ldquo;a&rdquo; is <strong>97</strong> → 110 0001. With this, it was possible to encode all the letters of the English alphabet 🇬🇧.</p>
<p><em>Interactive Observable notebook widget &mdash; <a href="https://jeffersonmourak.com/blog/unicode-en/">view it on the blog</a>.</em></p>
<p><em>&hellip; meanwhile in the rest of the world &hellip;</em></p>
<figure class="full-width">
  <img src="https://media1.giphy.com/media/v1.Y2lkPTc5MGI3NjExZzNvcXQ4aTBpNGs5YWI3czdjeXBuaTU0dmw3eG44aWNxbTFxbzBieSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/3oz8xQBcUXftkrlmmc/giphy.gif" alt="Scene from South Park series" loading="lazy">
  <figcaption>Scene from South Park series</figcaption>
</figure><p>As you might imagine, with the advancement of technology and computer capacity, each country used this extra capacity to encode their own characters. Japan, for example, didn&rsquo;t even use ASCII. Other encoders, like Shift JIS, used multiple bytes, and with all this, a gigantic incompatibility was generated.</p>
<blockquote>
<p>Fun fact:
In Japan, there&rsquo;s the word mojibake (文字化け), which means &ldquo;distorted character&rdquo;. This happened due to encoding problems between all Japanese alphabets and also the Latin one.</p></blockquote>
<p>However, even with all this incompatibility during the 1980s and 1990s, what were the chances of a London company having to constantly send documents to Japan? At that time, the solution was simple: print and send by fax!</p>
<figure class="full-width">
  <img src="https://media4.giphy.com/media/v1.Y2lkPTc5MGI3NjExY2IzdGtqeHVmemRsZ2cwYTliY2trM2Zla3M2bXZndHF1eHE5b2NkciZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/xT5LMKZ9tnioMLEjBe/giphy.gif" alt="Scene from The Simpsons Series" loading="lazy">
  <figcaption>Scene from The Simpsons Series</figcaption>
</figure><p>Then the internet came, and what was bad got even worse&hellip; Now we have to deal with documents being constantly sent over the internet, and over time the following was formed:</p>
<h2 id="unicode-consortium">Unicode Consortium</h2>
<p>And as in an event that could be called a miracle of common sense, over the last few decades, a standard was formed with 154,998 characters, covering every language you can imagine: Arabic, Japanese, Cyrillic, Chinese, Korean, and even Egyptian hieroglyphs.</p>
<p>What they did in a simplified way was take hundreds of thousands of numbers and assign them to hundreds of thousands of characters, that is, the number 35307 will represent the Japanese character 觫, the number 963 will represent σ, and so on.</p>
<h3 id="utf-8">UTF-8</h3>
<p>Perfect, now we have hundreds of thousands of numbers to represent every possible character, but how are we going to do this with binary?</p>
<p>To represent a number in these proportions, we&rsquo;ll need at least 32 bits to represent any number of that magnitude, which now brought problems for the English alphabet, because Unicode is compatible with ASCII, meaning &ldquo;A&rdquo; is still <strong>65</strong> and &ldquo;a&rdquo; is still <strong>97</strong>. But when we look at the 32-bit binary of these numbers, we now use 4x more space to represent the same characters.</p>
<table>
  <thead>
      <tr>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
          <th></th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td><strong>1</strong></td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td><strong>1</strong></td>
          <td></td>
          <td><strong>A</strong></td>
      </tr>
      <tr>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td><strong>1</strong></td>
          <td><strong>1</strong></td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td>0</td>
          <td><strong>1</strong></td>
          <td></td>
          <td><strong>a</strong></td>
      </tr>
  </tbody>
</table>
<p>Counting above, there are 25 consecutive zeros that will be present in every text that uses Latin characters, and that&rsquo;s just the first of our problems. The second is that some old systems interpret a sequence of 8 zeros [<code>NULL</code>] as the end of a character, the famous <code>\0</code> in C.</p>
<p>So UTF-8 comes in. The first thing is: if the letter has a number below 127, then you represent it exactly the same as ASCII.</p>
<p>So the first problem is solved: &ldquo;A&rdquo; is still <strong>65</strong> and fits in 8 bits. <code>01000001</code>.</p>
<p>And for numbers greater than 127? For that, you&rsquo;ll break your binary into 2 bytes.</p>
<table>
  <thead>
      <tr>
          <th>1</th>
          <th>2</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>110xxxxx</code></td>
          <td><code>10xxxxxx</code></td>
      </tr>
  </tbody>
</table>
<p>In byte 1, you have the header <code>110</code>, which means this character was broken into 2 bytes.
In byte 2, you start with the continuation header <code>10</code>. All other remaining bits you&rsquo;ll fill with the number you want to represent.</p>
<p>To calculate, just remove the headers, join all the bits, and the resulting number is the Unicode character. You can do this up to 4096. Beyond that? No problem! Using the header <code>1110</code> + 2 bytes, you have 16 bits.</p>
<table>
  <thead>
      <tr>
          <th>1</th>
          <th>2</th>
          <th>3</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>1110xxxx</code></td>
          <td><code>10xxxxxx</code></td>
          <td><code>10xxxxxx</code></td>
      </tr>
  </tbody>
</table>
<p>Want to go further? That&rsquo;s fine! The standard supports up to the header <code>1111110x</code> + 6 continuation bytes.</p>
<h3 id="encoding-utf-8">Encoding UTF-8</h3>
<p><em>Interactive Observable notebook widget &mdash; <a href="https://jeffersonmourak.com/blog/unicode-en/">view it on the blog</a>.</em></p>
<p>It&rsquo;s amazing how this standard manages to deliver:</p>
<ul>
<li>
<p>It&rsquo;s compatible with previous systems;</p>
</li>
<li>
<p>It doesn&rsquo;t waste space;</p>
</li>
<li>
<p>And at no point in life will there be 8 consecutive zeros in any part of any byte.</p>
</li>
</ul>
<p>Additionally, another reason that made it become the world standard today is that, to move between characters, if you don&rsquo;t know where you are, you just look for the next header, you don&rsquo;t need an index.</p>
<p>It&rsquo;s been several years since UTF-8 became the standard in all internet communication, and the fact that today the average Japanese person doesn&rsquo;t need to worry about mojibake anymore is because of this brilliant method of encoding text.</p>
<h2 id="references">References</h2>
<ul>
<li><a href="https://www.youtube.com/watch?v=MijmeoH9LT4">Characters, Symbols and the Unicode Miracle - Computerphile (YouTube)</a></li>
<li><a href="https://datatracker.ietf.org/doc/html/rfc3629">UTF-8, a transformation format of ISO 10646 - ietf.org</a></li>
<li><a href="https://joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/">The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) - Joel Spolsky</a></li>
</ul>
]]></content:encoded></item><item><title>The Screen is the Canvas - Drawing Fonts with Mathematics</title><link>https://jeffersonmourak.com/blog/font-rendering-en/</link><pubDate>Thu, 31 Jul 2025 10:29:04 -0200</pubDate><guid>https://jeffersonmourak.com/blog/font-rendering-en/</guid><category domain="https://jeffersonmourak.com/tags/font-rendering/">Font Rendering</category><category domain="https://jeffersonmourak.com/tags/font/">Font</category><category domain="https://jeffersonmourak.com/tags/computer-graphics/">Computer Graphics</category><description>&lt;blockquote>
&lt;p>Hey Y&amp;rsquo;all! This is a translation of my blog post originally written in Portuguese.&lt;/p>&lt;/blockquote>
&lt;p>On Saturday, July 26, 2025, I presented a lecture on font rendering at Google I/O Extended Natal. Due to the rush of daily life, I couldn&amp;rsquo;t show many interactive practical examples. This article serves exactly that purpose: let&amp;rsquo;s explore a bit about how the text you&amp;rsquo;re reading is formed on your screen.&lt;/p>
&lt;p>To start, we&amp;rsquo;ll talk about Bitmaps. This is the most naive way to draw fonts, as a bitmap is nothing more than a ready-made image. Below, for example, we have a letter that occupies a space of 6 pixels in height by 6 pixels in width.&lt;/p></description><content:encoded><![CDATA[<blockquote>
<p>Hey Y&rsquo;all! This is a translation of my blog post originally written in Portuguese.</p></blockquote>
<p>On Saturday, July 26, 2025, I presented a lecture on font rendering at Google I/O Extended Natal. Due to the rush of daily life, I couldn&rsquo;t show many interactive practical examples. This article serves exactly that purpose: let&rsquo;s explore a bit about how the text you&rsquo;re reading is formed on your screen.</p>
<p>To start, we&rsquo;ll talk about Bitmaps. This is the most naive way to draw fonts, as a bitmap is nothing more than a ready-made image. Below, for example, we have a letter that occupies a space of 6 pixels in height by 6 pixels in width.</p>
<p>At the moment, you can easily visualize it because the pixel size is set to 10 pixels. However, if you change the size to 1 pixel, you&rsquo;ll see that it&rsquo;s not possible to read what&rsquo;s on the screen.</p>
<p><em>Interactive p5 widget &mdash; <a href="https://jeffersonmourak.com/blog/font-rendering-en/">view it on the blog</a>.</em></p>
<p>As shown, the biggest problem with bitmap fonts is that they are not scalable. That is, to change the font size, we would have to:</p>
<ul>
<li>
<p>Create a new font with each letter drawn in the new size.</p>
</li>
<li>
<p>Rasterize the font at another scale.</p>
</li>
</ul>
<p>Let&rsquo;s see what happens in the second option.</p>
<p>This is a simple scaling function. It receives as parameters the data of the letter you want to draw and the scale at which you want to increase it.</p>
<p>It works simply by duplicating existing pixels on both the <em>X</em> and <em>Y</em> axes.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#66d9ef">function</span> <span style="color:#a6e22e">scaleGlyph</span> (<span style="color:#a6e22e">glyph</span>, <span style="color:#a6e22e">scale</span>) {
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">newWidth</span> <span style="color:#f92672">=</span> Math.<span style="color:#a6e22e">ceil</span>(<span style="color:#a6e22e">glyph</span>.<span style="color:#a6e22e">width</span> <span style="color:#f92672">*</span> <span style="color:#a6e22e">scale</span>);
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">newHeight</span> <span style="color:#f92672">=</span> Math.<span style="color:#a6e22e">ceil</span>(<span style="color:#a6e22e">glyph</span>.<span style="color:#a6e22e">height</span> <span style="color:#f92672">*</span> <span style="color:#a6e22e">scale</span>);
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">newPixels</span> <span style="color:#f92672">=</span> Array.<span style="color:#a6e22e">from</span>({ <span style="color:#a6e22e">length</span><span style="color:#f92672">:</span> <span style="color:#a6e22e">newHeight</span> }, () =&gt;
</span></span><span style="display:flex;"><span>    Array(<span style="color:#a6e22e">newWidth</span>).<span style="color:#a6e22e">fill</span>(<span style="color:#ae81ff">0</span>)
</span></span><span style="display:flex;"><span>  );
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">let</span> <span style="color:#a6e22e">y</span> <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>; <span style="color:#a6e22e">y</span> <span style="color:#f92672">&lt;</span> <span style="color:#a6e22e">glyph</span>.<span style="color:#a6e22e">height</span>; <span style="color:#a6e22e">y</span><span style="color:#f92672">++</span>) {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">let</span> <span style="color:#a6e22e">x</span> <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>; <span style="color:#a6e22e">x</span> <span style="color:#f92672">&lt;</span> <span style="color:#a6e22e">glyph</span>.<span style="color:#a6e22e">width</span>; <span style="color:#a6e22e">x</span><span style="color:#f92672">++</span>) {
</span></span><span style="display:flex;"><span>      <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">value</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">glyph</span>.<span style="color:#a6e22e">pixels</span>[<span style="color:#a6e22e">y</span>][<span style="color:#a6e22e">x</span>];
</span></span><span style="display:flex;"><span>      <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">newX</span> <span style="color:#f92672">=</span> Math.<span style="color:#a6e22e">floor</span>(<span style="color:#a6e22e">x</span> <span style="color:#f92672">*</span> <span style="color:#a6e22e">scale</span>);
</span></span><span style="display:flex;"><span>      <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">newY</span> <span style="color:#f92672">=</span> Math.<span style="color:#a6e22e">floor</span>(<span style="color:#a6e22e">y</span> <span style="color:#f92672">*</span> <span style="color:#a6e22e">scale</span>);
</span></span><span style="display:flex;"><span>      <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">let</span> <span style="color:#a6e22e">dy</span> <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>; <span style="color:#a6e22e">dy</span> <span style="color:#f92672">&lt;</span> <span style="color:#a6e22e">scale</span>; <span style="color:#a6e22e">dy</span><span style="color:#f92672">++</span>) {
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">for</span> (<span style="color:#66d9ef">let</span> <span style="color:#a6e22e">dx</span> <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>; <span style="color:#a6e22e">dx</span> <span style="color:#f92672">&lt;</span> <span style="color:#a6e22e">scale</span>; <span style="color:#a6e22e">dx</span><span style="color:#f92672">++</span>) {
</span></span><span style="display:flex;"><span>          <span style="color:#66d9ef">if</span> (<span style="color:#a6e22e">newY</span> <span style="color:#f92672">+</span> <span style="color:#a6e22e">dy</span> <span style="color:#f92672">&lt;</span> <span style="color:#a6e22e">newHeight</span> <span style="color:#f92672">&amp;&amp;</span> <span style="color:#a6e22e">newX</span> <span style="color:#f92672">+</span> <span style="color:#a6e22e">dx</span> <span style="color:#f92672">&lt;</span> <span style="color:#a6e22e">newWidth</span>) {
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">newPixels</span>[<span style="color:#a6e22e">newY</span> <span style="color:#f92672">+</span> <span style="color:#a6e22e">dy</span>][<span style="color:#a6e22e">newX</span> <span style="color:#f92672">+</span> <span style="color:#a6e22e">dx</span>] <span style="color:#f92672">=</span> <span style="color:#a6e22e">value</span>;
</span></span><span style="display:flex;"><span>          }
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>      }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>  }
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">return</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">width</span><span style="color:#f92672">:</span> <span style="color:#a6e22e">newWidth</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">height</span><span style="color:#f92672">:</span> <span style="color:#a6e22e">newHeight</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">pixels</span><span style="color:#f92672">:</span> <span style="color:#a6e22e">newPixels</span>,
</span></span><span style="display:flex;"><span>  };
</span></span><span style="display:flex;"><span>};
</span></span></code></pre></div><p>The problem with this type of scaling is that fonts end up getting a blocky appearance, which brings the feeling of a low-resolution image.</p>
<p>Another way is by applying scaling using linear interpolation. This technique consists of taking an average of all the original points around, instead of simply copying the entire block, blindly repeating what&rsquo;s in the pixel. However, this now results in a blurred image appearance, and this characteristic becomes more pronounced the greater the difference between the original size and the final size.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#66d9ef">function</span> <span style="color:#a6e22e">lerp</span>(<span style="color:#a6e22e">x0</span>, <span style="color:#a6e22e">v0</span>, <span style="color:#a6e22e">x1</span>, <span style="color:#a6e22e">v1</span>, <span style="color:#a6e22e">x</span>) {
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">if</span> (<span style="color:#a6e22e">x0</span> <span style="color:#f92672">===</span> <span style="color:#a6e22e">x1</span>) {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">v0</span>;
</span></span><span style="display:flex;"><span>  }
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">v0</span> <span style="color:#f92672">+</span> (<span style="color:#a6e22e">v1</span> <span style="color:#f92672">-</span> <span style="color:#a6e22e">v0</span>) <span style="color:#f92672">*</span> ((<span style="color:#a6e22e">x</span> <span style="color:#f92672">-</span> <span style="color:#a6e22e">x0</span>) <span style="color:#f92672">/</span> (<span style="color:#a6e22e">x1</span> <span style="color:#f92672">-</span> <span style="color:#a6e22e">x0</span>));
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#66d9ef">function</span> <span style="color:#a6e22e">bilinearInterpolate</span>(<span style="color:#a6e22e">Q11</span>, <span style="color:#a6e22e">Q21</span>, <span style="color:#a6e22e">Q12</span>, <span style="color:#a6e22e">Q22</span>, <span style="color:#a6e22e">x</span>, <span style="color:#a6e22e">y</span>) {
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">if</span> (
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Q11</span>.<span style="color:#a6e22e">x</span> <span style="color:#f92672">!==</span> <span style="color:#a6e22e">Q12</span>.<span style="color:#a6e22e">x</span> <span style="color:#f92672">||</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Q21</span>.<span style="color:#a6e22e">x</span> <span style="color:#f92672">!==</span> <span style="color:#a6e22e">Q22</span>.<span style="color:#a6e22e">x</span> <span style="color:#f92672">||</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Q11</span>.<span style="color:#a6e22e">y</span> <span style="color:#f92672">!==</span> <span style="color:#a6e22e">Q21</span>.<span style="color:#a6e22e">y</span> <span style="color:#f92672">||</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Q12</span>.<span style="color:#a6e22e">y</span> <span style="color:#f92672">!==</span> <span style="color:#a6e22e">Q22</span>.<span style="color:#a6e22e">y</span>
</span></span><span style="display:flex;"><span>  ) {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">error</span>(
</span></span><span style="display:flex;"><span>      <span style="color:#e6db74">&#34;Error: The provided points do not form a proper rectangle for bilinear interpolation.&#34;</span>
</span></span><span style="display:flex;"><span>    );
</span></span><span style="display:flex;"><span>  }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">x1</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">Q11</span>.<span style="color:#a6e22e">x</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">x2</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">Q21</span>.<span style="color:#a6e22e">x</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">y1</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">Q11</span>.<span style="color:#a6e22e">y</span>;
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">y2</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">Q12</span>.<span style="color:#a6e22e">y</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">R1</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">lerp</span>(<span style="color:#a6e22e">x1</span>, <span style="color:#a6e22e">Q11</span>.<span style="color:#a6e22e">value</span>, <span style="color:#a6e22e">x2</span>, <span style="color:#a6e22e">Q21</span>.<span style="color:#a6e22e">value</span>, <span style="color:#a6e22e">x</span>);
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">R2</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">lerp</span>(<span style="color:#a6e22e">x1</span>, <span style="color:#a6e22e">Q12</span>.<span style="color:#a6e22e">value</span>, <span style="color:#a6e22e">x2</span>, <span style="color:#a6e22e">Q22</span>.<span style="color:#a6e22e">value</span>, <span style="color:#a6e22e">x</span>);
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">const</span> <span style="color:#a6e22e">P</span> <span style="color:#f92672">=</span> <span style="color:#a6e22e">lerp</span>(<span style="color:#a6e22e">y1</span>, <span style="color:#a6e22e">R1</span>, <span style="color:#a6e22e">y2</span>, <span style="color:#a6e22e">R2</span>, <span style="color:#a6e22e">y</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">P</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>With this we have the examples below:</p>
<p><em>Interactive p5 widget &mdash; <a href="https://jeffersonmourak.com/blog/font-rendering-en/">view it on the blog</a>.</em></p>
<h2 id="how-to-use-one-font-for-multiple-sizes">How to use one font for multiple sizes?</h2>
<p>In mathematics, there are equations that draw a graph on the screen. The most common examples are:</p>
<h3 id="quadratic-function">Quadratic function</h3>
<p><em>Interactive Observable notebook widget &mdash; <a href="https://jeffersonmourak.com/blog/font-rendering-en/">view it on the blog</a>.</em></p>
<h3 id="multiplicative-inverse-function">Multiplicative inverse function</h3>
<p><em>Interactive Observable notebook widget &mdash; <a href="https://jeffersonmourak.com/blog/font-rendering-en/">view it on the blog</a>.</em></p>
<h3 id="manipulating-the-curve">Manipulating the curve</h3>
<p>To move our equations, we can add any value after the result of the exponentiation, and thus we move our equation on the <em>Y</em> axis.</p>
<p><em>Interactive Observable notebook widget &mdash; <a href="https://jeffersonmourak.com/blog/font-rendering-en/">view it on the blog</a>.</em></p>
<p>To move our equation horizontally, we add that value before squaring it.</p>
<p><em>Interactive Observable notebook widget &mdash; <a href="https://jeffersonmourak.com/blog/font-rendering-en/">view it on the blog</a>.</em></p>
<p>So, we already have a way to represent our curves using mathematical equations.</p>
<p>But before we draw, let&rsquo;s learn about one more thing.</p>
<h2 id="bézier-curves">Bézier curves</h2>
<p>It&rsquo;s a polynomial curve expressed as the linear interpolation between some representative points, called control points.</p>
<p>In the example below, we have 3 points: <em>P0</em>, <em>P1</em> and <em>P2</em>, where <em>P0</em> and <em>P2</em> are the representative points and <em>P1</em> is the control point, you can move the examples below and see the result.</p>
<p><em>Interactive p5 widget &mdash; <a href="https://jeffersonmourak.com/blog/font-rendering-en/">view it on the blog</a>.</em></p>
<h3 id="drawing-a-letter-with-vectors">Drawing a letter with vectors</h3>
<p><em>Interactive p5 widget &mdash; <a href="https://jeffersonmourak.com/blog/font-rendering-en/">view it on the blog</a>.</em></p>
<p>With the Bézier concept, it becomes quite intuitive how we can draw a letter using mathematics: just organize points in sequence and mix straight lines with Bézier curves, making the <em>P2</em> of one end exactly where the <em>P0</em> of the other begins.</p>
<p>By the way, a straight line can also be made with Bézier; just align all the points. This way, it becomes even clearer how interpolation acts on the Bézier curve.</p>
<p><em>Interactive p5 widget &mdash; <a href="https://jeffersonmourak.com/blog/font-rendering-en/">view it on the blog</a>.</em></p>
<p>With this, we can now think about how to transform this into a bitmap. To do this, we first need to rasterize this font, starting by translating the Bézier curves into lines compatible with the screen resolution. This happens because the computer screen is a matrix of pixels; therefore, we need to transform curves into pixels readable to the human eye.</p>
<p><em>Interactive p5 widget &mdash; <a href="https://jeffersonmourak.com/blog/font-rendering-en/">view it on the blog</a>.</em></p>
<p>Once this is done, the last thing needed is to fill the letter. This part can be done by a process called scanline, which consists of launching a ray and counting how many times that ray will touch one of the walls of the letter. If the number of touches is even, the pixel is represented outside the letter; if it&rsquo;s odd, it&rsquo;s inside.</p>
<div class="figure-row">
  <figure>
      <img src="outter-intersect.png" alt="2 intersections with the letter">
      <figcaption>2 intersections with the letter</figcaption>
  </figure>
  <figure>
      <img src="in-vs-out.png" alt="2 and 3 intersections with the letter">
      <figcaption>2 and 3 intersections with the letter</figcaption>
  </figure>
  <figure>
      <img src="full-example.png" alt="2, 3, and 4 intersections with the letter">
      <figcaption>2, 3, and 4 intersections with the letter</figcaption>
  </figure>
</div>
<p>Notice that in the example of the letter &lsquo;O&rsquo;, there&rsquo;s a rendering flaw. It&rsquo;s there on purpose: the process of rendering fonts is complicated and full of edge cases that only increase the more we delve into the subject.</p>
<p>What I want to demonstrate with this flaw is that, besides counting how many times your line cuts the letter, you should also be aware if the line is cutting itself again.</p>
<p><em>Interactive p5 widget &mdash; <a href="https://jeffersonmourak.com/blog/font-rendering-en/">view it on the blog</a>.</em></p>
<p>Well, and with this, we conclude this stage of the font rendering process. In a few days, I&rsquo;ll publish two more articles on the topic to complement the lecture subject. They will be about Unicode and Text Shaping.</p>
<p>Thank you very much, and see you next time! 😊</p>
<figure>
  <img src="https://media2.giphy.com/media/v1.Y2lkPTc5MGI3NjExdXM1amV6MWJ0eWs4cjY3OHJhNXNwZ2o1ZWkzYnVrNjMxZTlwNmlpcyZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/1jkVi22T6iUrQJUNqk/giphy.gif" alt="Jimmy Fallon sliding and waiving bye" loading="lazy">
  <figcaption>Jimmy Fallon sliding and waiving bye</figcaption>
</figure><h2 id="references">References</h2>
<ul>
<li><a href="https://www.youtube.com/watch?v=qcMuyHzhvpI">A Brief look at Text Rendering - VoxelRifts (YouTube)</a></li>
<li><a href="https://www.youtube.com/watch?v=SO83KQuuZvg">Coding Adventure: Rendering Text -Sebastian Lague (YouTube)</a></li>
<li><a href="https://www.youtube.com/watch?v=LaYPoMPRSlk">The Math Behind Font Rasterization | How it Works - GamesWithGame (YouTube)</a></li>
<li><a href="https://faultlore.com/blah/text-hates-you/">Text Rendering Hates You - Aria Desires</a></li>
<li><a href="https://github.com/Chlumsky/msdfgen">Multi-channel signed distance field generator - Viktor Chlumský[Valve] (GitHub)</a></li>
<li><a href="https://github.com/harfbuzz/harfbuzz">Harfbuzz[Google] - (GitHub)</a></li>
</ul>
<!--
  p5.js is loaded here as a plain global so the inline `p5` codeblocks
  on this page can reference `window.p5` directly. A non-deferred
  <script> blocks parsing where it sits, so by the time any inline
  module bundle runs, `window.p5` is guaranteed to exist.
-->
<script src="https://cdn.jsdelivr.net/npm/p5@1.11.3/lib/p5.min.js"></script>
]]></content:encoded></item><item><title>Remember me?</title><link>https://jeffersonmourak.com/blog/memory-en/</link><pubDate>Sun, 19 Jan 2025 08:27:04 -0200</pubDate><guid>https://jeffersonmourak.com/blog/memory-en/</guid><category domain="https://jeffersonmourak.com/tags/cpu/">CPU</category><category domain="https://jeffersonmourak.com/tags/memory/">Memory</category><category domain="https://jeffersonmourak.com/tags/combinational/">Combinational</category><category domain="https://jeffersonmourak.com/tags/sequential/">Sequential</category><description>&lt;blockquote>
&lt;p>Hey Y&amp;rsquo;all! This is a translation of my blog post originally written in Portuguese.&lt;/p>&lt;/blockquote>
&lt;blockquote>
&lt;p>Hello everyone, this is the third article in the series about computers, and also the last one with this header here, because from here on it&amp;rsquo;s recommended that you have already read the two previous articles:&lt;/p>
&lt;ul>
&lt;li>&lt;a href="https://jeffersonmourak.com/blog/logic-gates/">Logic gates&lt;/a>.&lt;/li>
&lt;li>&lt;a href="https://jeffersonmourak.com/blog/the-binary/">Pleasure to meet you, Binary.&lt;/a>.&lt;/li>
&lt;/ul>&lt;/blockquote>
&lt;p>In the previous articles, we saw how logic gates can be used to make comparisons and also calculate arithmetic operations, but we haven&amp;rsquo;t yet seen how the computer can store information over time.&lt;/p></description><content:encoded><![CDATA[<blockquote>
<p>Hey Y&rsquo;all! This is a translation of my blog post originally written in Portuguese.</p></blockquote>
<blockquote>
<p>Hello everyone, this is the third article in the series about computers, and also the last one with this header here, because from here on it&rsquo;s recommended that you have already read the two previous articles:</p>
<ul>
<li><a href="https://jeffersonmourak.com/blog/logic-gates/">Logic gates</a>.</li>
<li><a href="https://jeffersonmourak.com/blog/the-binary/">Pleasure to meet you, Binary.</a>.</li>
</ul></blockquote>
<p>In the previous articles, we saw how logic gates can be used to make comparisons and also calculate arithmetic operations, but we haven&rsquo;t yet seen how the computer can store information over time.</p>
<p>Another thing we&rsquo;ll do here is categorize each type of chip: the ones shown previously are <em><strong>combinational</strong></em>, which means they don&rsquo;t need anything beyond signals at their inputs to be able to compute a result, but there are also <em><strong>sequential</strong></em> ones. The difference between them is that sequential depends not only on the input signals, but also on the previously processed value.</p>
<p>A very practical way to use logic gates to store information is using a chip called <em><strong>flip-flop</strong></em>, which is represented below. It contains 2 inputs: the top one <code>set</code> and the bottom one <code>reset</code> (you can click on the circuit below to interact with them).</p>
<p><em>Interactive circ widget &mdash; <a href="https://jeffersonmourak.com/blog/memory-en/">view it on the blog</a>.</em></p>
<pre><code>╭───────╮     ╭───╮           ╭───╮     ╭───╮
│ reset ├○───▶┤NOT├○╮     ╭──▶┤   │ ╭──▶┤LED│
╰───────╯     ╰───╯ │     │   │AND├○●   ╰───╯
                    ╰─────┼──▶┤   │ │        
                          │   ╰───╯ │        
            ╭─────────────┼─────────╯        
╭─────╮     │ ╭─────────╮ │                  
│ set ├○╮   ╰▶┤         │ │                  
╰─────╯ │     │[or:_or_]├○╯                  
        ╰────▶┤         │                    
              ╰─────────╯                    </code></pre>
<p>If you stop to analyze, it&rsquo;s quite simple: following the <code>set</code> trail, you&rsquo;ll see there are two logic gates in the path, an <code>OR</code> and an <code>AND</code>. One of the <code>OR</code> gate inputs is connected to the result of the <code>AND</code> at the end of the chip. This combination makes it so that when we have <strong>0</strong> and are given a value <strong>1</strong>, the <code>OR</code> gate will result in <strong>1</strong>, and the <code>AND</code> will also.</p>
<p>However, when we remove the signal from <code>set</code>, the value that was previously placed is persisted because now the <code>OR</code> is maintaining the state that keeps the <code>AND</code> also in the previous state. Problem solved, right? No! Notice that our circuit is now locked, as there&rsquo;s no way to get out of this state unless the <code>AND</code> gets another value, and that&rsquo;s what the <code>reset</code> gate is for: it&rsquo;s connected to a <code>NOT</code>, meaning while it&rsquo;s off (0), its result will be 1 (on), and vice versa.</p>
<p>An evolution we can make to the <em><strong>flip-flop</strong></em> is to transform it into a <em><strong>Register</strong></em>. In the register, we can choose whether it&rsquo;s time or not to read our signal and store it, and subsequently rewrite it, all at our leisure. There are many ways to achieve this chip, so I&rsquo;ll use the implementation demonstrated by <a href="https://www.youtube.com/watch?v=I0-izyq6q5s">Sebastian Lague</a>.</p>
<p>In it, we just need to add 3 more logic gates and that&rsquo;s it!</p>
<p><em>Interactive circ widget &mdash; <a href="https://jeffersonmourak.com/blog/memory-en/">view it on the blog</a>.</em></p>
<pre><code>╭──────╮        ╭───╮     ╭───╮     ╭───╮     ╭───╮     ╭───╮     ╭───╮     ╭───╮     ╭───╮
│ data ├○──●───▶┤   │  ╭─▶┤NOT├○┬──▶┤NOT├○───▶┤   │ ╭──▶┤NOT├○╮ ╭▶┤   │ ╭──▶┤NOT├○●──▶┤LED│
╰──────╯   │    │AND├○─╯  ╰───╯ │   ╰───╯     │AND├○╯   ╰───╯ │ │ │AND├○╯   ╰───╯ │   ╰───╯
           │╭──▶┤   │           │           ╭▶┤   │           ╰─┼▶┤   │           │        
           ││   ╰───╯           │           │ ╰───╯             │ ╰───╯           │        
           ││                   ├───────────┼───────────────────╯                 │        
╭─────────╮││   ╭───╮     ╭───╮ │           │                                     │        
│ enabled ├○┼──▶┤NOT├○───▶┤   │ │           ╰─────────────────────────────────────╯        
╰─────────╯ │   ╰───╯     │AND├○╯                                                          
            ╰────────────▶┤   │                                                            
                          ╰───╯                                                            </code></pre>
<p>If we break it down a bit, what happens is the following: the upper input, which we&rsquo;ll now call <code>data</code>, will only be saved in the <em><strong>flip-flop</strong></em> when the lower input, which also changed names, now called <code>enabled</code>. So instead of using two inputs, one to save and another to erase, this combination of <code>AND</code>s and <code>NOT</code> chooses which operation will be performed.</p>
<ul>
<li>If <code>data</code> = 1 and <code>enabled</code> = 1, then it&rsquo;s the same as turning on the <code>set</code> input of the <em><strong>flip-flop</strong></em>.</li>
<li>If <code>data</code> = 0 and <code>enabled</code> = 1, then it&rsquo;s the same as turning on the <code>reset</code> input of the <em><strong>flip-flop</strong></em>.
Notice that <code>enabled</code> always needs to be activated so that it can save the signal that was placed in <code>data</code>.</li>
</ul>
<p>Another thing we can do is take inspiration from the previous article, where at the end we learned that if we combine several Adders, we can do calculations with multiple digits. In memory it&rsquo;s the same way: combining several registers, you can provide an input for each <code>data</code> and share the <code>enabled</code> signal, and thus you can save the values of an entire binary number.</p>
<p>For now, we say goodbye here, see you next time 😄</p>
<figure>
  <img src="https://media2.giphy.com/media/v1.Y2lkPTc5MGI3NjExOXlucmJmbW5mZWp4MGRpOXhpZWZwczdma2xmemIxZGVjZ3hiZG5sciZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/m9eG1qVjvN56H0MXt8/giphy.gif" alt="Bye bye!" loading="lazy">
  <figcaption>Bye bye!</figcaption>
</figure><h2 id="references">References</h2>
<ul>
<li><a href="https://www.youtube.com/watch?v=I0-izyq6q5s">How Do Computers Remember? (YouTube)</a></li>
<li><a href="https://www.youtube.com/watch?v=KM0DdEaY5sY">SR latch (YouTube)</a></li>
<li><a href="https://www.youtube.com/watch?v=peCh_859q7Q">D latch</a></li>
<li><a href="https://www.youtube.com/watch?v=HjneAhCy2N4">HOW TRANSISTORS RUN CODE? (YouTube)</a></li>
<li><a href="https://www.nand2tetris.org/">From Nand to Tetris</a></li>
</ul>
]]></content:encoded></item><item><title>Pleasure to meet you, Binary.</title><link>https://jeffersonmourak.com/blog/the-binary-en/</link><pubDate>Sat, 14 Dec 2024 08:27:04 -0200</pubDate><guid>https://jeffersonmourak.com/blog/the-binary-en/</guid><category domain="https://jeffersonmourak.com/tags/cpu/">CPU</category><category domain="https://jeffersonmourak.com/tags/binary/">Binary</category><category domain="https://jeffersonmourak.com/tags/numeric-systems/">Numeric-Systems</category><category>Computing</category><description>&lt;blockquote>
&lt;p>Hey Y&amp;rsquo;all! This is a translation of my blog post originally written in Portuguese.&lt;/p>&lt;/blockquote>
&lt;blockquote>
&lt;p>Hello people of this internet, for those who are arriving here now, this is the second article of
a series I&amp;rsquo;m doing to talk about the fundamentals of computing, I recommend reading the
first one about &lt;a href="https://jeffersonmourak.com/blog/logic-gates/">logic gates&lt;/a>.
And for those who have already read the first article, take a quick look there because I did a revision of the article, I highly recommend taking a look 😊&lt;/p></description><content:encoded><![CDATA[<blockquote>
<p>Hey Y&rsquo;all! This is a translation of my blog post originally written in Portuguese.</p></blockquote>
<blockquote>
<p>Hello people of this internet, for those who are arriving here now, this is the second article of
a series I&rsquo;m doing to talk about the fundamentals of computing, I recommend reading the
first one about <a href="https://jeffersonmourak.com/blog/logic-gates/">logic gates</a>.
And for those who have already read the first article, take a quick look there because I did a revision of the article, I highly recommend taking a look 😊</p></blockquote>
<p>Let&rsquo;s dive a bit deeper and explore some concepts of mathematics and also human history.</p>
<p>Mathematics came from the human race&rsquo;s need to count and measure – how many animals Maria takes care of, how many days until the next solstice&hellip; Over time and human expansion, ancient peoples created various numbering systems for their daily lives. Games like <em>Grand Theft Auto V</em>, <em>Call of Duty: Black Ops III</em>, <em>Dragon Quest XI</em> are examples of using the numeral system that the Romans used, and the <em>I Ching</em>, an ancient Chinese text, has roots in binary representations.</p>
<p>However, not only ancient civilizations used other numerical systems. The hours of a day are divided between 12 hours of the morning period and 12 hours of the afternoon period, or a dozen eggs. In the French language, there are still remnants of a vigesimal-based system (20), for example, the number 80 is said as <em>quatre-vingts</em> (four twenties), and one of the most recent is Braille, which also has roots in binary representations.</p>
<p>Nowadays, we use decimal representation, and the reason is quite simple: the average number of fingers on human hands is an incredible 10 fingers, so it&rsquo;s easier to count using only hands.</p>
<h2 id="----">☝️ + ☝️ = ✌️</h2>
<p>This base 10 representation is positional, meaning the position in which the digits are &ldquo;drawn&rdquo; changes the quantity being represented. <em>70</em> represents ten times more than <em>07</em>, hence the popular saying &ldquo;zero to the left&rdquo;. In school, we learn about decimal places – <em>units</em>, <em>tens</em>, <em>hundreds</em>&hellip; They serve to represent what the position of that digit is.</p>
<table>
  <thead>
      <tr>
          <th style="text-align: center">Tens</th>
          <th>Units</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td style="text-align: center">7</td>
          <td>0</td>
      </tr>
      <tr>
          <td style="text-align: center">0</td>
          <td>7</td>
      </tr>
  </tbody>
</table>
<p>And on top of this order, there&rsquo;s a mathematical formula that converts a number from any base to decimal. To make it easier:</p>
<ul>
<li><strong>Digit</strong>: Symbol that represents a unique quantity.
<em>Ex.: 1, 2, 3, 4, 5, 6, 7, 8, 9, 0</em></li>
<li><strong>Number</strong>: Digits organized in order of <em><strong>position</strong></em>.
<em>Ex.: 10, 144, 999, 42, 37, <code>0x8A6C</code>, <code>0b101010</code></em></li>
<li><strong>Base</strong>: Maximum quantity of digits that can be represented.
<em>Ex.: binary (<code>0b</code>) 2, decimal 10, hexadecimal (<code>0x</code>)</em></li>
<li><strong>Index</strong>: The magnitude of the digit in the position.
_Ex.: 10, the 1 in 10 has <em>Index 1</em> and the 0 has <em>Index 0</em> (it&rsquo;s a bit counterintuitive, but it makes sense).
In other words, the index is the position from right to left, starting at 0.</li>
</ul>
<p>The formula is quite simple: <em><strong>(d)igit × (b)ase<sup>(i)ndex</sup></strong></em>. In the example we&rsquo;re doing, this is the formula to convert 70 to decimal&hellip; to 70 in decimal. <strong>7 × 10<sup>1</sup> + 0 × 10<sup>0</sup> = 70</strong></p>
<p><em>Interactive binary counter widget (base 10, 3 digits) &mdash; <a href="https://jeffersonmourak.com/blog/the-binary-en/">view it on the blog</a>.</em></p>

<p>When we talk about numbers this way, it becomes easier to represent binary, which is nothing more than the quantity that can be represented by the formula <em><strong>d × 2<sup>i</sup></strong></em>.</p>
<p><em>Interactive binary counter widget (base 2, 6 digits) &mdash; <a href="https://jeffersonmourak.com/blog/the-binary-en/">view it on the blog</a>.</em></p>

<p>And this way of representing quantities can also be manipulated with mathematical operations. The addition 25 + 17 is solved in a way similar to this.</p>
<pre tabindex="0"><code>1
2 5
1 7
--- +
4 2
</code></pre><p>In the example above, we have some elements that need to be highlighted:</p>
<ol>
<li>A <em>digit</em> can only be added to another <em>digit</em> that is in the same <em>index</em>.</li>
<li>When the sum cannot be represented with just one digit, the smallest index is kept as the <em>result</em>, and the <em>remainder</em> is sent to the next index.</li>
<li>The <em>result</em> of a sum will always be the sum of the <em><strong>digits</strong></em> of the <em><strong>index</strong></em> + the <em><strong>remainder</strong></em> that came from the <em><strong>digit</strong></em> in the previous <em><strong>index</strong></em>.</li>
</ol>
<p>With these same steps, we can apply addition in any base.</p>
<pre tabindex="0"><code>    1 1 1       |  1      |  
1 0 0 0 1 1     |  2 5    |  1 9
0 0 0 1 1 1     |  1 7    |  1 1
------------ +  |  --- +  |  --- +
1 0 1 0 1 0     |  4 2    |  2 A 
</code></pre><figure>
  <img src="./hand.jpeg" alt="Calculation of 25 &#43; 17 in Binary, Decimal and Hexadecimal bases" loading="lazy">
  <figcaption>Calculation of 25 + 17 in Binary, Decimal and Hexadecimal bases</figcaption>
</figure><p><em>Interactive binary counter widget (base 2, 6 digits) &mdash; <a href="https://jeffersonmourak.com/blog/the-binary-en/">view it on the blog</a>.</em></p>

<p>Now that we already know each other, how can we make the computer know us too?</p>
<h2 id="playing-with-translation">Playing with translation</h2>
<p>Remembering the <a href="https://jeffersonmourak.com/blog/logic-gates/">previous article</a>, let&rsquo;s isolate the addition of two digits in tables. First, let&rsquo;s look at just the result. We have this table that very much resembles the OR gate table, except for this small change at the end.</p>
<div style="display: flex; gap: 16px;">
<span>
<table>
<tr><td colspan="3">Result</td></tr>
<tr>
 <td>?</td>
 <td>0</td>
 <td>1</td>
</tr>
<tr>
 <td>0</td>
 <td>0</td>
 <td>1</td>
</tr>
<tr>
 <td>1</td>
 <td>1</td>
 <td>0</td>
</tr>
</table>
</span>
<span>
<table>
<tr><td colspan="3">OR</td></tr>
<tr>
 <td>∨</td>
 <td>0</td>
 <td>1</td>
</tr>
<tr>
 <td>0</td>
 <td>0</td>
 <td>1</td>
</tr>
<tr>
 <td>1</td>
 <td>1</td>
 <td>1</td>
</tr>
</table>
</span>
</div>
<p>For this, we can combine the results of the gates we saw earlier into a single circuit called &ldquo;Exclusive OR&rdquo; or &ldquo;XOR&rdquo;.</p>
<p><em>Interactive circ widget &mdash; <a href="https://jeffersonmourak.com/blog/the-binary-en/">view it on the blog</a>.</em></p>
<pre><code>╭───╮     ╭─────────────╮     ╭───╮     ╭───╮
│ a ├○●●─▶┤             │╭───▶┤   │ ╭──▶┤LED│
╰───╯ ││  │[nand:_nand_]├○╮   │AND├○╯   ╰───╯
      │●─▶┤             ││╰──▶┤   │          
      ││  ╰─────────────╯│    ╰───╯          
      ││                 │                   
╭───╮ ││  ╭─────────╮    │                   
│ b ├○●╰─▶┤         │    │                   
╰───╯ │   │[or:_or_]├○───╯                   
      ╰──▶┤         │                        
          ╰─────────╯                        </code></pre>
<p>Now, let&rsquo;s also look at the remainder table of our addition and you&rsquo;ll notice that it&rsquo;s an exact copy of the AND gate.</p>
<div style="display: flex; gap: 16px;">
<span>
<table>
<tr><td colspan="3">Remainder</td></tr>
<tr>
 <td>?</td>
 <td>0</td>
 <td>1</td>
</tr>
<tr>
 <td>0</td>
 <td>0</td>
 <td>0</td>
</tr>
<tr>
 <td>1</td>
 <td>0</td>
 <td>1</td>
</tr>
</table>
</span>
<span>
<table>
<tr><td colspan="3">AND</td></tr>
<tr>
 <td>∧</td>
 <td>0</td>
 <td>1</td>
</tr>
<tr>
 <td>0</td>
 <td>0</td>
 <td>0</td>
</tr>
<tr>
 <td>1</td>
 <td>0</td>
 <td>1</td>
</tr>
</table>
</span>
</div>
<p>And just like Captain Planet, &ldquo;By the union of their powers&rdquo;, we will be able to perform the operation of adding two digits in binary, and this component is called &ldquo;Adder&rdquo;.</p>
<p><em>Interactive circ widget &mdash; <a href="https://jeffersonmourak.com/blog/the-binary-en/">view it on the blog</a>.</em></p>
<pre><code>╭───╮        ╭───╮               ╭───╮               ╭─────────╮     ╭───╮
│ a ├○●●────▶┤   │           ╭──▶┤   │ ╭────────────▶┤         │ ╭──▶┤LED│
╰───╯ ││     │AND├○╮         │   │AND├○╯             │[or:_or_]├○╯   ╰───╯
      │●────▶┤   │ │         │ ╭▶┤   │             ╭▶┤         │          
      ││     ╰───╯ │         │ │ ╰───╯             │ ╰─────────╯          
      ││           ╰─────────┼─┼───────────────────╯                      
╭───╮ ││     ╭─────────────╮ │ │ ╭─────────────╮                     ╭───╮
│ b ├○●╰────▶┤             │ ●─┼▶┤             │ ╭──────────────────▶┤LED│
╰───╯ │      │[xor:_xor_1_]├○● │ │[xor:_xor_2_]├○╯                   ╰───╯
      ╰─────▶┤             │   ●▶┤             │                          
             ╰─────────────╯   │ ╰─────────────╯                          
                               │                                          
╭──────╮                       │                                          
│ c_in ├○──────────────────────●                                          
╰──────╯                                                                  </code></pre>
<p>With this combination of logic gates, a computer can already perform the incredible additions of: <code>0 + 0</code>, <code>1 + 0</code>, <code>0 + 1</code>, <code>1 + 1</code>, but beyond that, it can also say how much remainder there was from the addition. And when several ADDERs are combined, we can perform addition of more complex numbers like 42, but that we&rsquo;ll see in the next article.</p>
<p>That&rsquo;s all for today, folks.
<img src="https://media0.giphy.com/media/v1.Y2lkPTc5MGI3NjExaXg4ZHNiMjVkMG1nY3M3NjRhaDI5cmFoNWtmaHVxNDBweGV5ZjdwOCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/xUPOqo6E1XvWXwlCyQ/giphy.gif" alt="That&rsquo;s all folks!" loading="lazy"></p>
<h2 id="references">References</h2>
<ul>
<li><a href="https://pt.wikipedia.org/wiki/%C3%81lgebra_booliana">A brief history of numerical systems (Youtube)</a></li>
<li><a href="https://en.wikipedia.org/wiki/Numeral_system">Numeral system (Wikipédia)</a></li>
<li><a href="https://pt.wikipedia.org/wiki/Sistema_de_numera%C3%A7%C3%A3o">Sistema de numeração (Wikipédia)</a></li>
<li><a href="https://www.youtube.com/watch?v=QZwneRb-zqA">Exploring How Computers Work (YouTube)</a></li>
<li><a href="https://www.youtube.com/watch?v=sTu3LwpF6XI">Making logic gates from transistors (YouTube)</a></li>
<li><a href="https://www.youtube.com/watch?v=HjneAhCy2N4">HOW TRANSISTORS RUN CODE? (YouTube)</a></li>
<li><a href="https://www.nand2tetris.org/">From Nand to Tetris</a></li>
</ul>
]]></content:encoded></item><item><title>Logic Gates</title><link>https://jeffersonmourak.com/blog/logic-gates-en/</link><pubDate>Thu, 07 Nov 2024 08:27:04 -0200</pubDate><guid>https://jeffersonmourak.com/blog/logic-gates-en/</guid><category domain="https://jeffersonmourak.com/tags/cpu/">CPU</category><category domain="https://jeffersonmourak.com/tags/logic-gates/">Logic Gates</category><category>Computing</category><description>&lt;blockquote>
&lt;p>Hey Y&amp;rsquo;all! This is a translation of my blog post originally written in Portuguese.&lt;/p>&lt;/blockquote>
&lt;blockquote>
&lt;p>Revised [11/12/2024]&lt;/p>
&lt;p>Hi folks, so I had to make some changes here in the post to add some interactive widgets,
and I took the opportunity to give a bit more context to the content.&lt;/p>&lt;/blockquote>
&lt;p>Hello, this is the first article in a series about the fundamentals of computing. The idea is to demonstrate in a simple and practical way how this device transforms zeros and ones into practically anything, the functioning of CPUs and other interesting subjects. So, to start, the first concept we have to learn is that of logic gates: they are the foundation of all computing, with them we can do certain operations that receive as input one or more values and will have another value as a result.&lt;/p></description><content:encoded><![CDATA[<blockquote>
<p>Hey Y&rsquo;all! This is a translation of my blog post originally written in Portuguese.</p></blockquote>
<blockquote>
<p>Revised [11/12/2024]</p>
<p>Hi folks, so I had to make some changes here in the post to add some interactive widgets,
and I took the opportunity to give a bit more context to the content.</p></blockquote>
<p>Hello, this is the first article in a series about the fundamentals of computing. The idea is to demonstrate in a simple and practical way how this device transforms zeros and ones into practically anything, the functioning of CPUs and other interesting subjects. So, to start, the first concept we have to learn is that of logic gates: they are the foundation of all computing, with them we can do certain operations that receive as input one or more values and will have another value as a result.</p>
<p>But what are these values? The number 42? My mother&rsquo;s name? These &ldquo;values&rdquo; are energy, more specifically the presence and absence of it. As mentioned in the introduction, computers work using binary digits, meaning these machines only understand 0 and 1 or &ldquo;Has energy&rdquo; and &ldquo;No energy&rdquo;. However, we won&rsquo;t call these zeros and ones energy, for the computer they are signals.</p>
<p>When signals are combined and/or compared, we call it a logical operation, for example.</p>
<p>&ldquo;[Harp sounds of story beginning]&rdquo;</p>
<p>One day you hire an electrical company to install two switches in a hallway of your house, but due to a design error, the installation ends up like this: the wire passes through the first switch (let&rsquo;s call it switch A), goes straight to the second switch (B for intimates) and then exits to the lamp.</p>
<figure>
  <img src="./CPUImageFrame1.png" alt="" loading="lazy">
</figure><p>Did you notice that the lamp only lights up if both switches are on at the same time? If either one is turned off, the light goes out immediately! Well, this residential electrical atrocity can be explained, in mathematics, from one of its branches called boolean algebra, and in it we can demonstrate this with a table:</p>
<table>
  <thead>
      <tr>
          <th style="text-align: center">∧</th>
          <th style="text-align: center">0</th>
          <th style="text-align: center">1</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td style="text-align: center">0</td>
          <td style="text-align: center">0</td>
          <td style="text-align: center">0</td>
      </tr>
      <tr>
          <td style="text-align: center">1</td>
          <td style="text-align: center">0</td>
          <td style="text-align: center">1</td>
      </tr>
  </tbody>
</table>
<p>Writing this another way looks like this:</p>
<table>
  <thead>
      <tr>
          <th style="text-align: left">∧</th>
          <th style="text-align: left">A off</th>
          <th style="text-align: left">A on</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td style="text-align: left">B off</td>
          <td style="text-align: left">Light off</td>
          <td style="text-align: left">Light off</td>
      </tr>
      <tr>
          <td style="text-align: left">B on</td>
          <td style="text-align: left">Light off</td>
          <td style="text-align: left">Light on</td>
      </tr>
  </tbody>
</table>
<p>If you noticed well, now we have a way to represent what we want to happen with two switches and a lamp, and this example shown above is the first logic gate we&rsquo;re getting to know, the AND or Conjunction, and this table, demonstrated above in different forms, is called a Truth Table.</p>
<p>Ok, but what about my house light? How is it going to be? I need the light to turn on when A OR B is on. How do you do that?</p>
<p>The answer is in the question itself! 😉 We&rsquo;ll need another logic gate, the OR or Disjunction, and to understand how it works, let&rsquo;s use a truth table that fits these conditions:</p>
<table>
  <thead>
      <tr>
          <th style="text-align: center">?</th>
          <th style="text-align: center">0</th>
          <th style="text-align: center">1</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td style="text-align: center">0</td>
          <td style="text-align: center">0</td>
          <td style="text-align: center">1</td>
      </tr>
      <tr>
          <td style="text-align: center">1</td>
          <td style="text-align: center">1</td>
          <td style="text-align: center">1</td>
      </tr>
  </tbody>
</table>
<p>But before talking about OR, let&rsquo;s take a look at the NOT gate (or Negation). It&rsquo;s quite simple, but it will be important later on.</p>
<p><em>Interactive circ widget &mdash; <a href="https://jeffersonmourak.com/blog/logic-gates-en/">view it on the blog</a>.</em></p>
<pre><code>╭───╮     ╭───╮     ╭───╮
│ a ├○───▶┤NOT├○───▶┤LED│
╰───╯     ╰───╯     ╰───╯</code></pre>
<p>And if we write it in a truth table we&rsquo;ll have this here:</p>
<table>
  <thead>
      <tr>
          <th style="text-align: center">¬</th>
          <th style="text-align: center">0</th>
          <th style="text-align: center">1</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td style="text-align: center"></td>
          <td style="text-align: center">1</td>
          <td style="text-align: center">0</td>
      </tr>
  </tbody>
</table>
<p>So, let&rsquo;s take a break and review what we already know. First, the operations and how to represent them in a table. To simplify, let&rsquo;s transform these operations into symbols. Every time we refer to AND, this will be the symbol:</p>
<p><em>Interactive circ widget &mdash; <a href="https://jeffersonmourak.com/blog/logic-gates-en/">view it on the blog</a>.</em></p>
<pre><code>╭───╮     ╭───╮     ╭───╮
│ a ├○───▶┤   │ ╭──▶┤LED│
╰───╯     │AND├○╯   ╰───╯
       ╭─▶┤   │          
       │  ╰───╯          
       │                 
╭───╮  │                 
│ b ├○─╯                 
╰───╯                    </code></pre>
<p>and the NOT:</p>
<p><em>Interactive circ widget &mdash; <a href="https://jeffersonmourak.com/blog/logic-gates-en/">view it on the blog</a>.</em></p>
<pre><code>╭───╮     ╭───╮     ╭───╮
│ a ├○───▶┤NOT├○───▶┤LED│
╰───╯     ╰───╯     ╰───╯</code></pre>
<p>With these two logical operations (AND and NOT), we can already combine their results and create a third logic gate: the NAND (or Not AND).</p>
<p>It can be represented like this:</p>
<p><em>Interactive circ widget &mdash; <a href="https://jeffersonmourak.com/blog/logic-gates-en/">view it on the blog</a>.</em></p>
<pre><code>╭───╮     ╭─────────────╮     ╭───╮
│ a ├○───▶┤             │ ╭──▶┤LED│
╰───╯     │[nand:_nand_]├○╯   ╰───╯
       ╭─▶┤             │          
       │  ╰─────────────╯          
       │                           
╭───╮  │                           
│ b ├○─╯                           
╰───╯                              </code></pre>
<p><em>Interactive circ widget &mdash; <a href="https://jeffersonmourak.com/blog/logic-gates-en/">view it on the blog</a>.</em></p>
<pre><code>╭───╮     ╭───╮     ╭───╮     ╭───╮
│ a ├○───▶┤   │ ╭──▶┤NOT├○───▶┤LED│
╰───╯     │AND├○╯   ╰───╯     ╰───╯
       ╭─▶┤   │                    
       │  ╰───╯                    
       │                           
╭───╮  │                           
│ b ├○─╯                           
╰───╯                              </code></pre>
<p>Its truth table is identical to AND&rsquo;s, but with the results inverted.</p>
<table>
  <thead>
      <tr>
          <th style="text-align: center">¬∧</th>
          <th style="text-align: center">0</th>
          <th style="text-align: center">1</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td style="text-align: center">0</td>
          <td style="text-align: center">1</td>
          <td style="text-align: center">1</td>
      </tr>
      <tr>
          <td style="text-align: center">1</td>
          <td style="text-align: center">1</td>
          <td style="text-align: center">0</td>
      </tr>
  </tbody>
</table>
<p>Following this same logic of combining gates, we can use a NAND gate and invert each switch input with a NOT. Thus, we&rsquo;ll have the following:</p>
<p><em>Interactive circ widget &mdash; <a href="https://jeffersonmourak.com/blog/logic-gates-en/">view it on the blog</a>.</em></p>
<pre><code>╭───╮     ╭───╮     ╭─────────────╮     ╭───╮
│ a ├○───▶┤NOT├○───▶┤             │ ╭──▶┤LED│
╰───╯     ╰───╯     │[nand:_nand_]├○╯   ╰───╯
                 ╭─▶┤             │          
                 │  ╰─────────────╯          
                 │                           
╭───╮     ╭───╮  │                           
│ b ├○───▶┤NOT├○─╯                           
╰───╯     ╰───╯                              </code></pre>
<p>Analyzing the diagram above, we see that when both switches are off, both signals will be inverted by the NOT, and the NAND will result in ¬∧(1, 1) = 0. So with both off, the lamp turns off. But what happens when we have ¬∧(1, 0) or ¬∧(0, 1)? To find out, let&rsquo;s analyze the truth table:</p>
<table>
  <thead>
      <tr>
          <th style="text-align: center">∨</th>
          <th style="text-align: center">0</th>
          <th style="text-align: center">1</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td style="text-align: center">0</td>
          <td style="text-align: center">0</td>
          <td style="text-align: center">1</td>
      </tr>
      <tr>
          <td style="text-align: center">1</td>
          <td style="text-align: center">1</td>
          <td style="text-align: center">1</td>
      </tr>
  </tbody>
</table>
<figure>
  <img src="https://media1.giphy.com/media/v1.Y2lkPTc5MGI3NjExa2F3aTZ5Z2FhajUxem51aG5oYzBwYW1jdHI3NGswMzZzd3duMTh3biZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/n9h61thJkq6Xe/giphy.gif" alt="I tawt I taw a puddy tat!" loading="lazy">
  <figcaption>I tawt I taw a puddy tat!</figcaption>
</figure><p>But wait a minute, I have the impression that I&rsquo;ve seen this table before? Yes! This is the OR logic gate we were looking for. Quite a journey, isn&rsquo;t it? Now that we know how to fix the lamp wiring, we can take a break here. This is just the first article in a series about computers. Later on, we&rsquo;ll address other logic gates and other computing concepts.</p>
<h2 id="references">References</h2>
<ul>
<li><a href="https://pt.wikipedia.org/wiki/%C3%81lgebra_booliana">Boolean algebra (Wikipedia)</a></li>
<li><a href="https://en.wikipedia.org/wiki/Boolean_algebra">Boolean Algebra (Wikipedia)</a></li>
<li><a href="https://pt.wikipedia.org/wiki/Tabela-verdade">Truth table (Wikipedia)</a></li>
<li><a href="https://www.youtube.com/watch?v=QZwneRb-zqA">Exploring How Computers Work (YouTube)</a></li>
<li><a href="https://www.youtube.com/watch?v=sTu3LwpF6XI">Making logic gates from transistors (YouTube)</a></li>
<li><a href="https://www.youtube.com/watch?v=HjneAhCy2N4">HOW TRANSISTORS RUN CODE? (YouTube)</a></li>
</ul>
]]></content:encoded></item></channel></rss>