<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rohit Dhatrak</title>
    <description>The latest articles on DEV Community by Rohit Dhatrak (@rohitdhatrak).</description>
    <link>https://dev.to/rohitdhatrak</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F382774%2F8165d573-f30a-4200-9ff3-8f1cb0c83804.jpg</url>
      <title>DEV Community: Rohit Dhatrak</title>
      <link>https://dev.to/rohitdhatrak</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rohitdhatrak"/>
    <language>en</language>
    <item>
      <title>Building your own Arithmetic Logic Unit (ALU)</title>
      <dc:creator>Rohit Dhatrak</dc:creator>
      <pubDate>Sat, 01 Mar 2025 10:55:15 +0000</pubDate>
      <link>https://dev.to/rohitdhatrak/building-your-own-alu-5b2j</link>
      <guid>https://dev.to/rohitdhatrak/building-your-own-alu-5b2j</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this post, we will explore fundamental questions about a computer like why do computers use 1s and 0s? What are the building blocks of a computer? How does a computer perform arithmetic operations? We will do this by constructing our own version of an Arithmetic Logic Unit (ALU) from scratch.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why does a computer use 1s and 0s?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Binary Number System
&lt;/h3&gt;

&lt;p&gt;Computers use the binary number system (aka base 2 number system). This system consists of only two unique digits: 0 and 1. Thus, all information in a computer is represented using combinations of these two digits. But why just two digits? The answer lies in the nature of the electric circuits that make up a computer. These circuits have only two states: on, which is represented by a 1, and off, which is represented by a 0.&lt;/p&gt;

&lt;p&gt;The code snippet below shows how we represent decimal numbers in the binary number system. Notice that with each additional digit, the number of possible combinations doubles. With n digits, the total number of unique numbers that can be represented is &lt;code&gt;2^n&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// 1 digit: 2 possible combinations&lt;/span&gt;
&lt;span class="mi"&gt;0&lt;/span&gt;  &lt;span class="c1"&gt;// zero&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt;  &lt;span class="c1"&gt;// one&lt;/span&gt;

&lt;span class="c1"&gt;// 2 digits: 4 possible combinations&lt;/span&gt;
&lt;span class="mi"&gt;00&lt;/span&gt; &lt;span class="c1"&gt;// zero&lt;/span&gt;
&lt;span class="mi"&gt;01&lt;/span&gt; &lt;span class="c1"&gt;// one&lt;/span&gt;
&lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="c1"&gt;// two&lt;/span&gt;
&lt;span class="mi"&gt;11&lt;/span&gt; &lt;span class="c1"&gt;// three&lt;/span&gt;

&lt;span class="c1"&gt;// 3 digits: 8 possible combinations&lt;/span&gt;
&lt;span class="mi"&gt;000&lt;/span&gt; &lt;span class="c1"&gt;// zero&lt;/span&gt;
&lt;span class="mi"&gt;001&lt;/span&gt; &lt;span class="c1"&gt;// one&lt;/span&gt;
&lt;span class="mi"&gt;010&lt;/span&gt; &lt;span class="c1"&gt;// two&lt;/span&gt;
&lt;span class="mi"&gt;011&lt;/span&gt; &lt;span class="c1"&gt;// three&lt;/span&gt;
&lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="c1"&gt;// four&lt;/span&gt;
&lt;span class="mi"&gt;101&lt;/span&gt; &lt;span class="c1"&gt;// five&lt;/span&gt;
&lt;span class="mi"&gt;110&lt;/span&gt; &lt;span class="c1"&gt;// six&lt;/span&gt;
&lt;span class="mi"&gt;111&lt;/span&gt; &lt;span class="c1"&gt;// seven&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to learn more about the binary number system you can watch &lt;a href="https://youtu.be/sXxwr66Y79Y" rel="noopener noreferrer"&gt;this&lt;/a&gt; video by Khan Academy or read more about it &lt;a href="https://www.mathsisfun.com/binary-number-system.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Two’s Complement
&lt;/h3&gt;

&lt;p&gt;But how do we represent negative numbers using the binary number system? The two’s complement method is the most commonly used method (also known as radix complement). In this method half of the combinations are used to represent negative numbers.&lt;/p&gt;

