{"id":7906,"date":"2026-11-28T00:01:13","date_gmt":"2026-11-28T08:01:13","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7906"},"modified":"2026-11-21T15:43:57","modified_gmt":"2026-11-21T23:43:57","slug":"am-i-bad-at-binary-fractions","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7906","title":{"rendered":"Am I Bad at Binary Fractions?"},"content":{"rendered":"<p>Short answer: No, I&#8217;m not bad at binary fractions. As was explained in the comments from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7895\">last week&#8217;s Lesson<\/a>, I miscopied the AND mask from a previous lesson, using 0x00EFFFFF instead of 0x007FFFFF. Turns out, everything is okay with my translation; once this error is addressed, my translated output matches.<br \/>\n<!--more--><br \/>\nAs a review, here is the output from my incorrect code that attempts to generate the binary fraction for 0.825:<\/p>\n<p><code>0.110<span style=\"color:red\">1<\/span>00110011001100110011<\/code><\/p>\n<p>And here is how this value is stored inside the binary32 format used for a <em>float<\/em> in C:<\/p>\n<p><code>0.110<span style=\"color:red\">0<\/span>00110011001100110011<\/code><\/p>\n<p>Yes, that one red digit is due to the ineffective mask in my <em>binout()<\/em> function. The way the fraction is stored is correct and it matches what I came up with. Once corrected, as shown in the following code, everything works properly.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_11_28-Lesson.c\" rel=\"noopener\" target=\"_blank\">2026_11_28-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\n<span class=\"comments\">\/* output binary representation of the fraction *\/<\/span>\r\nvoid binout(unsigned b)\r\n{\r\n    int x,exponent;\r\n\r\n    <span class=\"comments\">\/* extract exponent *\/<\/span>\r\n    exponent = b &amp; 0x7F800000;\r\n    exponent &gt;&gt;= 23;\r\n    <span class=\"comments\">\/* undo the standard bias of 127 *\/<\/span>\r\n    exponent -= 127;\r\n    <span class=\"comments\">\/* mask out the sign and exponent *\/<\/span>\r\n    b &amp;= 0x007FFFFF;\r\n    <span class=\"comments\">\/* add the \"assumed\" leading bit *\/<\/span>\r\n    b |= 0x00800000;\r\n\r\n    <span class=\"comments\">\/* output binary decimal *\/<\/span>\r\n    printf(\"binary32: 0.\");\r\n    for( x=0; x&lt;32; x++ )\r\n    {\r\n        <span class=\"comments\">\/* account for exponent shift, max 9 places *\/<\/span>\r\n        if( x&gt;=9+exponent )\r\n            putchar( b &amp; 0x80000000 ? '1' : '0' );\r\n        b &lt;&lt;= 1;\r\n    }\r\n    putchar('\\n');\r\n}\r\n\r\nint main()\r\n{\r\n    union real {\r\n        float f;\r\n        long l;\r\n    } numb;\r\n    long save;\r\n    char binary[33];\r\n    int x;\r\n\r\n    <span class=\"comments\">\/* gather input *\/<\/span>\r\n    printf(\"Enter a positive decimal: \");\r\n    scanf(\"%f\",&amp;numb.f);\r\n    if( numb.f&gt;=1.0 )\r\n    {\r\n        puts(\"Enter a value between 0.0 to 1.0\");\r\n        return(-1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* retain original value *\/<\/span>\r\n    save = numb.l;\r\n\r\n    <span class=\"comments\">\/* process fraction *\/<\/span>\r\n    printf(\"input: %f\\n\",numb.f);\r\n    for( x=0; x&lt;32; x++ )\r\n    {\r\n        numb.f *= 2.0;\r\n        printf(\"x2 = %f\\n\",numb.f);\r\n        if( numb.f==0 )\r\n            break;\r\n        if( numb.f&gt;=1.0 )\r\n        {\r\n            binary[x] = '1';\r\n            numb.f -= 1.0;\r\n        }\r\n        else\r\n            binary[x] = '0';\r\n    }\r\n    binary[x] = '\\0';        <span class=\"comments\">\/* terminate string *\/<\/span>\r\n    printf(\" extract: 0.%s\\n\",binary);\r\n    binout(save);\r\n\r\n    <span class=\"comments\">\/* clean-up *\/<\/span>\r\n    return 0;\r\n}<\/pre>\n<p>This update to the code outputs each value of <code>numb.f<\/code> as it&#8217;s doubled. The result triggers whether a one or a zero appears in the final output of my attempt to translate a binary fraction. It also shows how precision plays a role in the expression of binary fractions:<\/p>\n<pre>Enter a positive decimal: 0.825\r\ninput: 0.825000\r\nx2 = 1.650000\r\nx2 = 1.300000\r\nx2 = 0.600000\r\nx2 = 1.200000\r\nx2 = 0.400000\r\n<span style=\"color:orange\">x2 = 0.799999<\/span>\r\nx2 = 1.599998\r\nx2 = 1.199997\r\nx2 = 0.399994\r\nx2 = 0.799988\r\nx2 = 1.599976\r\nx2 = 1.199951\r\nx2 = 0.399902\r\nx2 = 0.799805\r\nx2 = 1.599609\r\nx2 = 1.199219\r\nx2 = 0.398438\r\nx2 = 0.796875\r\nx2 = 1.593750\r\nx2 = 1.187500\r\nx2 = 0.375000\r\nx2 = 0.750000\r\nx2 = 1.500000\r\nx2 = 1.000000\r\nx2 = 0.000000\r\n extract: 0.110100110011001100110011\r\nbinary32: 0.110100110011001100110011<\/pre>\n<p>With the proper AND mask applied, the output of my calculation and the extraction from the <em>binout()<\/em> function match. The fourth digit after the decimal is 1 in both examples. Problem solved.<\/p>\n<p>Also note the output for each step of the process. Specifically, the line highlighted in orange: 0.4 &times; 2.0 equals 0.8, but the line shows 0.799999. Here you see the precision problem &mdash; and a reason why it&#8217;s a bad idea to use floating point numbers for a loop. The generated result is still proper. To confirm, I did the math (in a spreadsheet.) Here is the output from for the binary fraction:<\/p>\n<p>0.110100110011001100110011 = 0.5 + 0.25 + 0.625 + 0.007813 + 0.003906 + 0.000488 + 0.000244 + 0.000031 + 0.000015 + 0.000002 + 0.000001 = 0.824999<\/p>\n<p>Yeah, that&#8217;s close enough. There are some smaller values not shown in the line above that may push that &#8220;999&#8221; part up to 0.825.<\/p>\n<p>By the way, I think I&#8217;ve finally made the connection as to why real numbers are referred to as &#8220;floating point&#8221; in computerdom. It makes sense! The decimal place moves based on the exponent. It does, literally, float. Neat-o.<\/p>\n<p>A special thank you to M.Stumpfl for pointing out my typo and the basis for the translation problem discussed last week.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Binary fractions aren&#8217;t perfect, which is why &#8220;precision&#8221; comes into play. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7906\">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-7906","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\/7906","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=7906"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7906\/revisions"}],"predecessor-version":[{"id":7915,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7906\/revisions\/7915"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7906"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7906"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7906"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}