{"id":2037,"date":"2016-07-30T00:01:15","date_gmt":"2016-07-30T07:01:15","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2037"},"modified":"2016-08-10T20:41:40","modified_gmt":"2016-08-11T03:41:40","slug":"reading-a-binary-fraction","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2037","title":{"rendered":"Reading a Binary Fraction"},"content":{"rendered":"<p>As an example of how a binary values can hold fractions, I&#8217;ve concocted a simple real number format: An 8-bit value with 4 bits representing the whole number portion and 4 bits representing the fractional portion. Here&#8217;s how it looks:<br \/>\n<!--more--><br \/>\n<div id=\"attachment_2039\" style=\"width: 460px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2039\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/07\/0730-figure2-8-bit-real-number.png\" alt=\"Figure 1. Interpreting an 8-bit real number.\" width=\"450\" height=\"386\" class=\"size-full wp-image-2039\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/07\/0730-figure2-8-bit-real-number.png 450w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/07\/0730-figure2-8-bit-real-number-300x257.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/07\/0730-figure2-8-bit-real-number-350x300.png 350w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><p id=\"caption-attachment-2039\" class=\"wp-caption-text\">Figure 1. Interpreting an 8-bit real number.<\/p><\/div><\/p>\n<p>In Figure 1,  you see the binary value 0xC5, which when read as an <em>unsigned char<\/em> would be the value 197. In this interpretation, however, a decimal point lurks between the 4th and 5th bits in the value. Bits to the left represent positive powers of 2; bits to the right represent negative values.<\/p>\n<p>Performing the math from Figure 1, you get bits representing values 8.0 and 4.0, so 12 is set as the whole number portion. To the right you get bits representing values 0.25 (&frac14;) and 0.0625 (<sup>1<\/sup>\/<sub>16<\/sub>), for a total of 0.3125 or <sup>5<\/sup>\/<sub>16<\/sub>. The complete value is 12.3125 or 12<sup>5<\/sup>\/<sub>16<\/sub>.<\/p>\n<p>Bits are just bits, however, so what really matters is how your code interprets them. The value could be 197, or -59 for a <em>signed char<\/em> value, or the character &#197;. In this interpretation, however, it&#8217;s the real number 12.3125. To get to that result, you write code that properly interprets the 8-bit real number.<\/p>\n<p>The following code shows how to make such a calculation. It takes the value <code>0xC5<\/code> and processes the bits in that <em>unsigned char<\/em> to generate a real number as output:<\/p>\n<pre class=\"screen\">\r\n#include <stdio.h>\r\n\r\nint main()\r\n{\r\n    float twos = 8;\r\n    float result = 0;\r\n    unsigned char binary = 0xC5;\r\n    int x;\r\n\r\n    printf(\"Binary value 0x%X is \",binary);\r\n    for(x=0;x&lt;8;x++)\r\n    {\r\n        if( binary &amp; 0x80)\r\n            result += twos;\r\n        binary &lt;&lt;= 1;\r\n        twos \/= 2;\r\n    }\r\n    printf(\"%.4f\\n\",result);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The value of <em>float<\/em> variable <code>twos<\/code> is set to 8 at Line 5. That&#8217;s the power of 2 equal to the far-left bit in the code&#8217;s 4:4 real number format, shown in Figure 1.<\/p>\n<p>Variable <code>binary<\/code> holds the sample value, <code>0xC5<\/code>, which is the same value used in Figure 1.<\/p>\n<p>The <em>for<\/em> loop at Line 11 processes all 8 bits of value <code>binary<\/code>. Each bit is examined (Line 13) and the appropriate power of 2 is assigned to variable <code>result<\/code> if the bit is set.<\/p>\n<p>In Line 15, the value of variable <code>binary<\/code> is shifted left. That way each bit in the value is examined individually. Variable <code>twos<\/code> is cut in half in Line 16. As a <em>float<\/em>, its value is reduced from 8 to 4 to 2 to 1 and then to 0.5 or &frac12;, and then to 0.25 or &frac14;, and so on. These values match the bit positions stored in variable <code>binary<\/code> as it continues to shift left.<\/p>\n<p>Here is the code&#8217;s output:<\/p>\n<pre><code>Binary value 0xC5 is 12.3125<\/code><\/pre>\n<p>You could modify the code to hold larger and more precise values: A <em>short int<\/em> holds 16-bits, an <em>int<\/em> 32 bits, and a <em>long<\/em> 64 bits. You must keep the decimal place consistent with whatever size binary value you choose.<\/p>\n<p>The beauty of all this binary nonsense is that the math still works: If you add or subtract the real number interpretations of binary values, the results calculate as you would expect.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There&#8217;s a decimal in them thar bits! <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2037\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2037","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2037","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2037"}],"version-history":[{"count":10,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2037\/revisions"}],"predecessor-version":[{"id":2082,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2037\/revisions\/2082"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}