<?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" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ binary - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ binary - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Mon, 25 May 2026 05:06:45 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/binary/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ What is Coding? Computer Coding Definition ]]>
                </title>
                <description>
                    <![CDATA[ When you're starting your coding journey, one of the most exciting – and at times overwhelming – things about it is just how much there is to learn. You'll discover different languages, frameworks, libraries, inventions and conventions. One technolog... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/what-is-coding/</link>
                <guid isPermaLink="false">66b1e4e4ab763c1471e56792</guid>
                
                    <category>
                        <![CDATA[ beginners guide ]]>
                    </category>
                
                    <category>
                        <![CDATA[ binary ]]>
                    </category>
                
                    <category>
                        <![CDATA[ coding ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Dionysia Lemonaki ]]>
                </dc:creator>
                <pubDate>Tue, 10 Aug 2021 19:54:45 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/08/hope-house-press-leather-diary-studio-h3sAF1cVURw-unsplash.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>When you're starting your coding journey, one of the most exciting – and at times overwhelming – things about it is just how much there is to learn.</p>
<p>You'll discover different languages, frameworks, libraries, inventions and conventions.</p>
<p>One technology often requires knowledge of another one, and everything seems to be interconnected and intertwined. </p>
<p>With new technologies coming out often in such a fast paced and ever-changing industry, it can quickly get confusing for beginner coders.</p>
<p>When learning how to code, instead of just focusing on learning a specific technology, it can also help to learn the foundations – the building blocks – and to peel back the layers of abstraction to get to know the underlying principles that all these technologies have in common.</p>
<p>Understanding what coding is at a fundamental level will make solving problems easier and will give you a better understanding of how these technologies work underneath the hood.</p>
<p>In this article we will learn what coding is so you have a solid foundation on which to build.</p>
<h2 id="heading-how-do-computers-work">How Do Computers Work?</h2>
<p>Computers, when powered off, are just costly electronic machines. They're objects consisting of a bunch of metals, plastics, and other materials.</p>
<p>However, once you press the power button and they have gone through their power-up process, called <em>booting up</em>, they come to life.</p>
<p>Your computer turns into this extremely powerful machine. It's this electronic device that gets to do complicated tasks at mind boggling speeds that would be difficult, if not impossible, for humans to do. </p>
<p>Their screens are vibrant and active and there's a variety of buttons and icons ready to be clicked. </p>
<h3 id="heading-computers-and-electricity">Computers and Electricity</h3>
<p>Computers are powered and function with the help of electricity.</p>
<p>Electricity has only two states – it can be <strong>either turned on or off.</strong> </p>
<p>Electricity being turned on and flowing represents true, it has the 'on' state. On the other hand, when it's turned off and not flowing this represents false and the 'off' state.</p>
<p>It can only ever have one state at each point in time. </p>
<p>The two states of electricity are called <em>Binary States</em>, the prefix bi- meaning two.</p>
<h3 id="heading-what-is-binary-code">What is Binary Code?</h3>
<p>This concept of electrical charges and the existence of only two possible states ties in nicely with the numerical system computers use in their hardware. They use it to complete every single task they are given. It's called binary code and is made up of sequences of <code>0</code>s and <code>1</code>s.</p>
<p>Binary code by design corresponds directly with specific machine instructions, commands, and locations in the computer's memory. The computer then reads and interprets these instructions and then carries out particular tasks.</p>
<p>Computers are made up of a combination of thousands of tiny physical devices that act as electrical switches, called <strong>transistors.</strong> </p>
<p><a target="_blank" href="https://commons.wikimedia.org/wiki/File:Transistors.agr.jpg"><img src="https://www.freecodecamp.org/news/content/images/2021/08/Screenshot-2021-08-10-at-3.11.05-PM.png" alt="Screenshot-2021-08-10-at-3.11.05-PM" width="600" height="400" loading="lazy"></a></p>
<p>These little electrical hardware components, the transistors, enable or disable the flow of electricity. </p>
<p>They can have either a positive or a negative electric charge depending on their state – that is, whether they carry electricity or don’t.</p>
<p>Those thousands of tiny little switches can either be on or off.</p>
<h3 id="heading-how-binary-works">How Binary Works</h3>
<p>You may have heard that <em>‘Computers work in 1s and 0s’</em> but what does that actually mean? We might not deal with binary code directly anymore, but it is the only thing that the computer's CPU understands.</p>
<p>Are there literally 0s and 1s stored in our physical devices flowing and moving around? Not really.</p>
<p>However, our CPU's consist of many microscopic digital circuits that carry information.</p>
<p>With the help of the transistors that come together to form these circuits and their tiny sequences of electrical signals that are switched either on (<code>1</code>) or off (<code>0</code>), there can only be only be two types of voltages – a high and a low. And that results in the representation of different values, that is different instructions or calculations being performed. </p>
<p>When these electrical signals from the transistor circuits and other electrical components are linked and combined/modified in a certain way, they can create a wide range of possible tasks and operations the computer can perform and see through.</p>
<h2 id="heading-computers-and-their-relationship-with-humans">Computers and Their Relationship with Humans</h2>
<p>This machine-level programming language is the only language that computers can directly understand and are capable of making sense of.</p>
<p>The binary code can vary from computer to computer and from machine to machine. When it comes to this level of programming a computer, there is no portability. This means that programs and software can't be transferred to different systems.</p>
<h3 id="heading-limitations-of-machine-languages">Limitations of Machine Languages</h3>
<p>Machine languages can vary depending on the operating system they are implemented on.</p>
<p>This is, of course, very limiting. </p>
<p>Machine code or binary code may run very fast, they may be the computer's native language, and they may be extremely efficient because the instructions are executed directly by the CPU. But it's a very dull and monotonous – not to mention extremely error-prone – way to use a computer.</p>
<p>Attempting to use a computer by manually typing binary codes for each transitor is a cumbersome process.</p>
<p>Making an error while directly managing the computer's data storage and operations is very hard to fix.</p>
<p>Machine-level languages are hard for humans to read, write, learn and understand. So programmers and computer scientists found a better and arguably easier way to solve problems.</p>
<p>The things a computer can do by itself are on a very primitive level and are limited in scope.</p>
<p>They are good at performing arithmetic calculations like adding numbers or checking if a number is equal to zero.</p>
<h3 id="heading-the-human-element-in-computing">The Human Element in Computing</h3>
<p>Humans created these machines that have revolutionized our way of life, but when it comes down to it, computers are really not that smart and have limited capabilities by themselves. </p>
<p>They only do exactly what they are told to do. They don’t make assumptions or have any common sense like humans do.</p>
<p>Computers at their core are machines that perform mathematical operations. But they are also good at displaying some text on the screen or repeating a task over and over again.</p>
<p>Those operations are basic and don’t go much further than that. They're known as the computer’s <strong>instruction set</strong>.</p>
<p>Even though at their core computers can only do very basic tasks, they are able to perform extremely complex actions and follow and execute instructions from programs they are given. This is thanks to the many layers of abstraction they have.</p>
<p>The true power however, lies in the hands of humans. Whatever we want to achieve and whatever we imagine, we can use this machine as a tool to do complicated calculations, conduct research to find and extract a document among billions of other documents, or keep in touch with friends and family far away.</p>
<p>Whatever we can think of, we can now create it by coding a program.</p>
<p>Computers and programs can improve our lives collectively all around the world. But how do we make them do what we want?</p>
<h2 id="heading-what-is-computer-programming">What is computer programming?</h2>
<p>Computer coding and computer programming are terms that are often used interchangeably. They do have some differences though.</p>
<h3 id="heading-programming-vs-coding">Programming vs coding</h3>
<p>Programming means telling a computer what to do and how to do that thing you told it to do. </p>
<p>It involves providing well thought-out, methodogical instructions for your computer to read and execute.</p>
<p>You have to break down large tasks into smaller ones. And you keep repeating that process of breaking something down into smaller tasks until you reach a point where you don’t need to tell the computer what to do anymore – it already knows how to do that task.</p>
<p>The essence of programming is the process of problem solving, complex thinking, attention to detail, and reasoning – all using a computer.</p>
<p>Programming involves thinking of all the different steps a user could take and considering all the different things that could go wrong. Once you've thought of all potential problems a user may encounter you have to find solutions before you code anything.</p>
<p>We can think of problem solving as taking an input (the information and details about our problem that we want to solve) and generating an output (the end goal or the solution to our problem).</p>
<p>Outputs can be complicated and millions can run per second.</p>
<h3 id="heading-problem-solving-with-algorithms">Problem Solving with Algorithms</h3>
<p>When you're problem solving using a computer, you need to express the solution to that problem according to the instruction set of the computer.</p>
<p>For that purpose, we use <strong>algorithms</strong> – a systematic approach to solving problems.</p>
<p>Algorithms are an idea or method that's expressed in a very concise and precise set of rules and step by step instructions. The computer needs to follow these instructions in order to solve the problem.</p>
<p>When we think of algorithms, they don’t only apply to computers. They are also machine independent. </p>
<p>We humans follow algorithms too – sets of instructions for completing tasks in our daily lives.</p>
<p>Some examples could be:</p>
<ul>
<li>counting people in a room</li>
<li>doing arithmetic calculations</li>
<li>trying to figure out the correct route to take to reach a particular destination</li>
<li>following a cooking recipe</li>
</ul>
<p>In the last example, we can think of the recipe as the instructions that we use, and we're the computer that has to read and then execute them correctly.</p>
<p>An algorithm is a plan that presents the steps you need to follow in order to get a desired result.</p>
<h3 id="heading-how-computers-use-algorithms">How Computers Use Algorithms</h3>
<p>When it comes to computers, algorithms need to be precise as computers take everything literally. They don’t read between the lines or make any assumptions.</p>
<p>There is no room for ambiguity, so algorithms not only need to be precise but also organized, correct, free from errors, efficient, and well-designed. All this helps minimize the time and effort the computer needs to spend to complete a task.</p>
<p>Computers execute algorithms in a mechanical way without putting much thought into each step. And they should work in the exact way we intended them to work. </p>
<p>A computer program is a collection of those instructions – or algorithms – in a text file which serves as an instruction manual.</p>
<p>It describes a very precise sequence of steps for the computer to follow. The computer performs a particular task, its hardrive executes the instructions, and you get the final desired result in the end. </p>
<p>Aside from the thinking, research, design, and in-depth planning, programming also involves testing, debugging, deployment, and maintanance of the finished result.</p>
<p>When you're developing a program to solve a particular problem, you typically express the idea for the solution using an algorithm. Then developers code the program by implementing that algorithm. You use a language that has particular syntax and that both humans and computers can understand.</p>
<p>This is where the actual coding comes in.</p>
<h2 id="heading-what-is-computer-coding-a-simple-definition">What is Computer Coding? A Simple Definition.</h2>
<p>Coding is the process of transforming ideas, solutions, and instructions into the language that the computer can understand – that is, binary-machine code.</p>
<p>Coding is how humans are able to talk with computers.</p>
<p>Coding involves communicating and giving instructions for different actions we want our computers to perform using a computer programming language.</p>
<p><strong>Programming languages</strong>, like JavaSctipt, Java, C/C++, or Python, act as the translator between humans and machines. </p>
<p>These languages bridge the communication gap between computers and humans by representing, expressing, and putting algorithms into practice. They do this using a specific sequence of statements that machines understand and can follow.</p>
<p>Programming languages are similar to human languages in the sense that they are made up of basic syntactical elements like nouns, verbs, and phrases. And you group these elements together to form something that resembles a sentence to create meaning.  </p>
<p>These languages actually resemble and look a lot like English. But they offer a shorter, more precise and less verbose way to create instructions that the computer can understand.</p>
<p>A spoken/natural language like English, on the other hand, leaves a lot of room for ambiguity and different interpretations from different people.</p>
<p>Programming languages are a set of rules that define how you write computer code.</p>
<p>We use computer code to create  all the web applications, websites, games, operating systems, and all the other software programs and technologies we use on a daily basis.</p>
<p>You can look at the code that makes up your favorite websites by hitting <code>Control and clicking your touchpad/mouse</code> then selecting <code>View Page Source</code> (or <code>inspect</code>) from the menu that pops up (or you can use the shortcut <code>Option Command U</code>):</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/Screenshot-2021-08-10-at-8.36.15-PM.png" alt="Screenshot-2021-08-10-at-8.36.15-PM" width="600" height="400" loading="lazy"></p>
<p>You'll then see the HTML, CSS, and JavaScript that make up the frontend code of the website you're using:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/Screenshot-2021-08-10-at-8.37.47-PM.png" alt="Screenshot-2021-08-10-at-8.37.47-PM" width="600" height="400" loading="lazy"></p>
<p>In a nutshell, coding is the act of translating problems that are first in a natural, human language to a machine readable language. And that translation happens thanks to programming languages and coding. </p>
<p>Coding requires that you understand the intricacies, the particular syntax, and the specific keywords that make up a programming language. Once you know these features of a language, you can start developing applications.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this article we learned how computers work at a high level. We then definied what programming is, what coding is, and the differences between them. </p>
<p>Remember that coding is only the process of writing code to develop programs and applications.</p>
<p>Coding is a subset of programming, which entails the logical reasoning, analysis, and planning out a sequence of instructions for a computer program or application before any coding is done. </p>
<p>Programming is the bigger picture in the process. Coding is a part of that process, but should always come after the programming, or problem-solving and planning stage.</p>
<p>I hoped this helped you understsand programming and coding basics. Thanks for reading!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Unicode Characters – What Every Developer Should Know About Encoding ]]>
                </title>
                <description>
                    <![CDATA[ If you are coding an international app that uses multiple languages, you'll need to know about encoding. Or even if you're just curious how words end up on your screen – yep, that's encoding, too. I'll explain a brief history of encoding in this arti... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/everything-you-need-to-know-about-encoding/</link>
                <guid isPermaLink="false">66bc55d2cd8a65d579e3aa06</guid>
                
                    <category>
                        <![CDATA[ ascii ]]>
                    </category>
                
                    <category>
                        <![CDATA[ binary ]]>
                    </category>
                
                    <category>
                        <![CDATA[ encoding ]]>
                    </category>
                
                    <category>
                        <![CDATA[ unicode ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kealan Parr ]]>
                </dc:creator>
                <pubDate>Mon, 01 Mar 2021 16:01:00 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2020/12/Title.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>If you are coding an international app that uses multiple languages, you'll need to know about encoding. Or even if you're just curious how words end up on your screen – yep, that's encoding, too.</p>
<p>I'll explain a brief history of encoding in this article (and I'll discuss how little standardisation there was) and then I'll talk about what we use now. I'll also cover some <strong>Computer Science</strong> theory you need to understand.</p>
<h2 id="heading-introduction-to-encoding">Introduction to Encoding</h2>
<p>A computer only can understand binary. Binary is the language of computers, and is made up of <code>0</code>'s and <code>1</code>'s. There is nothing else allowed. One digit is called a <code>bit</code>, and a <code>byte</code> is 8 bits. So 8 <code>0</code>'s or <code>1</code>'s make up one <code>byte</code>.</p>
<p>Everything eventually ends up as binary – programming languages, mouse moves, typing, and all the words on the screen.</p>
<p>If all the text you're reading was once binary too, then how do we turn binary into text? Let's look at what we used to do back in the beginning.</p>
<h2 id="heading-a-brief-history-of-encoding">A Brief History of Encoding</h2>
<p>In the early days of the internet, it was English only. We didn't need to worry about any other characters and the <strong>American Standard Code for Information Interchange</strong> (<strong>ASCII</strong>) was the character encoding that fit this purpose. </p>
<p><strong>ASCII</strong> is a mapping, from binary to alphanumeric characters. So when the PC receives binary:</p>
<pre><code><span class="hljs-number">01001000</span> <span class="hljs-number">01100101</span> <span class="hljs-number">01101100</span> <span class="hljs-number">01101100</span> <span class="hljs-number">01101111</span> <span class="hljs-number">00100000</span> <span class="hljs-number">01110111</span> <span class="hljs-number">01101111</span> <span class="hljs-number">01110010</span> <span class="hljs-number">01101100</span> <span class="hljs-number">01100100</span>
</code></pre><p>With <strong>ASCII</strong> it can translate that into "Hello world".</p>
<p>One byte (eight bits) was large enough to fit every English character, and some control characters too. Some of these control characters were used for instruments called teleprinters, so at the time they were useful (not so much now!) </p>
<p>But the control characters were things like  7 (<code>111</code> in binary) that would make a bell sound on your PC, 8 (<code>1000</code> in binary) that would print over the last character it just printed, or 12 (<code>1100</code> in binary) that would clear a video terminal from all the text just written.</p>
<p>Computers at this time were using 8 bits for one byte (they didn't always), so there were no issues. We could store all our control characters, all our numbers, all the English characters and have some left! Because one byte can encode 255 characters, and ASCII only needed 127 characters. So we had 128 encodings that were unused.</p>
<p>Let's look at an ASCII table here to see every character. All lowercase and uppercase A-Z and 0-9 were encoded to binary numbers. Remember the first 32 are unprintable control characters.</p>
<h2 id="heading-ascii-character-table">ASCII Character Table</h2>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/12/image-172.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Can you see how it ends with 127? We have some spare room at the end.</p>
<h1 id="heading-issues-with-ascii">Issues with ASCII</h1>
<p>The spare characters were 127 through to 255. People began to think about what would be best to fill those remaining characters. <strong>But everyone had different ideas about what those final characters should be.</strong></p>
<p>The American National Standards Institute (<strong>ANSI</strong> - don't get confused with <strong>ASCII</strong>) is a standards body for establishing standards across lots of different fields. They decided what everyone was doing with 0-127, which is what <strong>ASCII</strong> was already doing. But the rest were open.</p>
<p>No one was debating what 0-127 in the <strong>ASCII</strong> encoding was. The problem was with the <strong>spare ones</strong>.</p>
<p>Below is what the first IBM computers did with the 128-255 encodings for ASCII.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/12/image-169.png" alt="Image" width="600" height="400" loading="lazy">
<em>Some squiggles, some background icons, math operators and some accented characters like é.</em></p>
<p>But other computers didn't all follow this. And everyone wanted to implement their own encodings for the end of <strong>ASCII</strong>.</p>
<p>These different endings for <strong>ASCII</strong> were called <strong>code pages</strong>.</p>
<h3 id="heading-what-are-ascii-code-pages">What are ASCII Code Pages?</h3>
<p><a target="_blank" href="https://www.aivosto.com/articles/charsets-codepages.html">Here's</a> a collection of over 465 different codepages! You can see that there were multiple codepages <strong>EVEN</strong> <strong>for the same language</strong>. Greek and Chinese both have multiple codepages, for example.</p>
<p>So how on EARTH were we ever going to standardise this? Or make it work between differing languages? Between the same language with different codepages? In a non English language? </p>
<p>Chinese has over 100,000 different characters. We don't even have enough spare characters for Chinese, let alone agreeing that the final characters should be Chinese ones. This isn't looking so good.</p>
<p>This problem even has its own term: <strong>Mojibake</strong>.</p>
<p>It's garbled text you may sometimes see from decoding text, but using the wrong decoding. It means <strong>character transformation</strong> in Japanese.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/12/image-171.png" alt="Image" width="600" height="400" loading="lazy">
<em>Example of completely garbled text (mojibake).</em></p>
<h2 id="heading-this-sounds-a-little-insane">This sounds a little insane...</h2>
<p>Exactly! We will have zero chance of reliably interchanging data.</p>
<p>The internet is just a huge connection of computers around the world. Imagine if all these countries decided what they each thought the standards should be. If the Greek computers only accepted Greek and the English computers only sent English...? You would just be shouting into an empty cave. No one would understand you. And no one would be able to decode the nonsense.</p>
<p>ASCII wasn't fit for real life use. In a global, connected internet, we had to evolve, or else forever deal with hundreds of codepages.</p>
<p>��� <strong>Unless you</strong> ������ <strong>fancied trying</strong> ��� <strong>to</strong> ��� <strong>read paragraphs like this.</strong> �֎֏0590֐��׀ׁׂ׃ׅׄ׆ׇ</p>
<h2 id="heading-along-came-unicode">Along Came Unicode</h2>
<p>Unicode is sometimes called the <a target="_blank" href="https://en.wikipedia.org/wiki/Universal_Coded_Character_Set">Universal Coded Character Set</a> (UCS), or even ISO/IEC 10646. But Unicode is its more common name.</p>
<p>But, this is where Unicode entered the scene to help solve the problems that <strong>encoding</strong> and <strong>code pages</strong> were causing.</p>
<p>Unicode is made up of lots of <strong>code points</strong> (mapping lots of characters from around the world to a key that all computers can reference.) A collection of <strong>code points</strong> is called a <strong>character set</strong> - which is what Unicode is.</p>
<p>We can map something abstract to a letter we want to reference. And it does every character! Even <a target="_blank" href="https://unicode.org/charts/PDF/U13000.pdf">Egyptian Hieroglyphs</a>.</p>
<p>Some people did all the hard work of mapping what each character would be (in all the languages) to a key that we could all access. They look like this:</p>
<p><strong>"Hello World"</strong> </p>
<h6 id="heading-u0048-latin-capital-letter-h">U+0048 : LATIN CAPITAL LETTER H</h6>
<p>U+0065 : LATIN SMALL LETTER E<br>U+006C : LATIN SMALL LETTER L<br>U+006C : LATIN SMALL LETTER L<br>U+006F : LATIN SMALL LETTER O<br>U+0020 : SPACE [SP]<br>U+0057 : LATIN CAPITAL LETTER W<br>U+006F : LATIN SMALL LETTER O<br>U+0072 : LATIN SMALL LETTER R<br>U+006C : LATIN SMALL LETTER L<br>U+0064 : LATIN SMALL LETTER D</p>
<p>The U+ lets us know it's the Unicode standard, and the number is what results when the binary get's transformed to numbers. It uses <a target="_blank" href="https://www.bbc.co.uk/bitesize/guides/zp73wmn/revision/1#:~:text=Hexadecimal%20(or%20hex)%20is%20a,values%20in%20binary%20and%20denary.">hexadecimal</a> notation which is just a simpler way of representing binary numbers. You don't have to worry too much about the hexadecimal here, though. </p>
<p><a target="_blank" href="https://www.babelstone.co.uk/Unicode/whatisit.html">Here's</a> a link where you can type whatever you want into the text box, and see the Unicode character encoding. Or look at all 143,859 Unicode character points <a target="_blank" href="https://unicode-table.com/en/">here</a>. You can also see where each character is from in the world!</p>
<p>I just want to be clear. At this point we have a big dictionary of <strong>code points</strong> mapping to characters. A really big <strong>character set</strong>. Nothing more. </p>
<p><strong>There's one final ingredient we need to add to our mix.</strong></p>
<h2 id="heading-unicode-transform-protocol-utf">Unicode Transform Protocol (UTF)</h2>
<p>UTF is a way we encode Unicode code points. The UTF encodings are defined by the Unicode standard, and are able to encode every single Unicode <strong>code point</strong> we need.</p>
<p>But there are different types of UTF standards. They differ depending on the amount of bytes used to encode one <strong>code point</strong>. It also depends on whether you're using <strong>UTF-8</strong> (one byte per code point), <strong>UTF-16</strong> (two bytes per code point) or <strong>UTF-32</strong> (four bytes per code point).</p>
<p>If we have these different encodings, how do we know which encoding a file will use? There's a thing called a <strong>Byte Order Mark</strong> (<strong>BOM</strong>) - sometimes referred to as an <strong>Encoding Signature</strong>. The <strong>BOM</strong> is a two-byte marker at the beginning of a file that tells what encoding the file is using.</p>
<p><strong>UTF-8</strong> is the most used on the internet, and is also specified in HTML5 as the preferred encoding for new documents, so I'll spend the most time explaining this one.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/12/image-163.png" alt="Image" width="600" height="400" loading="lazy">
<em>You can see in the <a target="_blank" href="https://en.wikipedia.org/wiki/UTF-8#/media/File:Utf8webgrowth.svg">diagram </a>even from 2012, UTF-8 was widely becoming the most used encoding. And for the web it still is.</em></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/12/image-179.png" alt="Image" width="600" height="400" loading="lazy">
_W3 <a target="_blank" href="https://w3techs.com/technologies/cross/character_encoding/ranking">diagram </a>to show how well used UTF-8 is used on a variety of websites._</p>
<h2 id="heading-what-is-utf-8-and-how-does-it-work">What is UTF-8 and How Does it Work?</h2>
<p><strong>UTF-8</strong> encodes all the Unicode code points from 0-127 in 1 byte (the same as <strong>ASCII</strong>). This means that if you were coding your program using <strong>ASCII</strong>, and your users used <strong>UTF-8,</strong> they <em>wouldn't notice anything was wrong</em>. Everything would just work. </p>
<p>Just remember how strong a selling point this is. We needed to remain <strong>ASCII</strong> backwards compatible while <strong>UTF-8</strong> was being implemented and used by everyone. It doesn't break anything currently being used.</p>
<p>Because it's called <strong>UTF-8</strong>, remember that's the minimum number of bits (8 bits being one byte!) that a <strong>code point</strong> will be. There are other Unicode characters that are stored in multiple bytes (up to 6 bytes depending on the character). This is what people mean when the encoding is called <strong>variable length</strong>.</p>
<p>It might be more, depending on the language. English is 1 byte. <a target="_blank" href="https://design215.com/toolbox/ascii-utf8.php">European (Latin), Hebrew and Arabic</a> are represented with 2 bytes. 3 bytes are used for <a target="_blank" href="https://design215.com/toolbox/utf8-3byte-characters.php">Chinese, Japanese, Korean and other Asian characters</a><em>.</em> You get the idea. </p>
<p>When you need a character to span more than one byte, you have a bit combination to identify a continuation sign, saying this character is continued over the next several bytes. So you’ll still only use one byte per character for English, but if you need a document to contain some foreign characters, you can do that too.</p>
<p>And now, wonderfully, we can all agree on what the <a target="_blank" href="https://en.wikipedia.org/wiki/Cuneiform_(Unicode_block)">Sumerian cuneiform characters</a> encoding is (𒀵 𒁷𒂅 𒐤), as well as some <a target="_blank" href="https://unicode.org/emoji/charts/full-emoji-list.html">emoji's</a> 😉😉 so we can all communicate!</p>
<p>The high level overview is: You first read the <strong>BOM</strong> so you know your encoding. You decode the file into Unicode <strong>code points</strong>, and then represent the characters from the Unicode character set into characters drawn onto the screen.</p>
<h2 id="heading-a-final-word-about-utf">A Final Word About UTF</h2>
<p>Remember, encoding is <strong>key</strong>. If I send the complete wrong encoding you can't read anything. Be aware of it when receiving or sending data. Often it is abstracted away in the tools you use everyday, but as programmers it's important to understand what is happening under the hood. </p>
<p>How do we specify our encodings, then? Because HTML is written in English, and almost all encodings can deal with English fine. We can embed it right at the top in the <code>&lt;head&gt;</code> section.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"utf-8"</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
</code></pre>
<p>It's important to do this at the very start of the <code>&lt;head&gt;</code>, as the parsing of the <a target="_blank" href="https://html.spec.whatwg.org/multipage/parsing.html#determining-the-character-encoding">HTML may have to start again</a> if the encoding it's currently using is wrong.</p>
<p>We also can get the encoding from the <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type">Content-Type</a> header from the HTTP request/ response.</p>
<p>If an HTML document doesn't contain the encoding tag, the <a target="_blank" href="https://html.spec.whatwg.org/multipage/parsing.html#determining-the-character-encoding">HTML5 spec</a> has some interesting ways it can guess the encoding called <a target="_blank" href="https://encoding.spec.whatwg.org/#bom-sniff"><strong>BOM sniffing</strong></a>. This is where it guesses the encoding from the <strong>Byte Order Mark</strong> (<strong>BOM</strong>) we discussed earlier. </p>
<h2 id="heading-so-is-that-it">So is that it?</h2>
<p>Unicode isn't finished. Like any standard, we add, remove and make new proposals to the standard. No specification is ever considered "complete".</p>
<p>There are generally 1 or 2 release a year, and you can find them <a target="_blank" href="https://unicode.org/history/publicationdates.html">here</a>.</p>
<p>Recently I read about a very interesting bug around <a target="_blank" href="https://twitter.com/availablegreen/status/1332774350613835779">Twitter rendering Russian Unicode characters incorrectly</a>. </p>
<p>If you have read this far, congratulations – it's a lot to digest.</p>
<p>I would encourage you to do one last piece of homework. </p>
<p>Look at how broken websites can really be when the encoding is wrong. I used <a target="_blank" href="https://chrome.google.com/webstore/detail/set-character-encoding/bpojelgakakmcfmjfilgdlmhefphglae?hl=en">this</a> Google Chrome extension and changed my encoding and tried to read webpages. The message was completely unclear. Try and read this article. Try and navigate Wikipedia. See <strong>Mojibake</strong> for yourself.</p>
<p>It helps to see how important encoding truly is.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/12/image-164.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In my time spent researching and trying to simplify this article, I learned about <a target="_blank" href="https://en.wikipedia.org/wiki/Michael_Everson#">Michael Everson</a>. Since 1993, he has proposed over 200 Unicode changes, and has added thousands of characters to the standard. As of 2003, he was credited as the leading contributor of Unicode proposals. He is one huge reason why Unicode is what it is. Very impressive, and he has done a great deal for the Internet as we know it.</p>
<p>I hope this has explained a good overview of why we need encodings, what problems encoding solves, and what happens when it goes wrong.</p>
<p>I share my writing on <a target="_blank" href="https://twitter.com/kealanparr">Twitter</a> if you enjoyed this article and want to see more.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ What is Endianness? Big-Endian vs Little-Endian Explained with Examples ]]>
                </title>
                <description>
                    <![CDATA[ Computers only understand binary. This means that 0's and 1's make up the language computers work with. One bit is one 0 or 1. 8 bits make up a byte. From these simple pieces we can build incredibly complex connected computer systems to render videos... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/what-is-endianness-big-endian-vs-little-endian/</link>
                <guid isPermaLink="false">66bc55fba30f8b3984dad87a</guid>
                
                    <category>
                        <![CDATA[ binary ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Computer Science ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kealan Parr ]]>
                </dc:creator>
                <pubDate>Mon, 01 Feb 2021 23:23:58 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/01/Endianness.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Computers only understand binary. This means that <code>0</code>'s and <code>1</code>'s make up the language computers work with.</p>
<p>One bit is one <code>0</code> or <code>1</code>. 8 bits make up a <strong>byte</strong>. From these simple pieces we can build incredibly complex connected computer systems to render videos, show text from around the world, and compute extremely complex algorithms.</p>
<p>Some data (some English characters like a, e, i, o and u) can be represented by one byte, but some pieces of data take multiple bytes to be represented.</p>
<p>But <strong>endianness</strong> is a fundamental part of how computers read and understand bytes.</p>
<h2 id="heading-what-is-endianness">What is Endianness?</h2>
<p>Different languages read their text in different orders. English reads from left to right, for example, while Arabic is read right to left.</p>
<p>This is exactly what <strong>endianness</strong> is for computers.</p>
<p>If my computer reads <strong>bytes</strong> from left to right, and your computer reads from right to left, we're going to have issues when we need to communicate.</p>
<p><strong>Endianness</strong> means that the bytes in computer memory are read in a certain order. </p>
<p>We won't have any issues if we never need to share information. Each computer is internally consistent for their own data. It's just the internet has enabled us to share more data than we ever have before, and our data is not always <strong>read</strong> in the same order.</p>
<p><strong>Endianness</strong> is represented two ways <strong>Big-endian</strong> (<strong>BE</strong>) and <strong>Little-endian</strong> (<strong>LE</strong>). </p>
<ul>
<li><strong>BE</strong> stores the <strong>big-end</strong> first. When reading multiple bytes the first byte (or the lowest memory address) is the biggest - so it makes the most sense to people who read left to right.</li>
<li><strong>LE</strong> stores the <strong>little-end</strong> first. When reading multiple bytes the first byte (or the lowest memory address) is the littlest -  so it makes most sense to people who read right to left.</li>
</ul>
<p>If the above doesn't make sense, that's ok – let's look at an example.</p>
<h2 id="heading-example-of-how-endianness-works">Example of How Endianness Works</h2>
<p>Let's take a number that we have to use multiple bytes to represent, and show the big-endian and little-endian ways it can be represented.</p>
<p>We'll take a number that requires three bytes to be represented in binary.</p>
<p>This may slightly oversimplify it, but I hope it serves as a helpful visual explanation.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/Decimal-number_--1-.png" alt="Image" width="600" height="400" loading="lazy">
<em>A binary example where big-endian and little-endian numbers are arranged in the order they would be read.</em></p>
<p>The 0b at the beginning is just notation to let readers know it's binary. So we know the difference between binary <code>1100</code> and 1,100 as the decimal number (one thousand, one hundred). I also used colours to hopefully make it clearer.</p>
<p>I just want to be clear that <em>bit ordering</em> is fine. There is no difference between the ordering of <strong>bits</strong>. But there is a difference in the correct ordering of <strong>bytes</strong>. I hope the above demonstrates the order of the <code>0</code>'s and <code>1</code>'s within a byte don't change. But the <strong>byte</strong> ordering does change.</p>
<p>If we only ever had to send one <strong>byte</strong> too, there'd be no issues (there aren't multiple ways to order only 1 thing). It's only an issue with a sequence of more than one <strong>byte.</strong></p>
<h2 id="heading-most-significant-byte-msbyte">Most Significant Byte (MSbyte)</h2>
<p>The terminology <strong>Most Significant Byte</strong> is a common way to describe <strong>endianness</strong> so I want to make sure I cover it thoroughly.</p>
<p>Before we begin explaining with <strong>bits</strong> and <strong>bytes</strong>, let's just do it with a decimal number.</p>
<p>If I took the decimal number 2,984, what number could you change to change the number by the smallest amount? The 4. If I change the 4 to 5, the whole number only goes up by 1.</p>
<p>But let's say you change the 2 in 2,984. It will change the number significantly and go up by a thousand.</p>
<p>This is the exact same with <strong>bytes</strong> and <strong>bits</strong>.</p>
<p>We refer to the <strong>byte</strong> holding the smallest position as the <strong>Least Significant Byte</strong> (<strong>LSbyte</strong>) and the <strong>bit</strong> holding the smallest position as the <strong>Least Significant Bit</strong> (<strong>LSbit</strong>).</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/MSbyte.png" alt="Image" width="600" height="400" loading="lazy">
<em>A diagram to illustrate that the byte containing the lowest position numbers is the least significant byte.</em></p>
<p>The byte that holds the most significant position is referred to as the <strong>Most Significant Byte</strong> (<strong>MSbyte</strong>) and the bit holding the most significant bit position is the <strong>Most Significant Bit</strong> (<strong>MSbit</strong>).</p>
<p>Now knowing this new definition, we can define <strong>BE</strong> and <strong>LE</strong> as:</p>
<ul>
<li>Big endian stores data <strong>MSbyte</strong> first</li>
<li>Little endian stores data <strong>MSbyte</strong> last</li>
</ul>
<h2 id="heading-when-might-this-be-an-issue">When might this be an issue?</h2>
<p>Endianness has to be a consideration in computing in a few different cases.</p>
<p>For example Unicode characters (the character set used to render characters on your phone, PC, TV, everywhere!) have to pass a special character byte sequence (U+FEFF BYTE ORDER MARK) called the <strong>Byte Order Mark</strong> or <strong>BOM</strong>. The <strong>BOM</strong> serves a few purposes.</p>
<p>The <strong>BOM</strong> makes the system aware:</p>
<ul>
<li>That the incoming stream is Unicode.</li>
<li>Of which Unicode character encoding is used.</li>
<li>Of the <strong>endian</strong> order of the incoming stream.</li>
</ul>
<p>Some programming languages even expect you to detail what byte order sequence is being used. So one program may use, send, and receive numbers in <strong>BE</strong> or <strong>LE</strong> depending on what you want. Swift is one example of this (the language used to do iOS development).</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/image-206.png" alt="Image" width="600" height="400" loading="lazy">
<em>The Apple documentation detailing how to get the little-endian representation of an Integer.</em></p>
<p>You can read some Swift code <a target="_blank" href="https://gist.github.com/vukcevich/fa793c8bcb55b14b6e0a0700f5f7316b">here</a> for an example of byte swapping to get little and big-endian representations of <code>Integer</code>s too.</p>
<h2 id="heading-why-is-this-even-an-issue-in-the-first-place">Why is this even an issue in the first place?</h2>
<p>It just happens that different protocols emerged and then later had to interact with one another. <strong>BE</strong> is the dominant order in any network protocols, and is referred to as <strong>network order</strong>, for example. On the other hand, most PC's are <strong>little-endian</strong>. </p>
<p>You can run a C++ code snippet online <a target="_blank" href="http://cpp.sh/524wi">here</a> to see what endian your machine is (mine is <strong>little-endian</strong>).</p>
<p><strong>Endianness</strong> has largely ceased to matter with higher level languages and abstracting away particular pieces of implementation details we don't need to worry about.</p>
<p>Another part of it is processors decide whether they are little-endian or big-endian (or can handle both - called <strong>Bi-endian</strong>) so consumer choice has driven part of what we consider "normal" in our computer systems.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>I hope this article has explained what endianness is, what big-endian and little-endian are, and that you understand these concepts more clearly now.</p>
<p>I share my writing on <a target="_blank" href="https://twitter.com/kealanparr">Twitter</a> if you enjoyed this article and want to see more.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Microprocessor's Romance With Negative Integers – The How and Why of CPU Arithmetic Design ]]>
                </title>
                <description>
                    <![CDATA[ By Vivek Agrawal One of the first things we learn about computers is that they only understand 0s and 1s, or bits.  We humans, on the other hand, communicate numbers via the decimal system. This system uses digits from 0 to 9 along with plus and minu... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/microprocessors-romance-with-integers/</link>
                <guid isPermaLink="false">66d461712472e5ed2fa07bc1</guid>
                
                    <category>
                        <![CDATA[ binary ]]>
                    </category>
                
                    <category>
                        <![CDATA[ cpu ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Mathematics ]]>
                    </category>
                
                    <category>
                        <![CDATA[ systems ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Thu, 28 Jan 2021 18:30:45 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/01/cover2.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Vivek Agrawal</p>
<p>One of the first things we learn about computers is that they only understand <strong>0s and 1s</strong>, or <strong>bits</strong>. </p>
<p>We humans, on the other hand, communicate numbers via the decimal system. This system uses digits from 0 to 9 along with plus and minus signs (+ and -) to denote positive or negative numbers. </p>
<p>Since computers can use only two digits – 0 and 1 – engineers and mathematicians back in the day designed clever techniques for representing negative numbers and for doing arithmetic with them. Let's explore the beauty of those techniques.</p>
<h2 id="heading-first-some-background-on-how-computers-work">First, some background on how computers work</h2>
<p>Software, images, text, videos, numbers and everything in between are 0s and 1s at the lowest level in our computer.</p>
<p>For images, text, videos and numbers, we have encoding schemes that decide how these stuff will get to 0s and 1s. For example, ASCII and Unicode for text.</p>
<p>The software programs we code get to 0s and 1s via compilers and assemblers. Those set of 0s and 1s known as machine code (or machine instruction) are first stored in our computer's main memory (RAM) before the processor can execute them.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/fetch-decode-exec.png" alt="A diagram showing fetch decode execute cycle" width="600" height="400" loading="lazy">
_The fetch decode execute cycle architected by Sir <a target="_blank" href="https://en.wikipedia.org/wiki/John_von_Neumann">John von Neumann</a>. Every digital computer follows this cycle to run machine code._</p>
<p>The processor starts the execution cycle by <strong>fetching</strong> the instructions from the main memory, then the control unit of the processor <strong>decodes</strong> those instructions into two parts – operation code (opcode) and operands. </p>
<p>The opcode decides the further action that needs to be performed like ADD (addition), JMP (jump), INC (increment) and so on. The operands are the values (or memory locations) on which that operation will be performed. </p>
<p>The decoded instructions are sent to the Arithmetic and Logic Unit (ALU) for <strong>execution</strong>. In the ALU, the instruction is executed based on the opcode on the operands and the result is stored back in the memory. </p>
<p>For example, the assembly code <code>ADD eax, 42</code> is first turned into machine code (0s and 1s) by the assembler. Then it is stored into the main memory before the fetch-decode-execute cycle can begin. </p>
<p>When the fetching of the machine code for <code>ADD eax, 42</code> from the memory finishes, the instruction is decoded. The decoded output says that the opcode is <code>ADD</code> and the operands are <code>eax</code> and <code>42</code>.  </p>
<p><code>eax</code> is a register – a memory location inbuilt into the processor that can be accessed instantaneously by the processor. The <code>eax</code> register is called an accumulator in most processors. </p>
<p>The <code>ADD eax, 42</code> assembly code is designed to add 42 to the current value of the <code>eax</code> register (accumulator) and stores that sum in <code>eax</code>. It is <code>eax = eax + 42</code>. </p>
<p>Suppose that currently <code>eax</code> is 20. This means that the value of <code>eax</code> after executing <code>ADD eax, 42</code> will be 20 + 42 = 62.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/Edvac-1.jpg" alt="Two men operating EDVAC computer" width="600" height="400" loading="lazy">
<em>EDVAC was one of the earliest electronic binary computer built for the U.S. Army's Ballistics Research Laboratory. (<a target="_blank" href="https://en.wikipedia.org/wiki/EDVAC#/media/File:Edvac.jpg">Image source</a>, Public Domain).</em></p>
<p>The design of early computers such as EDVAC started with the desire to make tedious mathematical calculations easier and faster. </p>
<p>The whole responsibility of making computers compute lay on the shoulders of adders – circuits that add two numbers. This is because sophisticated operations like subtraction, multiplication, and division utilize adders in their circuits.  </p>
<p>Ultimately computers are just a fast arithmetic machines with logic capabilities. Understanding the challenges and the beauty of binary arithmetic design (of positive and especially negative integers) is <strong>one of the most fundamental concepts in a computer processor</strong>. </p>
<p>Let's first see how decimal numbers are represented in binary and how to add two binary values. Then we will start exploring the beauty.      </p>
<h2 id="heading-how-the-binary-system-works">How the binary system works</h2>
<p>If I tell you to read out <code>872500</code>, you will likely say <strong>872.5K</strong>. Let's take a look at how our minds do this.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/decimal_sys_img-2.png" alt="The procedure we humans use to read decimal numbers" width="600" height="400" loading="lazy"></p>
<p>We assign the one's place to the first digit from the right, then the ten's place to the second from the right, the hundredths to the third, and so on, growing each time by power of 10. </p>
<p>These powers of 10 in each place are the weights of the places. The weight of the hundredth place is one hundred. We multiply the digits in each place by their place's weight and sum them all up to get a complete number.</p>
<p>In the above diagram, you can see that the growth of each place's weight is in the powers of 10, starting from <code>10^0</code> and going through <code>10^5</code>. That's why decimals are called a base ten system.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/binary_sys_img-2.png" alt="The procedure via which computers read binary codes" width="600" height="400" loading="lazy"></p>
<p>In binary, each place's weight grows by a power of 2. This means that the place's weight starts from <code>2^0</code> and ends at <code>2^something</code>. That's the only difference.</p>
<p><code>00110101</code> in decimal translates to 53. Computers interpret binary in the same way as we humans interpret decimals, that is multiplying each place's digit by its weight and summing them up.</p>
<h3 id="heading-how-to-add-1s-and-0s">How to add 1s and 0s</h3>
<p>Addition works in binary pretty much the same way as it's done in decimals. Let's see that through an example. We'll add two binary numbers: <code>1101</code> (13) and <code>1100</code> (12).</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/1-1.png" alt="Step one of addition of 1101 and 1100 that is one plus zero" width="600" height="400" loading="lazy"></p>
<p>As we do in the decimal system, we start from the one's place (<code>2^0</code>). Adding 1 and 0 gives us 1. So we put a 1 there. Stay with me and you'll get the whole picture.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/2-1.png" alt="Step two of addition of 1101 and 1100 that is zero plus zero" width="600" height="400" loading="lazy"></p>
<p>0 plus 0 is 0. Moving on.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/3-2.png" alt="Step three of addition of 1101 and 1100 that is one plus one" width="600" height="400" loading="lazy"></p>
<p>1 plus 1 is 2. And 2 in binary is represented as <code>10</code>. We carry 1 to the next place and keep 0 as a result of the current place we are in. Isn't this the same as exceeding 9 in a place in decimal addition? </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/4-1.png" alt="Step four of addition of 1101 and 1100 that is one plus one plus one of carry row" width="600" height="400" loading="lazy"></p>
<p>We have two 1s there and one 1 that was carried forward from the previous place, so there are a total of three 1s. Their sum will be 3, and in binary 3 is <code>11</code> so we write <code>11</code>. The final result comes out to be <code>11001</code> or 25 in decimal form, which is indeed 13 + 12.</p>
<p>The above computation assumes that we have five bits available to store the result. If a 4-bit computer does this addition, then it will only have four bits available to store the result. </p>
<p>That fifth bit will be called an <strong>overflow</strong> in 4-bit computers. In integer arithmetic, the overflow bit is ignored or discarded. So we would have got <code>1001</code> (9) as our result if we were using a 4-bit computer.</p>
<h2 id="heading-the-beauty-of-binary-arithmetic-design">The beauty of binary arithmetic design</h2>
<p>Two important terms we need to understand before we move forward are <strong>least significant bit</strong> and <strong>most significant bit</strong>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/lsb_msb-1.png" alt="The least significant and the most significant bit in a byte word" width="600" height="400" loading="lazy"></p>
<p>The bit on the <strong>rightmost is the least significant bit</strong> because it has the smallest place weight (<code>2^0</code>). And the bit on the <strong>leftmost is the most significant bit</strong> as it has the highest place weight (<code>2^7</code>).</p>
<p>If the world only had positive numbers, then this would have been the end of this article (because we have already learned how to represent decimals in binary and how to add them in binary). </p>
<p>Thankfully, we have negative numbers, too. </p>
<p>The beauty of the CPU's arithmetic design rests in negativeness. </p>
<p>So how do computers represent negative numbers, and how does arithmetic on negative numbers work? Let's see an encoding approach to this problem.</p>
<p>Please note that in the below sections we will be working with a 4-bit computer to understand the concepts, meaning the fifth bit will be treated as an overflow. The same principles apply to all the CPU architectures like 16-bit, 32-bit or 64-bit to do arithmetic.</p>
<h3 id="heading-the-sign-magnitude-encoding-approach">The sign magnitude encoding approach</h3>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/sign-bit01-1.png" alt="The leftmost bit in a four bit binary is the sign bit and the remaining three represents magnitude" width="600" height="400" loading="lazy"></p>
<p><code>1101</code> in decimal form would be -5 in this encoding scheme. The leftmost or the most significant bit is the sign bit. It tells the processor about the sign of the number – that is, whether the number is positive or negative. </p>
<p><code>0</code> in the sign bit represents a positive value and <code>1</code> represents a negative value. The remaining bits tells us the actual magnitude.</p>
<p>In <code>1101</code>, the sign bit is <code>1</code>, so the number is negative. <code>101</code> equals 5 in decimal. So <code>1101</code> will compute to -5 in decimal.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/4bit-1.png" alt="All numbers that are possible with four bits using sign bit encoding scheme" width="600" height="400" loading="lazy">
<em>All possible numbers that can be represented by four bits with sign bit encoding scheme</em></p>
<p>In the above diagram you can see all the integers that can be represented by four bits using this encoding approach. All looks good up to this point. </p>
<p>But if we look closely, we can see a very serious design issue in this encoding scheme. Let's face that issue.</p>
<p>Let's add a positive and a negative number. For example we'll add +4 and -1. Our answer should be <code>(+4) + (-1) = (+3)</code> that is <code>0011</code>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/signbitproblem.png" alt="Adding +4 and -1 in binary resulting in -5 when using sign bit encoding scheme" width="600" height="400" loading="lazy"></p>
<p>See, the result is <code>1101</code> (-5). The actual answer should be <code>0011</code> (+3). If we were to implement this approach on a processor then we would need to add logic to deal with this issue, and engineers hate additional complexity in their logic. </p>
<p>As we add more circuits, the power consumption increases and performance suffers. </p>
<p>This might sound like a trivial issue for modern transistor-based computers. </p>
<p>But think of early computers like EDVAC which was run on thousands of vacuum tubes consuming power in kilowatts operated by hundreds of people a day. And the government spent millions to build them. </p>
<p>In those days putting additional circuits and vacuum tubes meant thousands of dollars and serious maintenance trouble.   </p>
<p>So engineers had to think of a smarter encoding design. </p>
<p>Now, the time has come to reveal the beauty that will tackle this problem and make our system simpler, more performant, and less power hungry.</p>
<h3 id="heading-a-beautiful-encoding-system-enters-and-the-cpu-shines">A beautiful encoding system enters and the CPU shines ❤️</h3>
<p>In this encoding scheme, like in the previous one, the leftmost bit acts as a sign bit – but with some art involved to represent negative numbers.</p>
<p>The positive numbers are represented in the exact same way as the previous encoding scheme: a leading <code>0</code> followed by remaining bits for the magnitude. For example, in this encoding scheme too, 6 will be represented as <code>0110</code>. </p>
<p>To represent a negative number, a two step math process is run in its positive counterpart. Meaning to represent -6 we will do a two step math process on +6 to get to -6 in binary. </p>
<p>Let's see how -6 will encode to binary:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/invert-1.png" alt="An illustration of bits getting inverted" width="600" height="400" loading="lazy"></p>
<p>In the previous sign magnitude approach, to calculate the negative of +6, we would have simply changed the sign bit from <code>0</code> to <code>1</code>. <code>0110</code> (+6) would become <code>1110</code> (-6).</p>
<p>In this new encoding scheme, we first invert the bits. Changing zeros to ones and ones to zeros. <code>0110</code> (+6) becomes <code>1001</code>. Inverting the bits is called "one's complement", so here we have calculated one's complement of <code>0110</code> resulting in <code>1001</code>. Then...</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/-6-1.png" alt="Adding binary one to 1001, resulting in 1001" width="600" height="400" loading="lazy"></p>
<p>We add <code>0001</code> (+1) to the one's complement we got from step one (<code>1001</code>). The result <strong><code>1010</code> will be the binary representation of -6. This encoding scheme is called two's complement.</strong> So keep in mind that calculating two's complement of a positive integer gives us its negative counterpart.  </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/twocomplementshort-4.png" alt="Steps of calculation of the two's complement of 0110" width="600" height="400" loading="lazy"></p>
<p>Inverting bits gives us the one's complement. Adding one to the one's complement gives us the two's complement of the original bits we started with. Simple, right?</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/2complement.png" alt="All possible numbers that can be represented by four bits with two's complement encoding scheme" width="600" height="400" loading="lazy">
<em>All possible numbers that can be represented by four bits with two's complement encoding scheme</em></p>
<p>Now, let's see why this encoding scheme is so beautiful. We'll add <code>0100</code> (+4) and <code>1111</code> (-1).</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/2complesolution.png" alt="Addition of 0100 and 1111 when using two's complement encoding scheme" width="600" height="400" loading="lazy"></p>
<p>See, we get the accurate result with the two's complement encoding scheme. Now we can add integers without worrying about their signs. </p>
<p>We've learned how a negative integer can be represented in 0s and 1s via two's complement encoding. Now suppose we execute <code>ADD eax, -3</code> and the current value in the eax register is -1. So the value in eax after the execution of <code>ADD eax, -3</code> will be -4 (which is <code>1100</code> in two's complement encoding). </p>
<p>When the operating system retrieves <code>1100</code> from eax to present the result to the user, how does the operating system decode <code>1100</code> to decimal? Or suppose if we as a programmer come across <code>1100</code>, how can we figure out what number <code>1100</code> represents? </p>
<p>Of course we cannot keep on calculating two's complement of each positive integer to see when we hit <code>1100</code>. That will be too slow.   </p>
<p>Programmers and the OS use a beautiful property of two's complement to decode the binary into decimal. </p>
<p>When we calculate two's complement of a positive number, we get its negative counterpart. Well, <strong>the reverse is also true</strong> – which means calculating two's complement of a negative number will give us its positive counterpart. We will see the why of this in a minute. </p>
<p>First, let's understand how the OS or a programmer will decode <code>1100</code> to decimal.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/2complementexample-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>On retrieving <code>1100</code> from the eax register, the OS sees <code>1</code> as sign bit that signals that the integer is negative. Two's complement of <code>1100</code> is calculated that gives the positive counterpart of <code>1100</code> which comes out as <code>0100</code> (+4). The OS then prepends a negative sign on the positive counterpart and returns the final answer as -4. Re-read this paragraph once again and you'll get a better understanding.</p>
<p>Then the CPU smiles and says goodbye to the beauty for today ;) </p>
<p>The CPU has gone to its house to meet its mother. Now we have plenty of time to discuss the inner workings of the art of two's complement.</p>
<h2 id="heading-why-and-how-does-twos-complement-encoding-work">Why and how does two's complement encoding work?</h2>
<p>If I tell you to find the negative of a number, say +42, what's the simplest way to find the negative of +42? </p>
<p>Arguably, the simplest way is to subtract the number from 0, right? <code>0 - (+42) = -42</code>. If we repeat this, we get back to the positive value, <code>0 - (-42) = +42</code>. This is all the math that two's complement is built upon.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/01/zerominusnum-1.png" alt="Subtracting 0101 from 10000 resulting in 1011" width="600" height="400" loading="lazy"></p>
<p>We are doing <code>10000</code> (0 in decimal since the leftmost 1 is an overflow) minus <code>0101</code> (+5). We get <code>1011</code> that is -5 in decimal in two's complement encoding. Ignore how subtraction is done. That's not important. Understanding the intuition behind two's complement is important.</p>
<p><code>10000</code> can be written as <code>1111 + 0001</code> (try adding these two, you will get <code>10000</code>). So actually we are doing:</p>
<pre><code>        <span class="hljs-number">10000</span>       -   <span class="hljs-number">0101</span>
=&gt;  (<span class="hljs-number">1111</span> + <span class="hljs-number">0001</span>)   -   <span class="hljs-number">0101</span>
</code></pre><p>Rearranging the above equation we can write it as:</p>
<pre><code>    (<span class="hljs-number">1111</span> + <span class="hljs-number">0001</span>)  -  <span class="hljs-number">0101</span>
=&gt;  (<span class="hljs-number">1111</span> - <span class="hljs-number">0101</span>)  +  <span class="hljs-number">0001</span>

Step <span class="hljs-number">1</span>: subtract <span class="hljs-number">0101</span> <span class="hljs-keyword">from</span> <span class="hljs-number">1111</span>

        <span class="hljs-number">1</span> <span class="hljs-number">1</span> <span class="hljs-number">1</span> <span class="hljs-number">1</span>
       <span class="hljs-number">-0</span> <span class="hljs-number">1</span> <span class="hljs-number">0</span> <span class="hljs-number">1</span>
       ---------
        <span class="hljs-number">1</span> <span class="hljs-number">0</span> <span class="hljs-number">1</span> <span class="hljs-number">0</span>

       see, subtracting <span class="hljs-number">0101</span> <span class="hljs-keyword">from</span> <span class="hljs-number">1111</span> is equivalent 
       to inverting the bits <span class="hljs-keyword">of</span> <span class="hljs-number">0101</span>, <span class="hljs-keyword">as</span> we got <span class="hljs-number">1010</span> <span class="hljs-keyword">as</span> a result. 



Step <span class="hljs-number">2</span>: add <span class="hljs-number">0001</span> to the above result  

        <span class="hljs-number">1</span> <span class="hljs-number">0</span> <span class="hljs-number">1</span> <span class="hljs-number">0</span>  ---&gt; result <span class="hljs-keyword">of</span> step <span class="hljs-number">1</span>
       +<span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">1</span>
       ---------
        <span class="hljs-number">1</span> <span class="hljs-number">0</span> <span class="hljs-number">1</span> <span class="hljs-number">1</span>      

       we get <span class="hljs-number">1011</span> that is <span class="hljs-number">-5</span> <span class="hljs-keyword">in</span> two<span class="hljs-string">'s complement encoding.</span>
</code></pre><p>Did you see that the two's complement system fundamentally does 0 minus the number? Inverting the bits and adding one is a fast and clever way to subtract the number from 0. </p>
<p>This is the reason we get the positive of a negative number and negative of a positive number when we calculate its two's complement – because we are actually subtracting the number from 0 (<code>0 - number</code>). </p>
<p>Computers in the 1900s used to have just the addition arithmetic logic because the two's complement encoding scheme is so beautiful that subtraction can easily be performed. </p>
<p>For example, to subtract 12 from 100, the CPU computes two's complement of +12 that produces -12 then we add -12 to 100 giving us the required output.  </p>
<p>Why don't we directly subtract from 0 to find the negative of a number or vice versa in binary? </p>
<p>Because subtraction is a slow and complicated process (thanks to borrowing) so our computer will need an expensive subtraction circuit if we go that way. Imagine subtracting from 0 every time we want to represent a negative integer. It'll be a nightmare for us and for our computers as well!</p>
<p>The two's complement encoding is a more performant solution, leads to a simple circuit design, and saves a lot of money. This is because we don't need an expensive circuit for subtraction and there's no additional logic to deal with the arithmetic of + and - integers. Just plain addition and we get to do both – add and subtract. </p>
<p>So let's appreciate our computer designers for this beautiful encoding scheme – <strong>the two's complement ❤️.</strong>  </p>
<h2 id="heading-final-words">Final words</h2>
<p>I promised myself that I would never charge for any learning material I produce. Whatever I do for education, whether it be a simple article or a course or an ebook, will always be 100% free and open. </p>
<p>I post useful resources and share meaningful thoughts on <a target="_blank" href="https://twitter.com/vkwebdev">my Twitter account</a>. You can follow me there and send me a DM if you learned something new from this article. It'll make my day :)</p>
<p>Every developer, every author, and every human being learns from someone. I believe the people and resources we learn from should be cited and spread. This encourages those good ones to do more for all of us. So here are my good ones.</p>
<p><a target="_blank" href="https://www.youtube.com/watch?v=zxb8DvLUqcM">Animesh of mycodeschool</a> taught me many programming concepts better than anyone else including the concepts I wrote about in this article.</p>
<p><a target="_blank" href="https://jaenis.ch/">André Jaenisch</a>, my mentor and friend, without his reviewing efforts and constant support I would not have written this article.</p>
<p>Happy learning!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How does String.padStart actually work? ]]>
                </title>
                <description>
                    <![CDATA[ By Yazeed Bzadough Previously, I shared my usage of padStart to elegantly replace what would’ve been loads of if statements. This magical method threw me off my rocker. I simply couldn’t believe it existed. What it does Mozilla Developer Network (MDN... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-does-string-padstart-actually-work-abba34d982e/</link>
                <guid isPermaLink="false">66d461957df3a1f32ee7f8be</guid>
                
                    <category>
                        <![CDATA[ binary ]]>
                    </category>
                
                    <category>
                        <![CDATA[ bitwise ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Sun, 31 Dec 2017 20:40:59 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*jANUTARhf9DaSPo_FdobsQ.gif" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Yazeed Bzadough</p>
<p><a target="_blank" href="https://medium.com/@yazeedb/youtube-durations-in-4-lines-of-javascript-e9a92cea67a4">Previously</a>, I shared my usage of <code>padStart</code> to elegantly replace what would’ve been loads of <code>if</code> statements. This magical method threw me off my rocker. I simply couldn’t believe it existed.</p>
<h3 id="heading-what-it-does">What it does</h3>
<p><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart">Mozilla Developer Network (MDN) Docs</a>:</p>
<blockquote>
<p>The <code>padStart()</code> method pads the current string with another string (repeated, if needed) so that the resulting string reaches the given length. The padding is applied from the start (left) of the current string.</p>
</blockquote>
<p>Keep <strong>prepending a string</strong> to <strong>another string</strong> until the <strong>target length</strong> is met.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*XgjBHs6faLKurpx6WOxmaQ.png" alt="1*XgjBHs6faLKurpx6WOxmaQ" width="1264" height="278" loading="lazy"><img src="https://cdn-media-1.freecodecamp.org/images/1*kvWWV9-Le3akATlMGLFIUA.png" alt="1*kvWWV9-Le3akATlMGLFIUA" width="1236" height="532" loading="lazy"></p>
<p>If the length is already less than the original string’s length, nothing happens.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*tmVv1tdy9Ye099ca2YBD4w.png" alt="1*tmVv1tdy9Ye099ca2YBD4w" width="1164" height="276" loading="lazy"></p>
<p>And since <code>padStart</code> returns a string, we can chain its methods.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*LhQzpSiSSlvTDkcHyL8HtA.png" alt="1*LhQzpSiSSlvTDkcHyL8HtA" width="1064" height="374" loading="lazy"></p>
<p>See? 1, 2, 3, 4, and 5 are all less than or equal to <code>world</code>'s length of 5, so <code>padStart</code> doesn’t do anything.</p>
<h3 id="heading-browser-support">Browser support</h3>
<p>Unfortunately, support’s currently “meh”</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*OsJkuMt7gxC407zlxv1Imw.png" alt="1*OsJkuMt7gxC407zlxv1Imw" width="1060" height="348" loading="lazy">Desktop support<img src="https://cdn-media-1.freecodecamp.org/images/1*dtwqtBR1j9vDDi2AkpP61Q.png" alt="1*dtwqtBR1j9vDDi2AkpP61Q" width="1260" height="394" loading="lazy">Mobile support</p>
<p>You can either use <a target="_blank" href="http://babeljs.io/#polyfill">babel-polyfill</a> or <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart#Polyfill">the polyfill by MDN</a>.</p>
<p>Here’s MDN’s polyfill.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*Zf4kxLLpi3CsYN94Nl4axQ.png" alt="1*Zf4kxLLpi3CsYN94Nl4axQ" width="1600" height="753" loading="lazy"></p>
<h3 id="heading-some-points-of-interest">Some points of interest:</h3>
<ul>
<li><strong>Prototypes</strong> (lines 1 and 2)</li>
<li><strong>Bitwise operators</strong> (line 4)</li>
<li><code>padString.repeat</code> (line 14)</li>
<li><code>padString.slice</code> (line 17)</li>
</ul>
<p>I’m down to step through them if you are ?</p>
<p>Lines 1 and 2 aren’t that bad: “If <code>padStart</code> isn’t supported by the browser, let’s create our own <code>padStart</code> and add it” (that’s polyfill-ing in a nutshell).</p>
<p>A common way to check a method’s browser support is to inspect its object’s prototype. Since <code>padStart</code> is a string method, it should exist on <code>String.prototype</code>.</p>
<p>My old version of Safari doesn’t support <code>padStart</code>.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*8zmT7mTVUn2Q4MqunHXicg.png" alt="1*8zmT7mTVUn2Q4MqunHXicg" width="1306" height="338" loading="lazy">My Safari’s padStart support</p>
<p>But my Chrome and Firefox do.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*paNRJ_6YQ9ThHHxkwEpZwA.png" alt="1*paNRJ_6YQ9ThHHxkwEpZwA" width="1360" height="322" loading="lazy">Chrome padStart support<img src="https://cdn-media-1.freecodecamp.org/images/1*jn3Exskqn_8EAQORGs_FKg.png" alt="1*jn3Exskqn_8EAQORGs_FKg" width="804" height="204" loading="lazy">Firefox padStart support</p>
<p>Consider this safety check on line 1</p>
<pre><code class="lang-js"><span class="hljs-keyword">if</span> (!<span class="hljs-built_in">String</span>.prototype.padStart) {
}
</code></pre>
<p>That <code>if</code> statement would only return <code>true</code> in my old Safari. It returns <code>false</code> in Chrome/Firefox, so no polyfill-ing happens.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*Zf4kxLLpi3CsYN94Nl4axQ.png" alt="1*Zf4kxLLpi3CsYN94Nl4axQ" width="1600" height="753" loading="lazy"></p>
<p>Moving on, line 2 creates a new function called <code>padStart</code> and assigns it to <code>String.prototype.padStart</code>. Because of JavaScript’s inheritance model, any string created afterwards can use <code>padStart</code>.</p>
<p>This function takes two parameters</p>
<p>1. <code>targetLength</code>: How long should the resulting string be?</p>
<p>2. <code>padString</code>: What are we padding it with?</p>
<p>Let’s shower this code with <code>debugger</code> statements.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*ttFL0luCSlQzdyOh-lpzOA.png" alt="1*ttFL0luCSlQzdyOh-lpzOA" width="1600" height="1175" loading="lazy"></p>
<p>I also removed that <code>if</code> statement from line 1, so even the native <code>String.prototype.padStart</code> will be overridden by this function–makes it useful if you want to debug in Chrome.</p>
<p><strong><em>Don’t override prototypes in production, kids!</em></strong></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*srYXzRnU1Qt46J3x91vKjQ.png" alt="1*srYXzRnU1Qt46J3x91vKjQ" width="599" height="331" loading="lazy"></p>
<p>Using our initial example</p>
<pre><code class="lang-js"><span class="hljs-string">'world'</span>.padStart(<span class="hljs-number">11</span>, <span class="hljs-string">'hello '</span>);
</code></pre>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*lFrlt-xxEwyByiesDNqHpw.png" alt="1*lFrlt-xxEwyByiesDNqHpw" width="1600" height="509" loading="lazy"></p>
<p>Check out line 2. We see that <code>targetLength</code> and <code>padString</code> made their way into our function. No craziness yet, but it’s coming. I’ve been avoiding line 5 long enough.</p>
<h3 id="heading-bitwise-operators">Bitwise operators</h3>
<p>The comment above line 5 briefly describes its purpose: “If <code>targetLength</code> is a number, round it down. If it’s not a number, make it 0”.</p>
<p><strong>Bitwise operators</strong> make this possible.</p>
<p><code>targetLength &gt;&gt; 0;</code></p>
<p>This operator <code>&gt;&gt;</code> is known as a <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Right_shift">sign-propagating right shift</a> (LOLWUT?).
You use it with two numbers</p>
<p><code>a &gt;&gt; b</code></p>
<p><strong>What this does:</strong></p>
<ol>
<li><code>a</code> is converted into binary (<a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Signed_32-bit_integers">details here</a>).</li>
<li>Binary <code>a</code> gets <em>right-shifted</em> <code>b</code> times.</li>
</ol>
<p>Our <code>targetLength</code> is 11–that’s 1011 in binary (here’s a <a target="_blank" href="https://www.binaryhexconverter.com/binary-to-decimal-converter">converter</a> if you don’t believe me ?).</p>
<p>A side effect of converting to binary is that numbers get rounded down and <em>most</em> non-numbers become 0.</p>
<p>Try the following examples</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*G9R342JuTLzAhZ3zXB5qYw.png" alt="1*G9R342JuTLzAhZ3zXB5qYw" width="914" height="563" loading="lazy"></p>
<p>See? Fractions become whole numbers. Non-numbers become 0, with one notable exception…</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*S5QRnVnjsJaP6LSR-f1yVg.png" alt="1*S5QRnVnjsJaP6LSR-f1yVg" width="914" height="231" loading="lazy"></p>
<p>Binary is just 1’s and 0’s, right? Those 1’s and 0’s represent “on” and “off” switches–<code>true</code> and <code>false</code>. <code>true</code>'s binary form is 1, and <code>false</code>'s binary form is 0. Just keep that in mind.</p>
<p>So now that we’ve “sanitized” <code>targetLength</code>, we begin the right-shifting.</p>
<p>Right-shift means you move each bit to the right <code>n</code> times. That’s it.</p>
<p>Here’s a PowerPoint visualization of <code>11 &gt;&gt; 1</code> (I forgot how great PowerPoint actually is).</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*jANUTARhf9DaSPo_FdobsQ.gif" alt="1*jANUTARhf9DaSPo_FdobsQ" width="600" height="338" loading="lazy"></p>
<p>Turn 11 into 1011 and right-shift it 1 time. Your end result is 101, which is 5 in binary.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*hWhIMjgIzV8HsBHsoqaXkw.png" alt="1*hWhIMjgIzV8HsBHsoqaXkw" width="684" height="146" loading="lazy"></p>
<p>But our code says <code>targetLength &gt;&gt; 0</code>.</p>
<h3 id="heading-so-were-right-shifting-0-times">So we’re right-shifting 0 times…</h3>
<p>The whole point of right-shifting 0 times is to abuse that side effect of converting <code>targetLength</code> into binary. We don’t actually want to shift anything because that’ll change the value.</p>
<h3 id="heading-moving-on">Moving on</h3>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*9fp5LQLp8M02XXNypggPAw.png" alt="1*9fp5LQLp8M02XXNypggPAw" width="1600" height="641" loading="lazy"></p>
<p>Jump to line 7’s <code>debugger</code> now. <code>targetLength</code> has been sanitized. <strong>Next!</strong></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*5olkuOlk90Alu9tVfbjS9Q.png" alt="1*5olkuOlk90Alu9tVfbjS9Q" width="1600" height="592" loading="lazy"></p>
<p><strong>Line 11.</strong></p>
<pre><code class="lang-js">padString = <span class="hljs-built_in">String</span>(padString || <span class="hljs-string">' '</span>);
</code></pre>
<p>If we don’t provide a <code>padString</code> argument, it defaults to an empty space. I actually never noticed until now.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*esccGoVlxpemIBmMunjmXA.png" alt="1*esccGoVlxpemIBmMunjmXA" width="1600" height="637" loading="lazy"></p>
<p><strong>Line 17.</strong></p>
<p>Notice how line 13 had another safety check, “If the original string’s length is greater than <code>targetLength</code>, don’t do anything. Just return the original string”</p>
<p>That makes sense because if our <code>targetLength</code> is 1, but the string is already 10 characters, what’s the point? We demonstrated that earlier with</p>
<pre><code class="lang-js"><span class="hljs-comment">// just returns 'world'</span>
<span class="hljs-string">'world'</span>.padStart(<span class="hljs-number">0</span>, <span class="hljs-string">'hello '</span>);
</code></pre>
<p>Line 18 determines how many <em>more</em> characters we need by subtracting <code>targetLength</code> from the original string’s length. We need 6, in this case.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*fNa4w2qk360VICQLqvp6jQ.png" alt="1*fNa4w2qk360VICQLqvp6jQ" width="1600" height="595" loading="lazy"></p>
<p><strong>Line 27.</strong></p>
<p>We skipped that <code>if</code> statement on line 20 because <code>targetLength</code> and <code>padString.length</code> just happened to be the same, but we’ll revisit that soon.</p>
<p>For now, we’re stopped right before line 29. Let’s break it up.</p>
<pre><code class="lang-js">padString.slice(<span class="hljs-number">0</span>, targetLength);
</code></pre>
<p>The good old <code>String.prototype.slice</code> method.</p>
<p><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice"><strong>MDN Docs</strong></a>:</p>
<blockquote>
<p>The <code>slice()</code> method extracts a section of a string and returns it as a new string.</p>
</blockquote>
<p>It’s index-based, so we’re starting at index 0 of <code>padString</code>, and grabbing the amount of characters equal to <code>targetLength</code>. It’s kind of like</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*5fgldncMrn1M42TDNexc5w.png" alt="1*5fgldncMrn1M42TDNexc5w" width="862" height="790" loading="lazy"></p>
<p>Return that sliced <code>padString</code> combined with the original string, and you’re done!</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*dPcP4geY5bM3H_Qu53rF3Q.png" alt="1*dPcP4geY5bM3H_Qu53rF3Q" width="1250" height="146" loading="lazy"></p>
<h3 id="heading-almost-done"><em>Almost</em> done</h3>
<p>I’d normally conclude here, but we haven’t explored that <code>if</code> statement on line 20. To make sure we hit it this time, let’s try another earlier example</p>
<pre><code class="lang-js"><span class="hljs-string">'yo'</span>.padStart(<span class="hljs-number">20</span>, <span class="hljs-string">'yo'</span>);
</code></pre>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*xMe4-5cz9E4TcaxRV-OpCw.png" alt="1*xMe4-5cz9E4TcaxRV-OpCw" width="1600" height="395" loading="lazy"></p>
<p>I skipped to line 20 because we already know what happens up to this point.</p>
<p><code>if (targetLength &gt; padString.length)</code></p>
<p><code>targetLength</code> is 18, and <code>padString</code> is <code>'yo'</code>, with 2 as its length.
18 &gt; 2, so what next?</p>
<pre><code class="lang-js">padString += padString.repeat(targetLength / padString.length);
</code></pre>
<p>Remember, <code>padStart</code> returns a <em>sliced</em> <code>padString</code> + original string. If you want to pad <code>'yo'</code> with <code>'yo'</code> until it’s 20 characters long, you’ll have to repeat many times. This is where that logic happens, using <code>padString.repeat</code>.</p>
<p><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat"><strong>MDN Docs</strong></a>:</p>
<blockquote>
<p>The <code>repeat()</code> method constructs and returns a new string which contains the specified number of copies of the string on which it was called, concatenated together.</p>
</blockquote>
<p>So it copy/pastes the string <code>n</code> times.</p>
<p>In order to find out how many repeats we need, divide <code>targetLength</code> by <code>padString.length</code>.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*8uNfkR56h7AhooHJILFSJQ.png" alt="1*8uNfkR56h7AhooHJILFSJQ" width="902" height="142" loading="lazy"></p>
<p>Repeat <code>'yo'</code> 9 times and get a string of <code>'yo'</code>s that is 18 characters long. Add that to your original <code>'yo'</code>, and your final count is 20 characters.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*0A9siQbKWnKn6cFuidfNMQ.png" alt="1*0A9siQbKWnKn6cFuidfNMQ" width="1284" height="408" loading="lazy"></p>
<p>Mission accomplished. Until next time!</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