&lt;p&gt;For example, in a 4-bit binary number system, -5 is represented by calculating &lt;code&gt;2^4 - 5&lt;/code&gt;, which equals 11. In binary, 11 is represented as 1011. To verify, recall that +5 is represented as 0101 in binary. Adding &lt;code&gt;1011&lt;/code&gt; and &lt;code&gt;0101&lt;/code&gt; yields &lt;code&gt;10000&lt;/code&gt;. Ignoring the overflow bit, we get &lt;code&gt;0000&lt;/code&gt;, confirming that the representation is correct.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="mi"&gt;0000&lt;/span&gt; &lt;span class="c1"&gt;// 0&lt;/span&gt;
&lt;span class="mi"&gt;0001&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;span class="mi"&gt;0010&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;span class="mi"&gt;0011&lt;/span&gt; &lt;span class="c1"&gt;// 3&lt;/span&gt;
&lt;span class="mi"&gt;0100&lt;/span&gt; &lt;span class="c1"&gt;// 4&lt;/span&gt;
&lt;span class="mi"&gt;0101&lt;/span&gt; &lt;span class="c1"&gt;// 5&lt;/span&gt;
&lt;span class="mi"&gt;0110&lt;/span&gt; &lt;span class="c1"&gt;// 6&lt;/span&gt;
&lt;span class="mi"&gt;0111&lt;/span&gt; &lt;span class="c1"&gt;// 7&lt;/span&gt;
&lt;span class="mi"&gt;1000&lt;/span&gt; &lt;span class="c1"&gt;// -8&lt;/span&gt;
&lt;span class="mi"&gt;1001&lt;/span&gt; &lt;span class="c1"&gt;// -7&lt;/span&gt;
&lt;span class="mi"&gt;1010&lt;/span&gt; &lt;span class="c1"&gt;// -6&lt;/span&gt;
&lt;span class="mi"&gt;1011&lt;/span&gt; &lt;span class="c1"&gt;// -5&lt;/span&gt;
&lt;span class="mi"&gt;1100&lt;/span&gt; &lt;span class="c1"&gt;// -4&lt;/span&gt;
&lt;span class="mi"&gt;1101&lt;/span&gt; &lt;span class="c1"&gt;// -3&lt;/span&gt;
&lt;span class="mi"&gt;1110&lt;/span&gt; &lt;span class="c1"&gt;// -2&lt;/span&gt;
&lt;span class="mi"&gt;1111&lt;/span&gt; &lt;span class="c1"&gt;// -1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code snippet above shows all the possible combinations of signed numbers in a 4-bit binary system.&lt;/p&gt;

&lt;p&gt;We can observe the following properties of the two’s complement number system:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;All positive numbers begin with 0, and all negative numbers begin with 1. Thus, the most significant bit (MSB) indicates the sign of the number.&lt;/li&gt;
&lt;li&gt;In an n-bit system, the total number of possible combinations is  &lt;code&gt;2^n&lt;/code&gt;. Since half of the range is used to represent negative numbers, we can represent numbers ranging from &lt;code&gt;-2^(n-1)&lt;/code&gt; to &lt;code&gt;2^(n-1) - 1&lt;/code&gt; (-1 because we have zero as well)&lt;/li&gt;
&lt;li&gt;To get the binary representation of -x from the binary representation of x, flip all the bits of x and then add 1 to the result.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Lego blocks of a computer
&lt;/h2&gt;

&lt;p&gt;A logic gate is a basic building block of digital circuits that performs a specific logical function on one or more binary inputs to produce a single binary output.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic Logic Gates
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;NOT Gate&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Function:&lt;/strong&gt; Outputs the opposite (inverted) value of the input.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Truth Table:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;A&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Output&lt;/span&gt;
&lt;span class="o"&gt;-----------&lt;/span&gt;
&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;AND Gate&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Function:&lt;/strong&gt; Outputs 1 only if all its inputs are 1.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Truth Table&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;A&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;B&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Output&lt;/span&gt;
&lt;span class="o"&gt;---------------&lt;/span&gt;
&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;OR Gate&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Function:&lt;/strong&gt; Outputs 1 if at least one of its inputs is 1.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Truth Table:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;A&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;B&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Output&lt;/span&gt;
&lt;span class="o"&gt;---------------&lt;/span&gt;
&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;XOR&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Function:&lt;/strong&gt; Outputs 1 only if all its inputs are different.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Truth Table:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;A&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;B&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Output&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|---|---|--------|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="mi"&gt;0&lt;/span&gt;    &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="mi"&gt;1&lt;/span&gt;    &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="mi"&gt;1&lt;/span&gt;    &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="mi"&gt;0&lt;/span&gt;    &lt;span class="o"&gt;|&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;NAND Gate&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Function:&lt;/strong&gt; Outputs 0 only if all its inputs are 1 (the opposite of the AND gate).&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Truth Table:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;A&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;B&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Output&lt;/span&gt;
&lt;span class="o"&gt;---------------&lt;/span&gt;
&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These are just a few of the basic logic gates; there are many more. As we delve deeper, we'll see that these gates form the foundation of a computer, enabling complex operations and computations by combining multiple gates in various configurations.&lt;/p&gt;

&lt;p&gt;Among these gates, the NAND gate stands out as a universal gate. This means that any logic gate can be constructed using only NAND gates, making it a powerful and versatile component in digital circuit design. &lt;/p&gt;

&lt;p&gt;Here is the &lt;a href="https://en.wikipedia.org/wiki/Hardware_description_language" rel="noopener noreferrer"&gt;HDL&lt;/a&gt; code for creating &lt;a href="https://github.com/RohitDhatrak/Nand2Tetris/blob/main/1-logic-gates/Not.hdl" rel="noopener noreferrer"&gt;NOT&lt;/a&gt;, &lt;a href="https://github.com/RohitDhatrak/Nand2Tetris/blob/main/1-logic-gates/And.hdl" rel="noopener noreferrer"&gt;AND&lt;/a&gt;, &lt;a href="https://github.com/RohitDhatrak/Nand2Tetris/blob/main/1-logic-gates/Or.hdl" rel="noopener noreferrer"&gt;OR&lt;/a&gt; &amp;amp; &lt;a href="https://github.com/RohitDhatrak/Nand2Tetris/blob/main/1-logic-gates/Xor.hdl" rel="noopener noreferrer"&gt;XOR&lt;/a&gt; gates using the NAND gate as a starting point. Additionally, there are 16-bit variants of these gates, which take 16-bit inputs and produce a 16-bit output instead of single-bit outputs. (&lt;a href="https://github.com/RohitDhatrak/Nand2Tetris/blob/main/1-logic-gates/Not16.hdl" rel="noopener noreferrer"&gt;NOT16&lt;/a&gt;, &lt;a href="https://github.com/RohitDhatrak/Nand2Tetris/blob/main/1-logic-gates/And16.hdl" rel="noopener noreferrer"&gt;AND16&lt;/a&gt;, &lt;a href="https://github.com/RohitDhatrak/Nand2Tetris/blob/main/1-logic-gates/Or16.hdl" rel="noopener noreferrer"&gt;OR16&lt;/a&gt;)&lt;/p&gt;

