{"id":2850,"date":"2014-02-17T09:00:39","date_gmt":"2014-02-17T14:00:39","guid":{"rendered":"http:\/\/www.goodmath.org\/blog\/?p=2850"},"modified":"2014-02-17T11:13:37","modified_gmt":"2014-02-17T16:13:37","slug":"representing-numbers-in-binary","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2014\/02\/17\/representing-numbers-in-binary\/","title":{"rendered":"Representing Numbers in Binary"},"content":{"rendered":"<p> Before we can really start writing interesting programs with our ARM, even simple ones, we need to understand a bit about how how a computer actually works with numbers.<\/p>\n<p> When people talk about computers, they frequently say something like &#8220;It&#8217;s all zeros and ones&#8221;. That&#8217;s wrong, in a couple of different ways.<\/p>\n<p> First, when you look at the actual computer hardware, there are no zeros and ones. There are two kinds of signals, represented as two different voltage levels. You can call them &#8220;high&#8221; and &#8220;low&#8221;, or &#8220;A&#8221; and &#8220;B&#8221;, or &#8220;+&#8221; and &#8220;-&#8220;. It doesn&#8217;t matter: it&#8217;s just two signals. (In fact, one fascinating thing to realize is that there&#8217;s a fundamental symmetry in those signals: you can swap the choice of which signal is 0 and 1, and if you just swap the and gates and the or gates, everything you won&#8217;t be able to tell the difference! So one ARM processor could use one signal for 1, and a different ARM could use that signal as 0, and you wouldn&#8217;t actually be able to tell. In fact, everyone does it the same way, because chip design and manufacture are standardized. But mathematically, it&#8217;s possible. We&#8217;ll talk about that duality\/symmetry in another post.)<\/p>\n<p> Second, the computer really doesn&#8217;t work with numbers at all. Computer hardware is all about binary logic. Even if you abstract from different voltages to the basic pairs of values, the computer still doesn&#8217;t understand numbers. You&#8217;ve got bits, but to get from bits to numbers, you need to decide on a meaning for the bits two possible values, and you need to decide how to put them together.<\/p>\n<p>That&#8217;s what we&#8217;re really going to focus on in this post. Once you&#8217;ve decided to call on of the two primitive values 1, and the other one 0, you need to decide how to combine multiple zeros and ones to make a number. <\/p>\n<p> It might seem silly, because isn&#8217;t it obvious that it should use binary? But the choice isn&#8217;t so clear. There have been a lot of different numeric representations. For one example, many of IBM&#8217;s mainframe computers used (and continue to use) something called binary coded decimal (BCD). It&#8217;s not just different for the sake of being different: in fact, for financial applications, BCD really does have some major advantages! Even if you have decided to use simple binary, it&#8217;s not that simple. Positive integers are easy. But how do you handle negative numbers? How do you handle things that aren&#8217;t integers?<\/p>\n<p> We&#8217;re going to start with the simplest case: unsigned integers. (We&#8217;ll worry about fractions, decimals, and floating point in another post.) Like pretty much all modern computers, the ARM uses the basic, mathematical binary exponential representation. This works basically the same as our usual base-10 numbers. We look at the digits from right to left. The leftmost digit (also called the <em>least significant digit<\/em> counts ones; the next digit counts 10s; the next counts 100s, and soon. So in base 10, the number 3256 means 6*10<sup>0<\/sup> plus 5*10<sup>1<\/sup> plus 2*10<sup>2<\/sup> plus 3*10<sup>3<\/sup>.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/www.goodmath.org\/blog\/wp-content\/uploads\/2014\/02\/binary-bits1.gif\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.goodmath.org\/blog\/wp-content\/uploads\/2014\/02\/binary-bits1.gif?resize=300%2C83\" alt=\"binary-bits\" width=\"300\" height=\"83\" class=\"alignnone size-medium wp-image-2854\" \/><\/a><\/p>\n<p> In binary, we do exactly the same thing, only we do it with powers of 2 instead of powers of 10. So in binary the number 1001101 is 1 + 0*2<sup>1<\/sup> + 1*2<sup>2<\/sup> + 1*2<sup>3<\/sup> + 0*2<sup>4<\/sup> + 0*2<sup>5<\/sup> + 1*2<sup>6<\/sup> =1 + 4 + 8 + 64 = 77.<\/p>\n<p> Arithmetic on binary is easy enough &#8211; you do the same thing you would with decimal, but in subtraction, borrows give you 2, not 10. As a quick example, let&#8217;s look at 7 + 13, which is 111 +  1101.<\/p>\n<ol>\n<li> We start at the right edge. We have 1 + 1 = 10 &#8211; so the first digit of the sum is 0, and we carry 1.<\/li>\n<li> Next we have 1 + 0 + 1<sub>(carry)<\/sub> = 10 &#8211; so the second digit is again 0, and we carry 1. Our sum so far is 00.<\/li>\n<li> Now we&#8217;re on to the third digit. 1 + 1 + 1<sub>(carry)<\/sub> = 11. So the third digit is 1, and we carry one. Our sum so far is 100.<\/li>\n<li> Now the fourth digit, 1 + 0 + 1<sub>(carry)<\/sub>, so we get 10. So the sum is 10100, or 20.<\/li>\n<\/ol>\n<p> We&#8217;ll ignore subtraction for a moment, because as we&#8217;ll see in a little while, in computer hardware, we don&#8217;t actually need to have subtraction as a primitive. Addition is the core of what computer arithmetic, and we&#8217;ll use addition to implement subtraction by taking the negative of the number being subtracted. (That is, to compute A-B, we&#8217;ll do A+(-B).)<\/p>\n<p> Positive integers with addition aren&#8217;t enough to do most stuff we want to do on a computer. Even if we&#8217;re never going to write a program that manipulates numbers, we absolutely need to be able to subtract. To a computer, the only way to compare values is to subtract one from another! So we need to be able to do negatives and subtractions. How can we represent negative numbers in binary?<\/p>\n<p> There are three basic choices, called <em>sign-bit<\/em>\/<em>sign-magnitude<\/em>, <em>one&#8217;s-complement<\/em> and <em>two&#8217;s complement<\/em>. I&#8217;ve put an example of the three representing the number 75 in the figure below.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/www.goodmath.org\/blog\/wp-content\/uploads\/2014\/02\/different-binary1.gif\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.goodmath.org\/blog\/wp-content\/uploads\/2014\/02\/different-binary1.gif?resize=300%2C257\" alt=\"different-binary\" width=\"300\" height=\"257\" class=\"alignnone size-medium wp-image-2855\" \/><\/a><\/p>\n<p> In sign-bit representation, what you do is take the leftmost bit (also called the <em>high-order bit<\/em>), and use it to indicate sign (for obvious reasons, you call it the <em>sign bit<\/em>.). If the sign bit is 0, then the number is positive; if it&#8217;s 1, then the number is negative. For example, 01010 would be +10; 11010 would be -10.<\/p>\n<p> For a human being, sign-bit looks great. But in practice, sign-bit was never used much, because while it looks simple to a human,  it&#8217;s quite complicated to build in computer hardware. IBM did use it in some early machines, but even they gave up on it.<\/p>\n<p> Next is one&#8217;s complement. In 1&#8217;s complement, high order bit is still a sign bit. But to convert a number from positive to negative, you don&#8217;t just change the sign bit &#8211; you invert every single bit in the number. You can still tell whether a number is positive or negative by its sign bit, but the rest of the bits are also different. +10 in one&#8217;s complement binary is 01010; -10 is 10101.<\/p>\n<p> Arithmetic in 1s complement is a bit weird. You can <em>almost<\/em> just add a negative number to a positive number as if they were both positive. Almost, but not quite.<\/p>\n<p> For example, let&#8217;s try 6 + -6. 6 is 0110, and -6 is 1001. Add them up: 1111. In twos complement, that&#8217;s -0. And there&#8217;s one of the weird issues about one&#8217;s complement: it&#8217;s got two distinct values for 0 &#8211; +0 and -0. Since they&#8217;re both just 0, we treat them as equal, and it&#8217;s not really a problem.<\/p>\n<p> How about 6 + -8? 6 is 00110 (we need 5 bits to handle 8), and -8 is 10111. Add<br \/>\nthem up, and you get 11101 &#8211; which is -2, the correct answer.<\/p>\n<p> Now, what about 8 + -6? 8 is 01000, and -6 is 11001. Add them up, and you<br \/>\nget 00001, with a carry of 1. So 8 + -6 = 1? That&#8217;s wrong! In one&#8217;s complement,<br \/>\nthere are a bunch of places where simple binary addition will be off by one.<br \/>\nSo you need to work out the algorithm for where it&#8217;s off-by-one and where it&#8217;s not, and you need to build more complicated hardware to incorporate it. That&#8217;s not attractive.<\/p>\n<p> Still, one&#8217;s complement has been used a lot. In particular, one of the first computers I got to use was an old, beaten-up PDP-1, which used 1&#8217;s complement numbers.<\/p>\n<p> Finally, we get to the representation that&#8217;s used in pretty much all modern computers: 2&#8217;s complement!<\/p>\n<p> Once again, 2&#8217;s complement uses a sign bit. But instead of flipping all of the bits, you do something different. In 2&#8217;s complement, you need to know how many bits you&#8217;re using. If you&#8217;re doing an N-bit 2&#8217;s complement binary number, then the number -x is represented by 2<sup>N<\/sup>-x. <\/p>\n<p> So if we&#8217;re doing 6 bits, and we wanted to represent -5, then we&#8217;d take 2<sup>6<\/sup>-5, or 64-5=59. In binary, that&#8217;s 111011. <\/p>\n<p> The really beautiful thing about 2s complement is that it&#8217;s pretty much the same thing as a trucated 2-adic integer &#8211; which means that arithmetic <em>just works<\/em>. If you&#8217;re adding two numbers, it doesn&#8217;t matter whether they&#8217;re signed numbers or not &#8211; it works.<\/p>\n<p> It&#8217;s also really easy to implement negation. You don&#8217;t have to do that whole &#8220;subtract from 2^N&#8221; thing. In 2s complement, -N is 1+(ones_complement(N)). That&#8217;s super-easy to implement in hardware, and it&#8217;s also easy to understand and do for a human: flip the bits and add one!<\/p>\n<p> Two&#8217;s complement is, in my opinion, a clear winner in integer representation, and the world of computer hardware maker agrees &#8211; everyone now uses 2&#8217;s complement for integers.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Before we can really start writing interesting programs with our ARM, even simple ones, we need to understand a bit about how how a computer actually works with numbers. When people talk about computers, they frequently say something like &#8220;It&#8217;s all zeros and ones&#8221;. That&#8217;s wrong, in a couple of different ways. First, when you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[260],"tags":[],"class_list":["post-2850","post","type-post","status-publish","format-standard","hentry","category-arm"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-JY","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/2850","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/comments?post=2850"}],"version-history":[{"count":4,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/2850\/revisions"}],"predecessor-version":[{"id":2858,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/2850\/revisions\/2858"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=2850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=2850"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=2850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}