top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How does a float variable store in memory ?

0 votes
677 views
How does a float variable store in memory ?
posted Sep 7, 2014 by Ganesh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

Floating point values are stored in a format defined by the IEEE 754 floating point standard.
This format uses the excess-127 representation for 32 bit numbers and the excess-1023 representation for 64 bit numbers.

The typical format of a 32 bit floating point number is as below.
Sign Bit + Exponent + Mantissa
Sign will occupy one bit, Exponent 8 bits and Mantissa 23 bits.

Now let us see how the floating point value 2.3 is represented.
1) From the value it is clear that it is a positive number. A/c to this standard, if the value is positive then 0 is stored in the sign bit else 1 is stored.
2) The integer 2 is represented as 10 in binary. And if we convert the fractional part into binary [0.3] into binary, we will get 01001100110011001...
3) So, 2.3 is represented in binary as (10.01001100110011001..). It is clear that the pattern 1001 repeats. So, if we convert this to normalized form we have 1.001001*E^1.
4) According to the standard, the exponent value is biased by a fixed value. For 32 bit numbers 127 is added to the exponent and for 64 bit numbers 1023 added. In our example, the exponent 1 [E^1] is added with 127 and when converted the result to binary we will get 10000000.
5) So, 2.3 will be stored as shown below.
0 10000000 00100110 01100110 1010011
If you observe the above format,
a) 0 is stored in sign bit [MSB],
b) 10000000 [exponent + bias value -127] is stored in 8 bit exponent field
c) Mantissa [00100110 01100110 1010011 ] is stored in 23 bits. Observe that the pattern 1001 repeats until 23 rd bit is reached.

Note: similar is the procedure for 64 bit floating point number but with few changes.

Let me know if this answers your question.

answer Sep 8, 2014 by Ranjith
0 votes

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
answer Sep 7, 2014 by Tapesh Kulkarni
Similar Questions
+1 vote

In the following example

int  main()
{
  char *ptr ="shakti";
}

Where does ptr get memory? IN data segment or in stack or code segment ?
Please clarify.

+2 votes

Say my CPU don't have support for the float variables and uses a long type integer (32 bit)

The leftmost bit (MSB) is the sign (0 means +, 1 means -) - lets call it S
The next 8 bits are the exponent - lets call it E.
The rest bits (23) are the Mantissa (i.e the fraction) - lets call it M.
The number is calculated by this formula: (-1)^S * M * (2^E)

Now we need a function

unsigned long add(unsigned long float1, unsigned long float2)

which gets 2 numbers of the representation above, and returns the result of the addition of them (as described above).

...