Saturday, November 21, 2026
HomeC ProgrammingBinary Fraction Comparability | C For Dummies Weblog

Binary Fraction Comparability | C For Dummies Weblog


Binary fractions are nutty, but they’re an vital a part of understanding how actual numbers are saved in a pc. Persevering with from final week’s Lesson, right here I examine my outcomes of fraction translation with values saved within the binary32 format utilized by the pc. Doing so requires some manipulation — and a few help from our previous pal, the C language union.

The issue with evaluating my binary fraction interpretation with what’s current in an actual quantity’s bit sample is that the bits within the binary32 format are shifted primarily based on an exponent worth. Additional, the main ‘1’ is faraway from the ensuing quantity. (The main 1 bit is “assumed” on this format.)

For instance, right here’s output for the fraction 0.375 from the code offered in final week’s Lesson:

Enter a optimistic decimal: 0.375
0.011

The worth 0.375 is saved within the binary 32 format as this bit sample:

00111110110000000000000000000000

Determine 1 reveals how these bits type out.

Determine 1. How the fraction is extracted from the binary32 format.

Within the determine, you see the signal bit is 0, optimistic. The exponent is 0x7D. The mantissa is 0x400000. To check the outcome from my code in final week’s Lesson with the fraction as saved within the binary32 requires some manipulation. This job is dealt with within the following replace to the code from final week’s Lesson:

2026_11_21-Lesson.c


#embody <stdio.h>
#embody <stdlib.h>


void binout(unsigned b)
{
    int x,exponent;

    
    exponent = b & 0x7F800000;
    exponent >>= 23;
    
    exponent -= 127;
    
    b &= 0x00EFFFFF;
    
    b |= 0x00800000;

    
    printf("binary32: 0.");
    for( x=0; x<32; x++ )
    {
        
        if( x>=9+exponent )
            putchar( b & 0x80000000 ? '1' : '0' );
        b <<= 1;
    }
    putchar('n');
}

int major()
{
    union actual {
        float f;
        lengthy l;
    } numb;
    int x;

    
    printf("Enter a optimistic decimal: ");
    scanf("%f",&numb.f);
    if( numb.f>=1.0 )
    {
        places("Enter a worth between 0.0 to 1.0");
        return(-1);
    }

    
    binout(numb.l);

    
    printf(" extract: 0.");
    for( x=0; x<32; x++ )
    {
        numb.f *= 2.0;
        if( numb.f==0 )
            break;
        if( numb.f>=1.0 )
        {
            putchar('1');
            numb.f -= 1.0;
        }
        else
            putchar('0');
    }
    putchar('n');

    
    return 0;
}

The binout() operate outputs a string representing the binary values saved within the fractional a part of a binary32 formatted actual quantity. It accomplishes this process in a number of steps.

First, the exponent worth is extracted:

exponent = b & 0x7F800000;
exponent >>= 23;

The bitwise & operator masks out the exponent worth, which is then shifted 23 locations proper. This course of is described in a earlier Lesson. As a result of the worth relies on the usual bias of 127 (per the binary32 format), it should be adjusted to acquire the true exponent worth:

exponent -= 127;

The outcome determines how the binary fraction is shifted, left or proper primarily based on the exponent’s signal.

Subsequent, the binary fraction is pulled from the remainder of the quantity:

b &= 0x00EFFFFF;

The bitwise & masks out the binary fraction. However the main 1 bit is assumed, so I add it again in with a bitwise | (OR) operator:

b |= 0x00800000;

The rest of the code outputs the binary fraction, however solely given the offset supplied by the exponent worth in variable exponent. This worth shifts the quantity’s beginning place when the result’s output contained in the for loop.

The aim of all these steps is to check the binary fraction because it’s saved within the binary32 format towards what my code generated from final week’s Lesson. Right here’s a pattern run:

Enter a optimistic decimal: 0.375
binary32: 0.0110000000000000000000000
 extract: 0.011

The values match. Listed here are just a few extra examples that match:

Enter a optimistic decimal: 0.625
binary32: 0.101000000000000000000000
 extract: 0.101

Enter a optimistic decimal: 0.1
binary32: 0.000110011001100110011001101
 extract: 0.000110011001100110011001101

However not all fractions match:

Enter a optimistic decimal: .999
binary32: 0.111011111011111001110111
 extract: 0.111111111011111001110111

Enter a optimistic decimal: 0.825
binary32: 0.110000110011001100110011
 extract: 0.110100110011001100110011

For the fractions 0.999 and 0.825, you see an additional bit in my output verses what’s output from the binary32 format. I clarify the funkiness behind this discrepancy in subsequent week’s Lesson.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments