{"id":7842,"date":"2026-10-31T00:01:19","date_gmt":"2026-10-31T07:01:19","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7842"},"modified":"2026-10-24T12:17:22","modified_gmt":"2026-10-24T19:17:22","slug":"splitting-up-a-real-number","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7842","title":{"rendered":"Splitting Up a Real Number"},"content":{"rendered":"<p>In the C language, the flexible storage provided by a union allows me to access and examine the bits stored in a real number. Due to different data types accessing the same data, I can look at the bits in a <em>float<\/em> as a <em>long int<\/em>. From there, I can output the individual bits to see what each one represents, which was covered in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7831\">last week&#8217;s Lesson<\/a>. In this Lesson, I update the code to examine the individual parts of the binary32 format.<br \/>\n<!--more--><br \/>\nHere is the updated code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_10_31-Lesson.c\" rel=\"noopener\" target=\"_blank\">2026_10_31-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\">\/* create a 32-character string from a\r\n   long int value *\/<\/span>\r\nchar *binString(long a)\r\n{\r\n    char *bin;\r\n    int x;\r\n\r\n    <span class=\"comments\">\/* allocate string storage *\/<\/span>\r\n    bin = malloc(sizeof(char) * 33);\r\n    if( bin==NULL )\r\n    {\r\n        fprintf(stderr,\"Unable to allocate memory\\n\");\r\n        exit(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* create binary string *\/<\/span>\r\n    for(x=0; x&lt;32; x++ )\r\n    {\r\n        *(bin+x) = a &amp; 0x80000000 ? '1' : '0';\r\n        a &lt;&lt;= 1;\r\n    }\r\n    *(bin+x) = '\\0';\r\n\r\n    return(bin);\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    unsigned signbit,exponent,mantissa;\r\n\r\n    <span class=\"comments\">\/* gather input *\/<\/span>\r\n    printf(\"Enter a value: \");\r\n    scanf(\"%f\",&amp;numb.f);\r\n\r\n    <span class=\"comments\">\/* output the full binary value *\/<\/span>\r\n    printf(\"Integer value is %ld\\n\",numb.l);\r\n    printf(\"Binary value is %s\\n\",binString(numb.l));\r\n\r\n    <span class=\"comments\">\/* output integer values - sign bit *\/<\/span>\r\n    signbit = numb.l &amp; 0x80000000;\r\n    signbit &gt;&gt;= 31;\r\n    printf(\"Sign bit is 0x%X\\n\",signbit);\r\n    <span class=\"comments\">\/* exponent *\/<\/span>\r\n    exponent = numb.l &amp; 0x7F800000;\r\n    exponent &gt;&gt;= 23;\r\n    printf(\"Exponent is 0x%X\\n\",exponent);\r\n    <span class=\"comments\">\/* fraction *\/<\/span>\r\n    mantissa = numb.l &amp; 0x00EFFFFF;\r\n    printf(\"Fraction is 0x%X\\n\",mantissa);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The <em>stringBin()<\/em> function translates a binary string into an integer value. This function was introduced in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3045\">this Lesson<\/a> from 2018. It&#8217;s used in last week&#8217;s code as well.<\/p>\n<p>The <em>main()<\/em> function splits the binary32 real number into its component parts: sign, exponent, and fraction. Integer values for each are output.<\/p>\n<p>The process works similarly for each step: mask off a specific portion of the binary number then shift the masked portion right so that its value is represented. Figure 1 illustrates the process.<\/p>\n<div id=\"attachment_7858\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7858\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/10\/1031-Figure1.png\" alt=\"bitmap diagrams\" width=\"550\" height=\"229\" class=\"size-full wp-image-7858\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/10\/1031-Figure1.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/10\/1031-Figure1-300x125.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/10\/1031-Figure1-500x208.png 500w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-7858\" class=\"wp-caption-text\">Figure 1. How a value is isolated with an &#038; (and) mask, then shifted to pluck it out of the longer binary number.<\/p><\/div>\n<p>Here are the individual steps to isolate the exponent as illustrated in Figure 1. This value is 8 bits in length set at offset 1 in the original binary value:<\/p>\n<p>A bitwise AND (<code>&<\/code>) mask is applied to filter out only the 8 bits of exponent value from the <em>long int<\/em> stored in <code>numb.b<\/code>:<\/p>\n<p><code>exponent = numb.b &amp; 0x7F800000;<\/code><\/p>\n<p>Then these 8 bits are shifted right 23 places to set them at the start of the number:<\/p>\n<p><code>exponent &gt;&gt;= 23;<\/code><\/p>\n<p>The effect of these two statements is to single out the exponent portion of the binary32 format and make it into its own integer value. Once modified, the value is output:<\/p>\n<p><code> printf(\"Exponent is 0x%X\\n\",exponent);<\/code><\/p>\n<p>The process repeats for each portion of the binary32 encoded number.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<pre>Enter a value: 12.375\r\nInteger value is 1095106560\r\nBinary value is 01000001010001100000000000000000\r\nSign bit is 0x0\r\nExponent is 0x82\r\nFraction is 0x460000<\/pre>\n<p>For the value 12.375, the sign bit is zero &mdash; positive. The exponent is 0x82 and the faction is 0x460000.<\/p>\n<p>The sign bit part of these values that&#8217;s cleanly understandable. As with a signed integer value, 0 is positive and 1 is negative. In fact, if you input -12.375, here is the output for comparison:<\/p>\n<pre>Enter a value: -13.375\r\nInteger value is 3243638784\r\nBinary value is 11000001010101100000000000000000\r\nSign bit is 0x1\r\nExponent is 0x82\r\nFraction is 0x460000<\/pre>\n<p>The sign bit is now 1, but the other values, exponent and fraction, are the same.<\/p>\n<p>The value 0x82 (130 decimal) doesn&#8217;t make sense to me as an exponent. Ditto for the fraction 0x460000 (4,587,520 decimal), which is equally confusing. Obviously, some form of translation is going on that allows for the interpretation of these quantities to represent 12.375.<\/p>\n<p>I cover how the values translate in next week&#8217;s Lesson.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Each part of a binary32 value represents a different attribute of a real number. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7842\">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-7842","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\/7842","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=7842"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7842\/revisions"}],"predecessor-version":[{"id":7872,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7842\/revisions\/7872"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}