Connecting Tech Pros Worldwide Forums | Help | Site Map

how is float arranged in memory

Newbie
 
Join Date: Oct 2006
Posts: 2
#1: Oct 13 '06
How is a float variable arranged in memory.. I know its 4 bytes.

but if I store 5.6 how is the bits arrangement of real and int part. how is the decimal point indicated.

Member
 
Join Date: Oct 2006
Posts: 73
#2: Oct 13 '06

re: how is float arranged in memory


Try this to find out:

#include <stdio.h>

int main()
{

float i=5.6;
unsigned char* a;
a = (unsigned char*)&i;
printf("%d, %d, %d, %d",*a, *(a+1), *(a+2), *(a+3));
return 0;
}
D_C D_C is offline
Needs Regular Fix
 
Join Date: Jun 2006
Posts: 294
#3: Oct 13 '06

re: how is float arranged in memory


If I remember correctly, the first bit is the sign bit (+ or -). The next 23 bits are the mantissa, and the last 8 are the exponent.

They assume that the first bit will eventually be 1, so they don't store the most significant 1 bit. So (sign)*(1+the fractional mantissa)^exponent is the number stored.
Reply