{"id":7895,"date":"2026-11-21T00:01:20","date_gmt":"2026-11-21T08:01:20","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7895"},"modified":"2026-11-14T12:24:48","modified_gmt":"2026-11-14T20:24:48","slug":"binary-fraction-comparison","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7895","title":{"rendered":"Binary Fraction Comparison"},"content":{"rendered":"<p>Binary fractions are nutty, yet they&#8217;re an important part of understanding how real numbers are stored in a computer. Continuing from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7877\">last week&#8217;s Lesson<\/a>, here I compare my results of fraction translation with values stored in the binary32 format used by the computer. Doing so requires some manipulation &mdash; and some assistance from our old pal, the C language union.<br \/>\n<!--more--><br \/>\nThe problem with comparing my binary fraction interpretation with what&#8217;s present in a real number&#8217;s bit pattern is that the bits in the binary32 format are shifted based on an exponent value. Further, the leading &#8216;1&#8217; is removed from the resulting number. (The leading 1 bit is &#8220;assumed&#8221; in this format.)<\/p>\n<p>For example, here&#8217;s output for the fraction 0.375 from the code presented in last week&#8217;s Lesson:<\/p>\n<pre>Enter a positive decimal: 0.375\r\n0.011<\/pre>\n<p>The value 0.375 is stored in the binary 32 format as this bit pattern:<\/p>\n<p><code>00111110110000000000000000000000<\/code><\/p>\n<p>Figure 1 shows how these bits sort out.<\/p>\n<div id=\"attachment_7896\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7896\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/11\/1121-Figure1.png\" alt=\"bit patterns of the binary32 format\" width=\"550\" height=\"151\" class=\"size-full wp-image-7896\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/11\/1121-Figure1.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/11\/1121-Figure1-300x82.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/11\/1121-Figure1-500x137.png 500w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-7896\" class=\"wp-caption-text\">Figure 1. How the fraction is extracted from the binary32 format.<\/p><\/div>\n<p>In the figure, you see the sign bit is 0, positive. The exponent is 0x7D. The mantissa is 0x400000. To compare the result from my code in last week&#8217;s Lesson with the fraction as stored in the binary32 requires some manipulation. This job is handled in the following update to the code from last week&#8217;s Lesson:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_11_21-Lesson.c\" rel=\"noopener\" target=\"_blank\">2026_11_21-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;= 0x00EFFFFF;\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    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\">\/* output original binary value *\/<\/span>\r\n    binout(numb.l);\r\n\r\n    <span class=\"comments\">\/* process fraction *\/<\/span>\r\n    printf(\" extract: 0.\");\r\n    for( x=0; x&lt;32; x++ )\r\n    {\r\n        numb.f *= 2.0;\r\n        if( numb.f==0 )\r\n            break;\r\n        if( numb.f&gt;=1.0 )\r\n        {\r\n            putchar('1');\r\n            numb.f -= 1.0;\r\n        }\r\n        else\r\n            putchar('0');\r\n    }\r\n    putchar('\\n');\r\n\r\n    <span class=\"comments\">\/* clean-up *\/<\/span>\r\n    return 0;\r\n}<\/pre>\n<p>The <em>binout()<\/em> function outputs a string representing the binary values stored in the fractional part of a binary32 formatted real number. It accomplishes this task in several steps.<\/p>\n<p>First, the exponent value is extracted:<\/p>\n<p><code>exponent = b &amp; 0x7F800000;<br \/>\nexponent &gt;&gt;= 23;<\/code><\/p>\n<p>The bitwise <code>&amp;<\/code> operator masks out the exponent value, which is then shifted 23 places right. This process is described in a <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7842\">previous Lesson<\/a>. Because the value is based on the standard bias of 127 (per the binary32 format), it must be adjusted to obtain the true exponent value:<\/p>\n<p><code>exponent -= 127;<\/code><\/p>\n<p>The result determines how the binary fraction is shifted, left or right based on the exponent&#8217;s sign.<\/p>\n<p>Next, the binary fraction is pulled from the rest of the number:<\/p>\n<p><code>b &amp;= 0x00EFFFFF;<\/code><\/p>\n<p>The bitwise <code>&amp;<\/code> masks out the binary fraction. But the leading 1 bit is assumed, so I add it back in with a bitwise <code>|<\/code> (OR) operator:<\/p>\n<p><code>b |= 0x00800000;<\/code><\/p>\n<p>The remainder of the code outputs the binary fraction, but only given the offset provided by the exponent value in variable <code>exponent<\/code>. This value shifts the number&#8217;s starting position when the result is output inside the <em>for<\/em> loop.<\/p>\n<p>The goal of all these steps is to compare the binary fraction as it&#8217;s stored in the binary32 format against what my code generated from last week&#8217;s Lesson. Here&#8217;s a sample run:<\/p>\n<pre>Enter a positive decimal: 0.375\r\nbinary32: 0.0110000000000000000000000\r\n extract: 0.011<\/pre>\n<p>The values match. Here are a few more examples that match:<\/p>\n<pre>Enter a positive decimal: 0.625\r\nbinary32: 0.101000000000000000000000\r\n extract: 0.101\r\n\r\nEnter a positive decimal: 0.1\r\nbinary32: 0.000110011001100110011001101\r\n extract: 0.000110011001100110011001101<\/pre>\n<p>But not all fractions match:<\/p>\n<pre>Enter a positive decimal: .999\r\nbinary32: 0.111<span style=\"color:red\">0<\/span>11111011111001110111\r\n extract: 0.111<span style=\"color:red\">1<\/span>11111011111001110111\r\n\r\nEnter a positive decimal: 0.825\r\nbinary32: 0.110<span style=\"color:red\">0<\/span>00110011001100110011\r\n extract: 0.110<span style=\"color:red\">1<\/span>00110011001100110011<\/pre>\n<p>For the fractions 0.999 and 0.825, you see an extra bit in my output verses what&#8217;s output from the binary32 format. I explain the funkiness behind this discrepancy in next week&#8217;s Lesson.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How do my results match up with the binary32 format? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7895\">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-7895","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\/7895","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=7895"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7895\/revisions"}],"predecessor-version":[{"id":7911,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7895\/revisions\/7911"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7895"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}