&lt;h3&gt;
  
  
  Multiplexer
&lt;/h3&gt;

&lt;p&gt;A multiplexer is a three-input gate that selects between two data inputs, &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;, based on a third input called the &lt;code&gt;sel&lt;/code&gt; (selection) bit. The multiplexer outputs the value of &lt;code&gt;a&lt;/code&gt; when the selection bit is 0 and the value of &lt;code&gt;b&lt;/code&gt; when the selection bit is 1. The truth table &amp;amp; the diagram of a multiplexer is given below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.rohitdhatrak.com%2F_astro%2Fmux.B0uTatN6_Z2wXBUy.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.rohitdhatrak.com%2F_astro%2Fmux.B0uTatN6_Z2wXBUy.webp" alt="Multiplexer" width="696" height="410"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Truth Table:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;sel&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;out&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|-----|---|---|-----|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="mi"&gt;0&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="mi"&gt;0&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="mi"&gt;0&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="mi"&gt;0&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="mi"&gt;0&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="mi"&gt;1&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="mi"&gt;0&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="mi"&gt;1&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="mi"&gt;1&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="mi"&gt;0&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="mi"&gt;1&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="mi"&gt;1&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="mi"&gt;1&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="mi"&gt;0&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="mi"&gt;1&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="mi"&gt;1&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This functionality allows a multiplexer to act as a simple switch that chooses between two inputs based on the selection bit. You can check the implementation of a &lt;a href="https://github.com/RohitDhatrak/Nand2Tetris/blob/main/1-logic-gates/Mux.hdl" rel="noopener noreferrer"&gt;Mux&lt;/a&gt; using the basic gates that we built earlier. Using the &lt;code&gt;Mux&lt;/code&gt; chip we can create a &lt;a href="https://github.com/RohitDhatrak/Nand2Tetris/blob/main/1-logic-gates/Mux16.hdl" rel="noopener noreferrer"&gt;&lt;code&gt;Mux16&lt;/code&gt;&lt;/a&gt; chip that will do the same thing but with a 16-bit input.&lt;/p&gt;

&lt;h3&gt;
  
  
  Half Adder
&lt;/h3&gt;

&lt;p&gt;A half adder is a digital circuit that performs the addition of two single-bit binary numbers. It produces two outputs: the sum bit and the carry bit.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Function:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sum (S):&lt;/strong&gt; Represents the sum of the two input bits.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Carry (C):&lt;/strong&gt; Represents the carry-out bit, which is 1 if the sum exceeds the value that can be represented by a single bit.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.rohitdhatrak.com%2F_astro%2Fhalf-adder.D9gvxGUh_2idkhg.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.rohitdhatrak.com%2F_astro%2Fhalf-adder.D9gvxGUh_2idkhg.webp" alt="Half Adder" width="706" height="438"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Truth Table:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;A&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;B&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Carry &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;C&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Sum &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;S&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|---|---|-----------|---------|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;     &lt;span class="mi"&gt;0&lt;/span&gt;     &lt;span class="o"&gt;|&lt;/span&gt;    &lt;span class="mi"&gt;0&lt;/span&gt;    &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;     &lt;span class="mi"&gt;0&lt;/span&gt;     &lt;span class="o"&gt;|&lt;/span&gt;    &lt;span class="mi"&gt;1&lt;/span&gt;    &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;     &lt;span class="mi"&gt;0&lt;/span&gt;     &lt;span class="o"&gt;|&lt;/span&gt;    &lt;span class="mi"&gt;1&lt;/span&gt;    &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;     &lt;span class="mi"&gt;1&lt;/span&gt;     &lt;span class="o"&gt;|&lt;/span&gt;    &lt;span class="mi"&gt;0&lt;/span&gt;    &lt;span class="o"&gt;|&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can observe that the sum and the carry can be computed by using an XOR and AND gate respectively. You can see the implementation of a HalfAdder circuit &lt;a href="https://github.com/RohitDhatrak/Nand2Tetris/blob/main/2-alu/HalfAdder.hdl" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Full Adder
&lt;/h3&gt;

