<?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: andresvanegas19</title>
    <description>The latest articles on DEV Community by andresvanegas19 (@andresvanegas19).</description>
    <link>https://dev.to/andresvanegas19</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%2F325998%2F89a1664a-a96c-4754-8807-d8eb27687600.jpeg</url>
      <title>DEV Community: andresvanegas19</title>
      <link>https://dev.to/andresvanegas19</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/andresvanegas19"/>
    <language>en</language>
    <item>
      <title>Computer storage for numbers</title>
      <dc:creator>andresvanegas19</dc:creator>
      <pubDate>Sun, 29 Mar 2020 05:34:28 +0000</pubDate>
      <link>https://dev.to/andresvanegas19/computer-storage-for-numbers-1h53</link>
      <guid>https://dev.to/andresvanegas19/computer-storage-for-numbers-1h53</guid>
      <description>&lt;p&gt;Computers use a binary system (0 and 1) since they are managed by electricity, This means that using the current of electricity instructions are given which is expressed with a 1 a &lt;code&gt;YES&lt;/code&gt; and with a 0 a &lt;code&gt;NO&lt;/code&gt;. In this way, computers will process information more quickly.&lt;/p&gt;




&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                            1 == "YES" &amp;amp;&amp;amp; 0 == "NO"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  How we represent numbers on the computer?
&lt;/h2&gt;

&lt;p&gt;To get a clear idea about binary numbers and how to convert binary numbers to decimal numbers (&lt;a&gt;This link is useful to understand&lt;/a&gt;) since you have a clear concept of how numbers are represented mathematically with 1 and 0 let’s go with the examples.&lt;/p&gt;

&lt;p&gt;10 is equivalent to 1010 then the computer needs to represent that 1010, so to speak to be easy to understand the machine opens several boxes to keep that number, that number is not only 1010, 1010 the machine will see it reflected as a &lt;code&gt;0000000000001010&lt;/code&gt;, this happens because the computer handles a 34-bit system that would be our 34 boxes to store our 10, In conclusion, we have a number 10 that we need the computer to recognize then this opens a space of 34 boxes (bits) so that she can store the number 10.&lt;/p&gt;

&lt;p&gt;An important fact of this binary number is that the last number will take it if it is negative or positive, this means that if we have a number like this &lt;code&gt;11111111110110&lt;/code&gt; means that we will have a number (-10) later we will explain the -10 why.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mathematical operations
&lt;/h2&gt;

&lt;p&gt;Mathematical operations with binaries are important to clarify the last subject of two’s complement, then if we add two binary numbers this is going to be the result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;  &lt;span class="mo"&gt;0000000000001010&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mo"&gt;0000000000001010&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;----------------&lt;/span&gt;
  &lt;span class="mo"&gt;0000000000010100&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;  &lt;span class="mo"&gt;0000000000000101&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mo"&gt;0000000000001010&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;----------------&lt;/span&gt;
  &lt;span class="mo"&gt;0000000000001111&lt;/span&gt; &lt;span class="o"&gt;=&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now to the subtraction of binaries, we know that a negative number represented in binary the last digit will have (1)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;  &lt;span class="mo"&gt;0000000000000101&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1000000000001010&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;----------------&lt;/span&gt;
  &lt;span class="mi"&gt;1000000000001111&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32783&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The result does not give us because it is not the way we represent the binary numbers, so if we need to pass our number to negative we need to revert the 1 and 0 of our original number, dare an example to make the topic clearer&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;  &lt;span class="mo"&gt;0110&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;---&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1001&lt;/span&gt; &lt;span class="o"&gt;--&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is called one complement, but then if we do the operation again&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;  &lt;span class="mo"&gt;0000000000000101&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1000000000001001&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;----------------&lt;/span&gt;
  &lt;span class="mo"&gt;0000000000001101&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;is still failing and is not accurate, so we need the two’s complement so that the operation can be accurate. then I will give an example of how a number is represented&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;  &lt;span class="mo"&gt;0011&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;---&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1101&lt;/span&gt; &lt;span class="o"&gt;--&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;  &lt;span class="mo"&gt;0000000000000101&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1111111111111010&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;----------------&lt;/span&gt;
  &lt;span class="mi"&gt;1111111111111111&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;we realize that then the mathematical operation if it gives so that the computer to do mathematical operations with negative numbers, has to represent them differently to be able to store them in memory and interact with them.&lt;/p&gt;

&lt;p&gt;The following picture is graphically how the computer can save negative values.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oMUCWUfN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.log2base2.com/images/storage/negative-integer-storage.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oMUCWUfN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://www.log2base2.com/images/storage/negative-integer-storage.png" alt="This image is from https://www.log2base2.com/storage/how-integers-are-stored-in-memory.html "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The following video will help you understand the subject if you have doubts.&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/4qH4unVtJkE"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=4qH4unVtJkE"&gt;This link &lt;/a&gt; show mathematically why two’s complement in more detail&lt;/p&gt;

&lt;p&gt;this link take the topic more &lt;a href="https://ryanstutorials.net/binary-tutorial/binary-negative-numbers.php"&gt;deeper &lt;/a&gt;&lt;/p&gt;

</description>
      <category>c</category>
      <category>binary</category>
      <category>mathematics</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
