{"id":7831,"date":"2026-10-24T00:01:43","date_gmt":"2026-10-24T07:01:43","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7831"},"modified":"2026-10-17T11:49:15","modified_gmt":"2026-10-17T18:49:15","slug":"how-real-numbers-are-stored-in-binary","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7831","title":{"rendered":"How Real Numbers are Stored in Binary"},"content":{"rendered":"<p>A computer&#8217;s native tongue is binary, the famous ones and zeros worshiped by early mainframe programmers. Clumped into bytes and words, these digits easily represent an armada of integers, both positive and negative values. But what about fractions, decimal values, huge numbers, and teeny tiny numbers? What&#8217;s the secret to their storage?<br \/>\n<!--more--><br \/>\nMany years ago, teams of computer scientists in white lab coats came up with the <a href=\"https:\/\/en.wikipedia.org\/wiki\/IEEE_754\" target=\"_blank\">IEEE 754 Standard<\/a> for floating point arithmetic on computer systems. This standard forms the basis of how real numbers are presented in and used by programming languages.<\/p>\n<p>In C, the <em>float<\/em> data type is defined as a single precision floating-point value. Also referred to as <em>binary32<\/em>, it consists of 32 bits of information which represent values in the range of 3.4 &times; 10<sup>38<\/sup> through 1.2 &times; 10<sup>-38<\/sup>, positive and negative. This range is incredibly range, which is where the precision part comes into play.<\/p>\n<p>The single precision of a <em>float<\/em> implies that the value represented is accurate up to about seven digits. Officially, the precision is defined as anywhere between six to nine significant decimal digits. For almost all mathematical operations, such precision is adequate; the <em>double<\/em> data type offers twice as much precision when required.<\/p>\n<p>I&#8217;m curious how such information is stored inside 32 bits. Obviously, something sneaky must be going on.<\/p>\n<p>Figure 1 illustrates how 32 bits of binary data is divided when representing a real number.<\/p>\n<div id=\"attachment_7833\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7833\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/10\/1024-Figure1.png\" alt=\"binary32 format\" width=\"550\" height=\"202\" class=\"size-full wp-image-7833\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/10\/1024-Figure1.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/10\/1024-Figure1-300x110.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/10\/1024-Figure1-500x184.png 500w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-7833\" class=\"wp-caption-text\">Figure 1. The deets on how a real number is stored in binary.<\/p><\/div>\n<p>The bits are organized into three clumps:<\/p>\n<ul>\n<li>A sign bit, positive or negative<\/li>\n<li>Eight exponent bits, which set the value&#8217;s magnitude (size)<\/li>\n<li>Twenty-three fraction bits, the value itself also called the mantissa or significand<\/li>\n<\/ul>\n<p>To witness this storage, I&#8217;m exploiting the weakness of a C language union by setting a <em>float<\/em> value but then examining it as a <em>long int<\/em>. This approach lets me peek at the bytes inside the single-precision value and output the result to reflect what you see in Figure 1. Here&#8217;s the code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_10_24-Lesson.c\" rel=\"noopener\" target=\"_blank\">2026_10_24-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    union real {\r\n        float a;\r\n        long b;\r\n    };\r\n    union real numb;\r\n    int x;\r\n\r\n    <span class=\"comments\">\/* gather input *\/<\/span>\r\n    printf(\"Enter a value: \");\r\n    scanf(\"%f\",&amp;numb.a);\r\n\r\n    <span class=\"comments\">\/* output real number value *\/<\/span>\r\n    printf(\"The value %f is stored as: \",numb.a);\r\n\r\n    <span class=\"comments\">\/* read bytes *\/<\/span>\r\n    for( x=0; x&lt;32; x++ )\r\n    {\r\n        if( x==1 || x==9 )\r\n        {\r\n            putchar(' ');\r\n        }\r\n        putchar( numb.b &amp; 0x80000000 ? '1' : '0' );\r\n        numb.b &lt;&lt;= 1;\r\n    }\r\n    putchar('\\n');\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The program prompts for a real number. The value is then displayed, along with each of the bits as stored in the binary32 format. The union <em>real<\/em> allows me to access the <em>float<\/em> inside <code>numb.a<\/code> as <em>long int<\/em> <code>numb.b<\/code>. A <em>for<\/em> loop plows through each of the 32 bits, outputting a one or zero in the proper place, along with a space between the sign bit and exponent bits as well as the exponent bits and the fraction bits.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<pre>Enter a value: 12.375\r\nThe value 12.375000 is stored as: 0 10000010 10001100000000000000000<\/pre>\n<p>This output accurately shows how the value 12.375 is represented as a real number in the binary32 format, a <em>float<\/em>. Of course, what you see are just bits and they don&#8217;t exactly explain much. For next week&#8217;s Lesson, I split this value into its integer components, which is the next step toward understanding what each clump represents.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Representing a decimal value, tiny or huge, using only ones and zeros is an artform. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7831\">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-7831","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\/7831","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=7831"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7831\/revisions"}],"predecessor-version":[{"id":7850,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7831\/revisions\/7850"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}