&lt;p&gt;A full adder is a digital circuit that performs the addition of three binary bits: two significant bits (a &amp;amp; b) and an incoming carry bit (Cin). It produces two outputs: the sum bit and the carry-out bit.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Function:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sum (S):&lt;/strong&gt; Represents the sum of the three input bits.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Carry (C):&lt;/strong&gt; Represents the carry-out bit, which is 1 if the sum exceeds the value that can be represented by a single bit.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.rohitdhatrak.com%2F_astro%2Ffull-adder.Cr22WFTS_18UR2P.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.rohitdhatrak.com%2F_astro%2Ffull-adder.Cr22WFTS_18UR2P.webp" alt="Full Adder" width="616" height="384"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Truth Table:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Cin&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Carry &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;C&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Sum &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;S&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|---|---|-----|-----------|---------|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="mi"&gt;0&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt;     &lt;span class="mi"&gt;0&lt;/span&gt;     &lt;span class="o"&gt;|&lt;/span&gt;    &lt;span class="mi"&gt;0&lt;/span&gt;    &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="mi"&gt;1&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt;     &lt;span class="mi"&gt;0&lt;/span&gt;     &lt;span class="o"&gt;|&lt;/span&gt;    &lt;span class="mi"&gt;1&lt;/span&gt;    &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="mi"&gt;0&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt;     &lt;span class="mi"&gt;0&lt;/span&gt;     &lt;span class="o"&gt;|&lt;/span&gt;    &lt;span class="mi"&gt;1&lt;/span&gt;    &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="mi"&gt;1&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt;     &lt;span class="mi"&gt;1&lt;/span&gt;     &lt;span class="o"&gt;|&lt;/span&gt;    &lt;span class="mi"&gt;0&lt;/span&gt;    &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="mi"&gt;0&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt;     &lt;span class="mi"&gt;0&lt;/span&gt;     &lt;span class="o"&gt;|&lt;/span&gt;    &lt;span class="mi"&gt;1&lt;/span&gt;    &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="mi"&gt;1&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt;     &lt;span class="mi"&gt;1&lt;/span&gt;     &lt;span class="o"&gt;|&lt;/span&gt;    &lt;span class="mi"&gt;0&lt;/span&gt;    &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="mi"&gt;0&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt;     &lt;span class="mi"&gt;1&lt;/span&gt;     &lt;span class="o"&gt;|&lt;/span&gt;    &lt;span class="mi"&gt;0&lt;/span&gt;    &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;  &lt;span class="mi"&gt;1&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt;     &lt;span class="mi"&gt;1&lt;/span&gt;     &lt;span class="o"&gt;|&lt;/span&gt;    &lt;span class="mi"&gt;1&lt;/span&gt;    &lt;span class="o"&gt;|&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can calculate the sum using two Half Adder circuits and then OR the two carry’s from the two Half Adders to give us the final carry output. You can take a look at the implementation &lt;a href="https://github.com/RohitDhatrak/Nand2Tetris/blob/main/2-alu/FullAdder.hdl" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Add16
&lt;/h3&gt;

&lt;p&gt;Using the Half Adder and Full Adder implemented above we can create a circuit that takes two 16-bit binary numbers and outputs the sum of the two numbers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.rohitdhatrak.com%2F_astro%2Fadd16.CuhwXz1J_Z1yHyQ0.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.rohitdhatrak.com%2F_astro%2Fadd16.CuhwXz1J_Z1yHyQ0.webp" alt="Add16" width="668" height="460"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can look at the implementation of an Add16 circuit &lt;a href="https://github.com/RohitDhatrak/Nand2Tetris/blob/main/2-alu/Add16.hdl" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does a computer do basic math?
&lt;/h2&gt;

&lt;p&gt;An Arithmetic Logic Unit (ALU) is a fundamental component of a computer's central processing unit (CPU) responsible for performing arithmetic and logic operations. It is a critical part of the CPU and is essential for executing a variety of tasks that the CPU needs to perform.&lt;/p&gt;

&lt;h3&gt;
  
  
  Our ALU Architecture
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.rohitdhatrak.com%2F_astro%2Falu.TZgx7mih_YfMLH.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.rohitdhatrak.com%2F_astro%2Falu.TZgx7mih_YfMLH.webp" alt="ALU" width="800" height="563"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The ALU (Arithmetic Logic Unit) receives two 16-bit binary inputs, &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;. Various control pins dictate the operations performed on these inputs before producing the final output. Here is a detailed breakdown of how each control pin influences the process:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;zx:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;zx&lt;/code&gt; is 1, set &lt;code&gt;x&lt;/code&gt; to 0, regardless of its original value.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;zy:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;zy&lt;/code&gt; is 1, set &lt;code&gt;y&lt;/code&gt; to 0, regardless of its original value.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;nx:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;nx&lt;/code&gt; is 1, negate the value of &lt;code&gt;x&lt;/code&gt; (apply bitwise NOT operation to &lt;code&gt;x&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ny:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;ny&lt;/code&gt; is 1, negate the value of &lt;code&gt;y&lt;/code&gt; (apply bitwise NOT operation to &lt;code&gt;y&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;f:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;f&lt;/code&gt; is 1, compute the sum of &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; (&lt;code&gt;x + y&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;f&lt;/code&gt; is 0, compute the bitwise AND of &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; (&lt;code&gt;x &amp;amp; y&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;no:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;no&lt;/code&gt; is 1, negate the result from the &lt;code&gt;f&lt;/code&gt; operation (apply bitwise NOT to the output of &lt;code&gt;f&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Finally, the ALU produces the following outputs:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;out:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The result after all specified operations have been applied to &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;zr:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;A flag that is set to 1 if the &lt;code&gt;out&lt;/code&gt; value is zero; otherwise, it is set to 0.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ng:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;A flag that is set to 1 if the &lt;code&gt;out&lt;/code&gt; value is negative (the most significant bit of the 16-bit output is 1); otherwise, it is set to 0.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Truth Table:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.rohitdhatrak.com%2F_astro%2Falu-truth-table.kKTspxXi_1Ups6u.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.rohitdhatrak.com%2F_astro%2Falu-truth-table.kKTspxXi_1Ups6u.webp" alt="ALU Truth Table" width="800" height="696"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using the 6 control bits (zx, nx, zy, ny, f &amp;amp; no) we can do all the above operations with our two inputs x &amp;amp; y&lt;/p&gt;

