2's complement representation of fractions?

Tropical_Peach picture Tropical_Peach · Mar 30, 2012 · Viewed 13.1k times · Source

I'm a little lost on this. I need to use two fractional bits 0.(a-1)(a-2)

Like that, now I can use .00 .01 .10 and .11 But I need negative numbers (in 2's complement) also, so would .10 be -.5 ? or would it be -.25 ? The same with .11 , that would be -.75? or would it be -.5 ? I'm pretty sure it would be the former in both cases, but I'm not entirely positive.

Answer

Adam Liss picture Adam Liss · Mar 30, 2012

In two's complement notation, all of the most significant bits of a negative number are set to 1. Let's assume you're storing these numbers as 8 bits, with 2 to the right of the "binary point."

By definition, x + -x = 0, so we can write:

0.5  +  -0.5 = 0.10 + 111111.10 = 0   // -0.5  = 111111.10
0.25 + -0.25 = 0.01 + 111111.11 = 0   // -0.25 = 111111.11
0.75 + -0.75 = 0.11 + 111111.01 = 0   // -0.75 = 111111.01

and so on.

Using 8 bits like this, the largest number you can store is

011111.11 = 31.75

the least-positive number is

000000.01 = 0.25

the least-negative number is

111111.11 = -0.25

and the smallest (that is, the most negative) is

100000.00 = -32