2's complement representation of fractions?
Solution 1
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
Solution 2
see it this way:
you have normal binary representation
let's assume 8 bit words ...
the first bit (MSB) has the value 128, the second 64, and so on ...
in other words the first bit (MSB) is 2^7 ... the second bit is 2^6 ... and the last bit is 2^0
now we can assume our 8 bit word has 2 decimal places ....
we now start with the first bit (MSB) 2^5 and end with the last bit beeing 2^-2
no magic here ...
now to turn that into binary complement: simply negate the value of the first bit
so instead of 2^5 it would be -2^5
so base 10 -0.75 would be in binary complement
111111.01 ...
(1*(-32) + 1*16 + 1*8 + 1*4 + 1*2 +1*1 + 0*0.5 + 1*0.25)
(1*(-2^5) + 1*2^4 + 1*2^3 + 1*2^2 + 1*2^1 +1*2^0 + 0*2^(-1) + 1*2^(-2))
Tropical_Peach
Don't ever use Arch Linux. It's just the worst and there is an unwritten rule of hate towards asking questions and seeking help. Assembly is the only language I recognize.
Updated on June 27, 2022Comments
-
Tropical_Peach almost 2 years
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. -
Tropical_Peach about 12 yearsThanks, this was closest, but I acctually figured it out...
00=>0=>0/4=>0 01=>1=>1/4=>.25 10=>-2=>-2/4=>-.5 11=>-1=>-1/4=>-.25
-
supercat about 12 yearsIf your number was unsigned, your bits would have values (+1/2) and (+1/4). For two's-complement format, flip the sign of the top bit, so they're (-1/2) and (+1/4). The available values are then 0, 1/4, -1/2, and -1/4.