&lt;h3&gt;
  
  
  How to make an ALU?
&lt;/h3&gt;

&lt;p&gt;An Arithmetic Logic Unit (ALU) can be constructed using the basic chips and gates we have created so far. Here’s a detailed explanation of how each step in the ALU works:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight verilog"&gt;&lt;code&gt;&lt;span class="c1"&gt;// zero the x &amp;amp; y input&lt;/span&gt;
&lt;span class="n"&gt;Mux16&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;zx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;zeroXResult&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Mux16&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;zy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;zeroYResult&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We check if the &lt;code&gt;zx&lt;/code&gt; and &lt;code&gt;zy&lt;/code&gt; control flags are set. If &lt;code&gt;zx&lt;/code&gt; is set to 1, the input &lt;code&gt;x&lt;/code&gt; is zeroed. Similarly, if &lt;code&gt;zy&lt;/code&gt; is set to 1, the input &lt;code&gt;y&lt;/code&gt; is zeroed. This is done using a &lt;code&gt;Mux16&lt;/code&gt; chip, which selects between the original input and zero based on the control flags.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight verilog"&gt;&lt;code&gt;&lt;span class="c1"&gt;// negate the x &amp;amp; y input?&lt;/span&gt;
&lt;span class="n"&gt;Not16&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;in&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;zeroXResult&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;notX&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Not16&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;in&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;zeroYResult&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;notY&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Mux16&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;zeroXResult&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;notX&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;notXResult&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Mux16&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;zeroYResult&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;notY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ny&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;notYResult&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we compute the bitwise NOT of &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;. Depending on the values of the &lt;code&gt;nx&lt;/code&gt; and &lt;code&gt;ny&lt;/code&gt; control flags, we either keep the original values or use their negated forms. The &lt;code&gt;Mux16&lt;/code&gt; chip is used again to select between the original and negated values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight verilog"&gt;&lt;code&gt;&lt;span class="c1"&gt;// compute (out = x + y) or (out = x &amp;amp; y)&lt;/span&gt;
&lt;span class="n"&gt;And16&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;notXResult&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;notYResult&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;andResult&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Add16&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;notXResult&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;notYResult&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;addResult&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Mux16&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;andResult&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;addResult&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fResult&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We check if the &lt;code&gt;f&lt;/code&gt; control flag is set. If &lt;code&gt;f&lt;/code&gt; is set to 1 the ALU either computes the sum of &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; otherwise it computes the bitwise AND of &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;. The &lt;code&gt;Mux16&lt;/code&gt; chip selects between the AND result and the addition result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight verilog"&gt;&lt;code&gt;&lt;span class="c1"&gt;// negate the output&lt;/span&gt;
&lt;span class="n"&gt;Not16&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;in&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fResult&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;notFResult&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// out &amp;amp; ng&lt;/span&gt;
&lt;span class="n"&gt;Mux16&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fResult&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;notFResult&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ng&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;0..7&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;outFirst8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;8..15&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;outSecond8&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Depending on the &lt;code&gt;no&lt;/code&gt; control flag, the ALU either keeps the computed result or negates it. The &lt;code&gt;Mux16&lt;/code&gt; chip selects between the original and negated output. The most significant bit (MSB) of the output is used to determine the sign of the result, setting the &lt;code&gt;ng&lt;/code&gt; (negative) flag if the MSB is 1. We also break the output in two parts and store it to use in the next step.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight verilog"&gt;&lt;code&gt;&lt;span class="c1"&gt;// zr&lt;/span&gt;
&lt;span class="n"&gt;Or8Way&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;in&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;outFirst8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;zr1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Or8Way&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;in&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;outSecond8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;zr2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Or&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;zr1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;zr2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;orZr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Not&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;in&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;orZr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;zr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To determine if the output is zero, the ALU uses two &lt;a href="https://github.com/RohitDhatrak/Nand2Tetris/blob/main/1-logic-gates/Or8Way.hdl" rel="noopener noreferrer"&gt;&lt;code&gt;Or8Way&lt;/code&gt;&lt;/a&gt; chips to check if any of the bits in the lower and upper halves of the output are 1. If both halves are zero, the &lt;code&gt;zr&lt;/code&gt; (zero) flag is set to 1 using an &lt;code&gt;Or&lt;/code&gt; and &lt;code&gt;Not&lt;/code&gt; gate.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By combining basic logic gates, multiplexers, and adders, we built an ALU—the heart of a CPU. While we have chosen the NAND gate as our fundamental building block, other gates such as NOR or AND-OR-INVERT (AOI) could also serve as a basis for constructing logic circuits. Also while our ALU is capable of computing 64 different functions, we focus on only 18 of these in our implementation. The design prioritizes functionality over efficiency, more advanced techniques like &lt;a href="https://en.wikipedia.org/wiki/Carry-lookahead_adder" rel="noopener noreferrer"&gt;carry lookahead&lt;/a&gt; can significantly optimize arithmetic operations, leading to better performance. Since addition is fundamental to computer architectures, such improvements can have a system-wide impact. However, our approach emphasizes a minimalistic ALU design, delegating complex arithmetic operations to system software. This division reflects a common trade-off in computing: balancing hardware complexity with software flexibility. This foundational understanding takes us a step closer to comprehending how low-level hardware functions and interacts with high-level programming, shaping the core of modern computing systems.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>computerscience</category>
      <category>coding</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Why JavaScript will always need Transpilers and Polyfills</title>
      <dc:creator>Rohit Dhatrak</dc:creator>
      <pubDate>Tue, 28 Sep 2021 15:27:18 +0000</pubDate>
      <link>https://dev.to/rohitdhatrak/why-javascript-will-always-need-transpilers-and-polyfills-45bk</link>
      <guid>https://dev.to/rohitdhatrak/why-javascript-will-always-need-transpilers-and-polyfills-45bk</guid>
      <description>&lt;p&gt;This blog post was originally published  &lt;a href="https://www.rohitdhatrak.com/backwards-forwards-compatibility/" rel="noopener noreferrer"&gt;here&lt;/a&gt; .&lt;/p&gt;

