Thread: Float =>memory Representation

  1. #1
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58

    Float =>memory Representation

    Can anyone Explain how -3.3f is stored in Memory?

    I Need to know how it was done?... I googled it.. I cant understand How...??

    Can u explain me ???

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Moved to Tech Board.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, first of all, we can convert it to a 32-bit number. The highest bit is the sign, and as it is negative, we get 0x80000000.

    Then we have the exponent, which is 127+int(log2(3.3)) -> 127 + 1 = 128.

    So now we have 0x80000000 | (128 << 23) = 0xC0000000.

    Now we have 23 bits of mantissa.
    3.3 / 2^(int(log2(3.3)) to deal with as the mantissa: 3.3 / 2^1 = 1.65.
    First remove the 1. (because it is implied). Leaves 0.65
    First bit:
    0.65 * 2 -> 1.3.
    1.3 > 1.0 => 1
    Second bit:
    (1.3 - (previous bit)) * 2 = 0.6
    0.6 > 1.0 => 0
    Third bit:
    (0.6 - (previous bit)) * 2 = 1.2
    1.2 > 1.0 => 1
    Fourth bit:
    (1.2 - (previous bit)) * 2 = 0.4
    0.4 > 1.0 => 0
    Fifth bit:
    (0.4 - (previous bit)) * 2 = 0.8
    0.8 > 1.0 => 0
    Sixth bit:
    (0.8 - (previous bit)) * 2 = 1.6
    1.6 > 1.0 => 1
    Sixth bit:
    (1.6 - (previous bit)) * 2 = 1.2
    1.2 > 1.0 => 1
    ... Repeat until finished 23rd bit (the sequence now repeats as above for infinity, just like 1/3 or 1/7 in decimal form) .

    Result: 101 0011 0011 0011 0011 0011 (or 53333 in hex)

    Combine this with the exponent and sign:
    0xC0533333

    Edit: as to the order those bytes are stored in memory, that would depend on the endianness of the processor used - x86 will store the high bits on the highest byte-address. Other processors may follow the same or have the "other" endian where the upper bits are stored first in the memory.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58

    Talking Thanks a lot

    Thanks a Lot 4 Such a nice Explanation!!!!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM