views:

316

answers:

4

Let's say I have this:

float i = 1.5

in binary, this float is represented as:

0 01111111 10000000000000000000000

I broke up the binary to represent the 'signed', 'exponent' and 'fraction' chunks.

What I don't understand is how this represents 1.5.

The exponent is 0 once you subtract the bias (127 - 127), and the fraction part with the implicit leading one is 1.1.

How does 1.1 scaled by nothing = 1.5???

+5  A: 

1.1 in binary is 1 + .5 = 1.5

pavpanchekha
+14  A: 

Think first in terms of decimal (base 10): 643.72 is:

  • (6 * 10+2) +
  • (4 * 10+1) +
  • (3 * 10+0) +
  • (7 * 10-1) +
  • (2 * 10-2) or 600 + 40 + 3 + 7/10 + 2/100.

That's because n0 is always one, n-1 is the same as 1/n and n-m is identical to one divided by nm).

Similarly, 1.1 in binary is:

  • (1 * 20) +
  • (1 * 2-1)

with 20 being one and 2-1 being one-half.

In decimal, the numbers to the left of the decimal point have multipliers 1, 10, 100 and so on heading left, and 1/10, 1/100, 1/1000 heading right from the decimal point (i.e., 102, 101, 100, decimal point, 10-1, 10-2, ...).

In base-2, the numbers to the left of the binary point have multipliers 1, 2, 4, 8, 16 and so on heading left. The numbers to the right have multipliers 1/2, 1/4, 1/8 and so on heading right.

So, for example, the binary number:

101.00101

is equivalent to:

4 + 1 + 1/8 + 1/32

or:

    5
5  --
   32
paxdiablo
It's simply "binary point"
Tyler McHenry
Thanks, @Tyler, that looks and sounds much better.
paxdiablo
Very nice answer, pax.
Stephen Canon
A: 

You want to read this - IEEE 754-1985

The actual standard is here

Romain Hippeau
The actual pdfs of all these standards can usually be found by a Google search…
Potatoswatter
@PotatoSwatter That is my point - GIMF
Romain Hippeau
The wikipedia page isn't the standard, and shouldn't be used as such. That's (I think) Potatoswatter's point.
Stephen Canon
Added IEEE site to actual standard - The first site explains it in more layman terms.
Romain Hippeau
My point was also that they can be found for free. I thought about providing a link but that would probably be unwise :v( .
Potatoswatter
A: 

The mantissa is essentially shifted by the exponent.

3 in binary is 0011
3>>1 in binary, equal to 3/2, is 0001.1
drawnonward