&lt;p&gt;To understand why we’ll always need transpilers and polyfills let's take a look at Backwards and Forwards compatibility in JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Backwards compatibility
&lt;/h2&gt;

&lt;p&gt;Backwards compatibility means that once something is added to the language there won’t be any changes in the future that cause it to become invalid.&lt;/p&gt;

&lt;p&gt;Think about this for a second. This assurance is no small thing, right?&lt;/p&gt;

&lt;p&gt;We certainly shouldn’t take it for granted. This has a huge impact on decisions involving adding something to the language. Because once it is added we can't remove it just like that.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ndk59hzjsg0uwwlnb9o.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ndk59hzjsg0uwwlnb9o.gif" alt="https://c.tenor.com/4xYdJ6ySbfUAAAAd/tata-bye-bye-rahul-gandhi-funny-meme.gif" width="640" height="368"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are not going to wake up one day and find our code has just stopped running. Because of this assurance we, JavaScript developers can sleep peacefully and it makes choosing JavaScript a safe bet.&lt;/p&gt;

&lt;p&gt;But there are some exceptions to this.🙃 JavaScript has a few backwards-incompatible changes. However, the JavaScript committee is very careful in doing so.&lt;/p&gt;

&lt;p&gt;They study the code on the web by gathering data from the browsers to get an estimate of the impact. They make the change only if the impact is going to be minimal and if the browsers are willing to take the brunt from the change.&lt;/p&gt;

&lt;h2&gt;
  
  
  Forwards Compatibility
&lt;/h2&gt;

&lt;p&gt;Forwards compatibility means new syntax would be able to run in an old JavaScript engine. That is if we take some code that was added to the language in 2019 it should be able to run in a JavaScript engine from 2015, 2010 or any previous years.&lt;/p&gt;

&lt;p&gt;JavaScript is &lt;strong&gt;not&lt;/strong&gt; forwards compatible.&lt;/p&gt;

&lt;p&gt;On the contrary HTML and CSS are forwards compatible but not backwards compatible.&lt;/p&gt;

&lt;p&gt;If we take some old HTML or CSS from 2005 it might not run or produce the same results. On the other hand, if we run modern-day HTML or CSS in an old browser it will just skip over the parts it doesn’t recognize, while the rest would be processed accordingly.&lt;/p&gt;

&lt;p&gt;This is possible because HTML and CSS are declarative and it is easier to skip over the stuff that is not recognizable. However, just imagine if the JavaScript engine starts to skip stuff that it doesn't recognize we'll get errors and bugs left and right in our code!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F57g48542e2mmo4aemv16.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F57g48542e2mmo4aemv16.gif" alt="https://y.yarn.co/66980f5e-23c0-4765-9c38-677cbf2d6126_text.gif" width="400" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We'll have to support some older versions of the browser because everyone doesn't have the latest version. So if we can’t run new code on an old engine should we always stick to an older syntax based on the oldest engine we need to support?&lt;/p&gt;

&lt;p&gt;This is where the tools come in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Transpilers
&lt;/h2&gt;

&lt;p&gt;A &lt;a href="https://en.wikipedia.org/wiki/Source-to-source_compiler" rel="noopener noreferrer"&gt;transpiler&lt;/a&gt; will convert a new syntax to an older syntax.&lt;/p&gt;

&lt;p&gt;The most commonly used transpiler is &lt;a href="https://babeljs.io/" rel="noopener noreferrer"&gt;Babel&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For example, consider the following snippet of code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;something&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we run this in an older version of a browser where &lt;code&gt;let&lt;/code&gt; is not defined we'll run into issues. So babel will transpile it to an &lt;strong&gt;equivalent&lt;/strong&gt; older syntax.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;x0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;x1&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;something&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;x0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;x1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can write newer forms of syntax without worrying about compatibility issues in old browsers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Polyfills
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Polyfill" rel="noopener noreferrer"&gt;Polyfills&lt;/a&gt; (aka shims) are useful when the issue is related to a missing API rather than some new syntax. Let us understand what we mean by this.&lt;/p&gt;

