Am I Bad at Binary Fractions?

Short answer: No, I’m not bad at binary fractions. As was explained in the comments from last week’s Lesson, 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.

As a review, here is the output from my incorrect code that attempts to generate the binary fraction for 0.825:

0.110100110011001100110011

And here is how this value is stored inside the binary32 format used for a float in C:

0.110000110011001100110011

Yes, that one red digit is due to the ineffective mask in my binout() 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.

2026_11_28-Lesson.c

#include <stdio.h>
#include <stdlib.h>

/* output binary representation of the fraction */
void binout(unsigned b)
{
    int x,exponent;

    /* extract exponent */
    exponent = b & 0x7F800000;
    exponent >>= 23;
    /* undo the standard bias of 127 */
    exponent -= 127;
    /* mask out the sign and exponent */
    b &= 0x007FFFFF;
    /* add the "assumed" leading bit */
    b |= 0x00800000;

    /* output binary decimal */
    printf("binary32: 0.");
    for( x=0; x<32; x++ )
    {
        /* account for exponent shift, max 9 places */
        if( x>=9+exponent )
            putchar( b & 0x80000000 ? '1' : '0' );
        b <<= 1;
    }
    putchar('\n');
}

int main()
{
    union real {
        float f;
        long l;
    } numb;
    long save;
    char binary[33];
    int x;

    /* gather input */
    printf("Enter a positive decimal: ");
    scanf("%f",&numb.f);
    if( numb.f>=1.0 )
    {
        puts("Enter a value between 0.0 to 1.0");
        return(-1);
    }

    /* retain original value */
    save = numb.l;

    /* process fraction */
    printf("input: %f\n",numb.f);
    for( x=0; x<32; x++ )
    {
        numb.f *= 2.0;
        printf("x2 = %f\n",numb.f);
        if( numb.f==0 )
            break;
        if( numb.f>=1.0 )
        {
            binary[x] = '1';
            numb.f -= 1.0;
        }
        else
            binary[x] = '0';
    }
    binary[x] = '\0';        /* terminate string */
    printf(" extract: 0.%s\n",binary);
    binout(save);

    /* clean-up */
    return 0;
}

This update to the code outputs each value of numb.f as it’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:

Enter a positive decimal: 0.825
input: 0.825000
x2 = 1.650000
x2 = 1.300000
x2 = 0.600000
x2 = 1.200000
x2 = 0.400000
x2 = 0.799999
x2 = 1.599998
x2 = 1.199997
x2 = 0.399994
x2 = 0.799988
x2 = 1.599976
x2 = 1.199951
x2 = 0.399902
x2 = 0.799805
x2 = 1.599609
x2 = 1.199219
x2 = 0.398438
x2 = 0.796875
x2 = 1.593750
x2 = 1.187500
x2 = 0.375000
x2 = 0.750000
x2 = 1.500000
x2 = 1.000000
x2 = 0.000000
 extract: 0.110100110011001100110011
binary32: 0.110100110011001100110011

With the proper AND mask applied, the output of my calculation and the extraction from the binout() function match. The fourth digit after the decimal is 1 in both examples. Problem solved.

Also note the output for each step of the process. Specifically, the line highlighted in orange: 0.4 × 2.0 equals 0.8, but the line shows 0.799999. Here you see the precision problem — and a reason why it’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:

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

Yeah, that’s close enough. There are some smaller values not shown in the line above that may push that “999” part up to 0.825.

By the way, I think I’ve finally made the connection as to why real numbers are referred to as “floating point” in computerdom. It makes sense! The decimal place moves based on the exponent. It does, literally, float. Neat-o.

A special thank you to M.Stumpfl for pointing out my typo and the basis for the translation problem discussed last week.

Leave a Reply