<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shynn Lawrence on Medium]]></title>
        <description><![CDATA[Stories by Shynn Lawrence on Medium]]></description>
        <link>https://medium.com/@shynnlawrence?source=rss-434381df4d4c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Shynn Lawrence on Medium</title>
            <link>https://medium.com/@shynnlawrence?source=rss-434381df4d4c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 18:58:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shynnlawrence/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Binary addition with Logic Gates]]></title>
            <link>https://shynnlawrence.medium.com/binary-addition-with-logic-gates-72b8a032a93e?source=rss-434381df4d4c------2</link>
            <guid isPermaLink="false">https://medium.com/p/72b8a032a93e</guid>
            <dc:creator><![CDATA[Shynn Lawrence]]></dc:creator>
            <pubDate>Sat, 05 Dec 2020 23:07:43 GMT</pubDate>
            <atom:updated>2020-12-05T23:27:10.403Z</atom:updated>
            <content:encoded><![CDATA[<p>Logic Gates and Addition</p><p>Logic gates are an interesting concept. Through logic gates, we can convert differing currents, or binary inputs, into outputs. Higher-level programmers will recognize this as “functions” or “methods”, and circuits are essentially a hardware form of that. Taking in generally 2 binary inputs, a logic gate can convert the inputs into outputs. To represent what is outputted when given certain inputs, something called a truth table can be made:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zpEqkIoJksm9yrWV" /><figcaption>Truth Tables for the 4 most common Boolean Operations</figcaption></figure><p>These boolean operations are found everywhere in programming languages; For example, we could print out a number if it is divisible by 2, and then print it one more time if it is divisible by 3:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c9b596ffaab355d81521ae92349edfa4/href">https://medium.com/media/c9b596ffaab355d81521ae92349edfa4/href</a></iframe><p>But what if we wanted to only print the number if its divisible by 2 <strong>and</strong> by 3? This is where boolean logic comes in:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5dbdc4c22c3a6bbbefa2ef3acc28ee01/href">https://medium.com/media/5dbdc4c22c3a6bbbefa2ef3acc28ee01/href</a></iframe><p>Using the <strong>and </strong>(&amp;, or &amp;&amp; in other languages) keyword, we can only execute the code block if both condition 1 <strong>and</strong> condition 2 are satisfied. z is divisible by 2 is <em>True</em>, z is divisible by 3 is <em>True</em>, so <em>True</em> <strong>and</strong> <em>True</em> is <em>True</em>, and the block executes. Two <em>True</em>s makes a <em>True (for the and operation)</em>, anything else is<em> False</em>. While the python example, and real-life analogies, are high level examples, low level boolean logic works the same way. A strong foundation in this, and basic binary, is required to understand the next steps: Implementing a basic circuit that proves this.</p><p>While physically making circuits may be a rewarding experience, it can be expensive and get risky; As such, I will be displaying the circuits as created on <a href="https://circuitverse.org/">CircuitVerse</a>, an <a href="https://github.com/CircuitVerse/">open source</a> Circuit Simulator. To start off with CircuitVerse, start a project and open up the simulator. You’ll end up somewhere similar to this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5UuSO6ZGH6qpimpMNMTJuw.png" /></figure><p>To give in an input, open up the input bar and drag on an “input” switch onto the grid. Note: Clicking the “switch” changes the current from 0 to 1, something we’ll take advantage of when testing our gates.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m31iDXmmq2-Bxfc3bkBj8w.png" /><figcaption>An input switch</figcaption></figure><p>We can also direct it straight to an output box by placing one on the grid, and dragging a wire between the two green dots on both input and output boxes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AAve9p4Q1RuDEg2gwX8UqA.png" /></figure><p>If all goes well, the output should display the same number as the input. Now to actually using the gates, we can open up the gates menu and place in whichever gate we want:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ub4hH6mt4uo67FKnS3WzJg.png" /><figcaption>The gates dropdown menu (closed in this image) is in the circuit elements menu</figcaption></figure><p>After playing around a bit, we can start onto more complex things. One of such things is the creation of a custom made <strong>nand </strong>gate, which follows the truth table:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/474/0*ORU7ASRwwKKWwUB1" /><figcaption>The <strong>nand</strong> truth table</figcaption></figure><p>At first glance, this may seem complex. However looking thoroughly, and remembering the fact that all complex logic gates rise from the simpler ones, we can recognize that this is just a reversed<strong> and</strong> gate, meaning that the outputs of a normal <strong>and </strong>gate will simply be reversed. To construct this in our digital circuit simulator, we can first set up the <strong>and</strong> gate and simply place a <strong>not</strong> gate at the end:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/748/1*1OGE3z0PW9Ju9-FELbKn_Q.png" /><figcaption>A simple Nand gate</figcaption></figure><p>If we try it for all the inputs:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*yXgfxFOCMapop3mAp0h3gQ.png" /></figure><p>We can see that it fits the <strong>Nand</strong> Truth Table perfectly. With this knowledge, what can we do? One interesting “function” we can create is a logical calculator. Because gates can only take in inputs of 1s and 0s, such a task is more difficult than it seems. The first way to understand bit addition is by creating a truth table for addition. Bit addition is not complex by itself, as it is just normal addition with a twist; Here is the truth table for bit addition:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*Af9QDf-2ThLZDAbd" /></figure><p>2 inputs will result in 2 outputs, a result and the carry (which can be used for larger addition problems). To create our boolean circuit, we first have to distinguish between the result and carry, as they are separate outputs. First, begin with the result; The result truth table looks eerily like the <strong>Xor</strong> truth table, which is a very important distinction:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/394/0*h1i8VgTXIVRc3YpH" /><figcaption>The exclusive or (<strong>xor</strong>) truth table, for reference</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/588/1*IwL7g8MfEASM8kH5lO4a1A.png" /><figcaption>Basic <strong>xor</strong> implemented in <a href="https://circuitverse.org/">circuitverse</a></figcaption></figure><p>After recognizing this fact, we can begin implementing this into our circuit and check the truth table if it gives the proper result (under the result column).</p><p>Note that both inputs are being added with each other, so 0 + 0 -&gt; 0, 1+0-&gt; 1, and 0+1 -&gt; 1. However in the case of 1+1, we reach a problem. 1+1 in binary addition -&gt; 10, but we only have one output. To remedy this, we can create another output for the extra carry. However we only need this carry output if the input is 1 <strong>and</strong> 1; Keyword is <strong>and</strong>, which is the exact gate we’ll use to establish this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*BKT3-nfi0VF7VEZdXnOivQ.png" /><figcaption>Begin by creating a new output and hooking up an <strong>and</strong> gate to it</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UIgpBgSSQXzJbuCvlkt0mg.png" /><figcaption>With some reorganization, we quickly get at our complete 1-bit adder</figcaption></figure><p>After some testing, we can see that it follows the 1-bit addition table from above.</p><p>To make this more complex, we’ll walk through creating a 2-bit adder, which just uses the one-bit adder with a few more additions. A simple 2 bit adder takes in 4 inputs, a 2-bit top addend and a 2-bit bottom addend. It applies the same operation as the one-bit addition circuit except 3 times (2 for the actual addition, and one extra for the carry). First, let’s setup the workspace, which requires 4 inputs and 3 outputs (The output can be larger than 11, so we need a 3rd bit). We will extend the inputs and outputs as needed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MjbMHgTr50vvrBSbAl4cyQ.png" /><figcaption>Using the rotate operation (arrow keys), we can rotate anything we place onto the grid</figcaption></figure><p>Like mentioned before, we simply need to do the same addition problem, except three times (twice for each bit and an extra time for the carry of the 1st addition), and with a few modifications. While it is difficult to understand what we’ll be doing, I recommend doing a few 2 bit problems and creating a simple truth table for it. First step we need to do is add the right-most bits, which we can do easily with our simple adder:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aeMYQdrdYd-sjJZ4S7Z0ew.png" /><figcaption>The right-most addition results in our right-most digit for our result</figcaption></figure><p>After implementing this circuit, we just have to replicate it again for the left-most digits in our addends. We will be using the carry for the 2nd bit in the sum soon enough. Next, hook up the addition of the left-most bits, but don’t assign them to the results (we still need to deal with the carry)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gucFQmVs8YXEEbS2fOXZ-w.png" /><figcaption>2nd addition</figcaption></figure><p>The second to last step is to run through one more addition operation, adding carry to result2:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WyzVkYO3ft_KF_cSZvrCMw.png" /><figcaption>2/3 bits finished, last one!</figcaption></figure><p>The last bit (and the last step) is fairly simple to arrive at: Its simply the logical<strong> </strong>carry2 <strong>or</strong> carry3:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*56yjTtw11AzUg3FbWULNGA.png" /><figcaption>Voilà! A 2 bit calculator.</figcaption></figure><p>After testing the calculator, we can see that it, indeed works. The captions are the binary form of the calculator’s input and outputs in decimal:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SygiPXvfikPQbNJp39V04w.png" /><figcaption>0+0=0</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7MFBsB5sX4Y_lsS3SsG4bA.png" /><figcaption>1+1=2</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZI_cgMyh5GQYqmCEkfBG0Q.png" /><figcaption>1+3 = 4</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2qKXUURy9n5h18q_CQUuZA.png" /><figcaption>3+0 = 3</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jDoeoGyM3CuEPxpbVFQAtQ.png" /><figcaption>3+3 = 6</figcaption></figure><p>You’ll notice that, even when we use all the bits, we only arrive at 110. To get the complete 111, we’d need to modify our calculator to take in a carry as well, which is the only possibility of getting 111 in these circumstances.</p><p>That was a lot! I commend you for even scrolling this far down, and I hope you learned something!</p><p>To learn more, visit:</p><ul><li><a href="https://en.wikipedia.org/wiki/Boolean_algebra">Boolean Algebra</a></li><li><a href="https://en.wikipedia.org/wiki/George_Boole">George Boole</a></li><li><a href="https://en.wikipedia.org/wiki/Logic_gate">Logic Gates</a></li><li><a href="https://en.wikipedia.org/wiki/De_Morgan%27s_laws">De Morgan’s Laws</a> and <a href="https://en.wikipedia.org/wiki/Karnaugh_map">Karnaugh Map</a> (boolean logic simplification)</li><li><a href="https://en.wikipedia.org/wiki/Opcode">Computer Opcodes</a> (Base instructions, in hexadecimal, to the CPU to do some calculations)</li></ul><p>Visit my <a href="https://github.com/shua1090">Github</a> for higher-level programming projects, and issue pull requests for anything you want to add to any projects!</p><p>-Shynn Lawrence</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=72b8a032a93e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>