&lt;p&gt;Let's assume we want to support an older version of a browser where Array.map() method is not defined.&lt;/p&gt;

&lt;p&gt;So to use the method we’ll have to provide our own implementation that will act as if it was already defined.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft7y7cztxjne1xgz90jku.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft7y7cztxjne1xgz90jku.gif" alt="https://c.tenor.com/kEJ7XCJ_HakAAAAd/thanos-ill-do-it.gif" width="526" height="200"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;newArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;newArray&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The if statement will check if the map() method is defined. If not then our custom implementation will kick in.&lt;/p&gt;

&lt;p&gt;Transpilers like Babel will automatically detect which polyfills are needed in our code but sometimes we might have to do it ourselves.&lt;/p&gt;

&lt;p&gt;The above example is just for illustration purposes. When you need to manually define polyfills use a robust and well-tested polyfill from an official library like &lt;a href="https://github.com/es-shims" rel="noopener noreferrer"&gt;es-shims&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Since JavaScript is not forwards compatible there will always be a gap between the latest code we can write and the oldest JS engine we need to support.&lt;/p&gt;

&lt;p&gt;As developers, we should focus on writing clean and newer syntax that communicates the ideas effectively and let the tools take care of the compatibility.&lt;/p&gt;

&lt;p&gt;Shoutout to the &lt;a href="https://github.com/getify/You-Dont-Know-JS" rel="noopener noreferrer"&gt;YDKJS&lt;/a&gt; book series by &lt;a href="https://www.linkedin.com/in/getify/" rel="noopener noreferrer"&gt;Kyle Simpson&lt;/a&gt; that enabled this blog post.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>babel</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Time and Space Complexity</title>
      <dc:creator>Rohit Dhatrak</dc:creator>
      <pubDate>Thu, 05 Nov 2020 12:05:11 +0000</pubDate>
      <link>https://dev.to/rohitdhatrak/time-and-space-complexity-633</link>
      <guid>https://dev.to/rohitdhatrak/time-and-space-complexity-633</guid>
      <description>&lt;h5&gt;
  
  
  What is time complexity?
&lt;/h5&gt;

&lt;p&gt;Time Complexity could be used to determine if our algorithm will be able to run in the required amount of time by looking at how the runtime grows according to the input. We don't measure the runtime in seconds as it depends on various factors which we don't want to take into consideration. We are interested in the behaviour of an algorithm for a large number of inputs.&lt;/p&gt;

&lt;h5&gt;
  
  
  How do we measure time complexity?
&lt;/h5&gt;

&lt;p&gt;Big-O notation gives the upper bound of the time complexity of an algorithm.&lt;/p&gt;

&lt;p&gt;Let's say we have a function T(n) which gives the runtime of an algorithm where n is the number of inputs. If we can find a function f(n) which when multiplied by c is greater than T(n) then we can say that f(n) upper bounds T(n).&lt;/p&gt;

&lt;p&gt;This can be mathematically represented as T(n) &amp;lt;= f(n). And we write it as O(f(n)).&lt;/p&gt;

&lt;h5&gt;
  
  
  How To Calculate Big O
&lt;/h5&gt;

&lt;p&gt;i) Break your algorithm into individual operations.&lt;br&gt;
ii) Calculate the no of times each operation repeats.&lt;br&gt;
iii) Add everything together.&lt;br&gt;
iv) Remove the constants multiples.&lt;br&gt;
v) Drop the lower order terms.&lt;/p&gt;

&lt;p&gt;We get rid of all the constant multiples and the lower order terms because they don't contribute much for large values of input. We'll see the steps in the examples below.&lt;/p&gt;
&lt;h5&gt;
  
  
  What are the different time complexities?
&lt;/h5&gt;
&lt;h6&gt;
  
  
  1. Constant time
