Within the C language, the versatile storage supplied by a union permits me to entry and study the bits saved in an actual quantity. As a consequence of totally different knowledge sorts accessing the identical knowledge, I can take a look at the bits in a float as a lengthy int. From there, I can output the person bits to see what every one represents, which was coated in final week’s Lesson. On this Lesson, I replace the code to look at the person components of the binary32 format.
Right here is the up to date code:
2026_10_31-Lesson.c
#embrace <stdio.h>
#embrace <stdlib.h>
char *binString(lengthy a)
{
char *bin;
int x;
bin = malloc(sizeof(char) * 33);
if( bin==NULL )
{
fprintf(stderr,"Unable to allocate memoryn");
exit(1);
}
for(x=0; x<32; x++ )
{
*(bin+x) = a & 0x80000000 ? '1' : '0';
a <<= 1;
}
*(bin+x) = ' ';
return(bin);
}
int foremost()
{
union actual {
float f;
lengthy l;
} numb;
unsigned signbit,exponent,mantissa;
printf("Enter a price: ");
scanf("%f",&numb.f);
printf("Integer worth is %ldn",numb.l);
printf("Binary worth is %sn",binString(numb.l));
signbit = numb.l & 0x80000000;
signbit >>= 31;
printf("Signal bit is 0xpercentXn",signbit);
exponent = numb.l & 0x7F800000;
exponent >>= 23;
printf("Exponent is 0xpercentXn",exponent);
mantissa = numb.l & 0x00EFFFFF;
printf("Fraction is 0xpercentXn",mantissa);
return 0;
}
The stringBin() perform interprets a binary string into an integer worth. This perform was launched in this Lesson from 2018. It’s utilized in final week’s code as nicely.
The foremost() perform splits the binary32 actual quantity into its part components: signal, exponent, and fraction. Integer values for every are output.
The method works equally for every step: masks off a particular portion of the binary quantity then shift the masked portion proper in order that its worth is represented. Determine 1 illustrates the method.
Determine 1. How a price is remoted with an & (and) masks, then shifted to pluck it out of the longer binary quantity.
Listed below are the person steps to isolate the exponent as illustrated in Determine 1. This worth is 8 bits in size set at offset 1 within the unique binary worth:
A bitwise AND (&) masks is utilized to filter out solely the 8 bits of exponent worth from the lengthy int saved in numb.b:
exponent = numb.b & 0x7F800000;
Then these 8 bits are shifted proper 23 locations to set them in the beginning of the quantity:
exponent >>= 23;
The impact of those two statements is to single out the exponent portion of the binary32 format and make it into its personal integer worth. As soon as modified, the worth is output:
printf("Exponent is 0xpercentXn",exponent);
The method repeats for every portion of the binary32 encoded quantity.
Right here’s a pattern run:
Enter a price: 12.375 Integer worth is 1095106560 Binary worth is 01000001010001100000000000000000 Signal bit is 0x0 Exponent is 0x82 Fraction is 0x460000
For the worth 12.375, the signal bit is zero — optimistic. The exponent is 0x82 and the faction is 0x460000.
The signal bit a part of these values that’s cleanly comprehensible. As with a signed integer worth, 0 is optimistic and 1 is adverse. Actually, for those who enter -12.375, right here is the output for comparability:
Enter a price: -13.375 Integer worth is 3243638784 Binary worth is 11000001010101100000000000000000 Signal bit is 0x1 Exponent is 0x82 Fraction is 0x460000
The signal bit is now 1, however the different values, exponent and fraction, are the identical.
The worth 0x82 (130 decimal) doesn’t make sense to me as an exponent. Ditto for the fraction 0x460000 (4,587,520 decimal), which is equally complicated. Clearly, some type of translation is occurring that permits for the interpretation of those portions to symbolize 12.375.
I cowl how the values translate in subsequent week’s Lesson.

