1

Below is a tutorial I'm trying to follow.

When attempting to convert the number back to its binary representation I get it wrong. Could someone tell me what I'm doing incorrectly?

-0.21875

binary representation:

.00111 = 1.11 * 2^-3

since..

0.5^3 + 0.5^4 + 0.5^5 = 0.21875

with an exponent of..

-3 + 127 = 124

therefore the binary representation should be:

s eeeeeeee mmmmmmmmmmmmmm
1 01111100 11100000000000

But why do I get the right answer converting 639.6875 the same way?:

Representation of integer value:

1001111111

Representation of decimal value:

1011

Combining and normalizing:

1001111111.1011 = 1.0011111111011 * 2^9

When normalizing, we place the radix directly to the right of the first 1 right? biasing with 127 to the exponent, 127+9 = 136:

10001000

binary representation:

s eeeeeeee mmmmmmmmmmmmmm
0 10001000 10011111111011
Ben Marconi
  • 161
  • 1
  • 1
  • 7

1 Answers1

0

First value

(paraphrased) I get the wrong answer when converting -0.21875 back to binary. Why?

For a normalized floating-point number, the leading '1' is not written into the bit representation. For example, encoding the binary number 1.0001101 would give a mantissa of 0001101(...).

This fact about normalized FP numbers is covered in the presentation slide that you posted - namely it is the part called (1 + f) (instead of just f).

Therefore, your binary representation of -0.21875 must have the mantissa bits 11000000000000000000000 (padded on the right to 23 bits long).

Second value

But why do I get the right answer converting 639.6875 the same way?

You don't get the right answer. The correct representation of 639.6875 is:

s eeeeeeee mmmmmmmmmmmmmmmmmmmmmmm
0 10001000 00111111110110000000000

Code check

We can confirm these values conveniently using Java:

printFloat(0b1_01111100_11000000000000000000000);  // -0.21875
printFloat(0b0_10001000_00111111110110000000000);  // 639.6875
printFloat(0b0_10001000_10011111111011000000000);  // 831.84375

void printFloat(int bits) { 
    System.out.println(Float.intBitsToFloat(bits));
}

A correction

I can see the intent of this line:

0.5^3 + 0.5^4 + 0.5^5 = 0.21875

but it should be written like so:

0.5 * 2^-3 + 0.5 * 2^-4 + 0.5 * 2^-5 = 0.21875
Nayuki
  • 17,911
  • 6
  • 53
  • 80