&lt;/h6&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int average(int a, int b) {
    int avg = (a+b)/2;      // occurs once
    return avg;             // occurs once
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;After adding all the values we get :&lt;br&gt;
1 + 1 = 2&lt;/p&gt;

&lt;p&gt;In this function, each step occurs once so after adding all the steps we get 2. Which can be written as O(1) because we'll drop all the constant multiples.&lt;/p&gt;

&lt;p&gt;Therefore as the input increases, the time taken by the algorithm stays the same.&lt;/p&gt;
&lt;h6&gt;
  
  
  2. Linear time
&lt;/h6&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int find(int arr[], int key) {
    int n = sizeof(arr);         // 1
    for(int i = 0; i &amp;lt; n; i++) { // n
        if(arr[i] == key)        // 1
            return i;            // 1
    }
    return -1;                   // 1
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;After adding all the values we get :&lt;br&gt;
1 + n * (1 + 1)+1&lt;br&gt;
= 2 + 2n&lt;/p&gt;

&lt;p&gt;We multiply the values in the for loop i.e (1 + 1) with no of times the loop occurs i.e n. Because the steps inside the for loop will repeat for n number of times.&lt;/p&gt;

&lt;p&gt;By dropping the lower order term 2, we get 2n. Then we will drop the constant multiple 2. So the time complexity is given by O(n).&lt;/p&gt;

&lt;p&gt;Therefore as input increases, the time taken by the algorithm increases linearly.&lt;/p&gt;
&lt;h6&gt;
  
  
  3. Quadratic time
&lt;/h6&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int findDuplicates(int array[]) {
    int n = sizeof(array);                        // 1
    for (int i = 0; i &amp;lt; n; i++){                  // n
        for (int j = 0; j &amp;lt; n; j++){              // n
            if (i !==j &amp;amp;&amp;amp; array[i] == array[j]){  // 1
                return i;                         // 1
            }
        }
    }
    return -1;                                    // 1
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;After adding all the values we get :&lt;br&gt;
1 + n * (n * (1 + 1 ) ) + 1&lt;br&gt;
= 2 + 2n&lt;sup&gt;2&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;By dropping the lower order term and the constant multiple we get O(n&lt;sup&gt;2&lt;/sup&gt;).&lt;/p&gt;

&lt;p&gt;Therefore as input increases, the time taken by the algorithm increases in a quadratic fashion.&lt;/p&gt;
&lt;h6&gt;
  
  
  4. Logarithmic time
&lt;/h6&gt;

&lt;p&gt;Log is written as log x y&lt;/p&gt;

&lt;p&gt;log 2 8 = 3&lt;br&gt;
You can think of it as how many 2's do we multiply together to get 8. The answer is 3.&lt;/p&gt;

&lt;p&gt;Now we'll take a look at binary search. The way this algorithm operates is similar to how we might search for a word in a dictionary. Let's say we open the dictionary somewhere in the middle. If the word we are looking for lies in the left half we ignore the right half and then again split the left half in two.&lt;/p&gt;

&lt;p&gt;If we keep on doing this process we'll eventually find the word we are looking for because the words in a dictionary are arranged in alphabetical order. Similarly, if we have an sorted array and we want to look for a value x we can apply binary search.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int binarySearch(int arr[], int x) {

    int l = 0;
    int h = sizeof(arr)-1;

    while (r &amp;gt;= l) {
        int mid = l + (r - l) / 2;

        // If the element is present at the
            // middle itself
            if (arr[mid] == x)
            return mid;

            // If element is smaller than mid, then
            // it can only be present in left subarray
            if (arr[mid] &amp;gt; x)
                h = mid - 1;

            // Else the element can only be present
            // in right subarray
        if (arr[mid]&amp;lt;)x
            l = mid + 1;
    }

    // We reach here when element is not present
    // in array
    return -1;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this algorithm, the amount of data we have to work with is reduced by half with each iteration.&lt;/p&gt;

&lt;p&gt;n/2, n/4, n/8,...&lt;/p&gt;

&lt;p&gt;Therefore in the worst case, we'll find the element in the last iteration i.e we kept on dividing until only one element was left.&lt;/p&gt;

&lt;p&gt;This can be mathematically represented by n/n = 1. The denominator can be represented by a power of 2 as we divided by 2 with every iteration.&lt;/p&gt;

&lt;p&gt;Therefore we can write it as n/2&lt;sup&gt;k&lt;/sup&gt; = 1 where k is the number of times we divided by 2.&lt;br&gt;
∴ n = 2&lt;sup&gt;k&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;Because k is the number of times we divided by 2, k will give us the number of iterations it took to get the number in the worst possible case.&lt;br&gt;
The equation n = 2&lt;sup&gt;k&lt;/sup&gt; can be written as k = log2(n). Therefore the time complexity is O(log n).&lt;/p&gt;

&lt;p&gt;Therefore as input increases, the time taken by the algorithm increases logarithmically.&lt;/p&gt;

&lt;p&gt;Therefore the Time Complexity of a loop is considered as O(logn) if the loop variable is divided or multiplied by a constant amount.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (int i = 1; i &amp;lt;=n; i *= c) {
    // some O(1) expressions
}
for (int i = n; i &amp;gt; 0; i /= c) {
    // some O(1) expressions
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8itti0oicab6wlelrwur.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8itti0oicab6wlelrwur.png" alt="Time Complexity Plots" width="800" height="691"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we can see the different time complexities we discussed above and how they compare to each other. O(1) is the fastest and O(n&lt;sup&gt;2&lt;/sup&gt;) is the slowest.&lt;/p&gt;

&lt;p&gt;There are other time complexities as well like O(nlog n), O(c&lt;sup&gt;n&lt;/sup&gt;), O(n!) which we'll cover in the future posts. Until then you can &lt;a href="https://tinyletter.com/rohitdhatrak" rel="noopener noreferrer"&gt;sign up&lt;/a&gt; to get notified about new posts.&lt;/p&gt;

&lt;p&gt;An interesting thing to note is that O(log n) is slower than O(n) for smaller values. But we don't really care about small values we check for the behaviour of the algorithm when the input is large.&lt;/p&gt;

&lt;h5&gt;
  
  
  What is Space Complexity?
&lt;/h5&gt;

&lt;p&gt;It is same as time complexity but instead of looking at how much more time our algorithm takes as the input grows we look at how much more space does our algorithm consume when we the input grows.&lt;/p&gt;

&lt;p&gt;We often optimize for time over space because usually space is not an issue but we want our algorithms to be faster. However, in scenarios like working with embedded systems where we have a memory constrain, space complexity becomes equally important.&lt;/p&gt;

&lt;p&gt;If you liked the post or have any questions/suggestions feel free to reach out on any of the social media platforms.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Originally posted &lt;a href="https://www.rohitdhatrak.com/posts/time-space-complexity/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>algorithms</category>
      <category>codenewbie</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
