Saturday, 21 October 2017

Memory representation of float data type in c

Float numbers are stored in exponential form i.e.

(Mantissa)*10^ (Exponent)

Here * indicates multiplication and ^ indicates power.
In memory only Mantissa and Exponent is stored not *, 10 and ^.
Total size of float data type: 32 bit
Those bits are used in following manner:
Exponent bit: 8
Mantissa bit: 24
Mantissa is signed number, so 24 bit are used as:
Mantissa_sign bit: 1
Mantisaa_data bit: 23
For only mantissa:
Mantissa_sign bit will zero if number is positive and Mantissa_sign bit will one if number is negative.
Exponent is also signed number, So 8 bit are used as:
Exponent_sign bit: 1
Exponent_data bit: 7

Following figure illustrate how floating point number is stored in memory.






Five important rules:

Rule 1: To find the mantissa and exponent, we convert data into scientific form.
Rule 2: Before the storing of exponent, 127 is added to exponent.
Rule 3: Exponent is stored in memory in first byte from right to left side.
Rule 4: If exponent will negative number it will be stored in 2’s complement form.
Rule 5: Mantissa is stored in the memory in second byte onward from right to left side.
Example:

Memory representation of:

         float a = -10.3f;


For this you have to follow following steps:

step1: convert the number (10.3) into binary form
Binary value of 10.3 is: 1010.0100110011001100110011001100110011…

step2: convert the above binary number in the scientific form. Scientific form of 1010.0100110011001100110011001100110011…=
1.01001001100110011001100 11001100110011…*10^3
Note: First digit i.e. 1, decimal point symbol, base of power i.e. 10, power symbol ^ and multiplication symbol * are not stored in the memory.

Step3: find exponent and mantissa and signed bit
Mantissa_data bit in binary = 0100100 11001100 11001101           
                      (Only first 23 bit from left side) 
Mantissa_sign bit: 1  (Since it is a negative number)
Exponent in decimal: 3
Question:
Why we have taken right most bit of mantissa_data bit one instead of zero?

Step 5: Add 127 in the exponent and convert in the binary number form.
(Why 127? since size of exponent_data bit is 7 and maximum possible number in seven bit will 1111111 in binary or 127 in decimal)

Exponent= 127+3=130
Binary value of 130 in eight bit: 1000001 0
Exponent_data bit: 1000001
   (Take first seven bit from left side)
Exponent_sign bit: 0 (Take rightmost bit)

Step 6: Now store the Mantissa_data bit, Mantissa_sign bit, Exponent_data bit and Exponent_sign bit at appropriate location as shown in the following figure. 





Note: Mantissa_data bits are stored from left to right while Exponent_data bits are stored from right to left.

How to check above memory representation is correct?

Answer:
We will take one char pointer and visit each byte of a float number and observe the output.
C program:
#include
int main(){
    int i;
    float f=-10.3f;
    char *p=(char *)&f;
    for(i=0;i<4 i="" span="">
         printf("%d   ",*p++);
    return 0;
}
Output: -51 -52 36 -63
Explanation:
Binary value of -51 in eight bit: 11001101
Binary value of -52 in eight bit: 11001100
Binary value of 36 in eight bit:  00100100
Binary value of -63 in eight bit: 11000001
This is exactly same as which we have represented in memory in the above figure.

No comments:

Post a Comment