# 2 complementary representation of fractions?

I lost it a bit. I need to use two fractional bits `0.(a-1)(a-2)`

Now I can use `.00 .01 .10`

and `.11`

But I also need negative numbers (in 2's complement), so what `.10`

be `-.5`

? or will it be `-.25`

? Same with `.11`

, will it be `-.75`

? or will it be `-.5`

? I'm sure this will be the first in both cases, but I'm not entirely sure.

+3

source to share

In 2-digit notation, all of the most significant bits of a negative number are set to 1. Suppose you store these numbers as 8 bits, and 2 to the right of the "binary dot".

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
```

```

etc.

Using 8 bits, for example, the largest amount you can store,

``````011111.11 = 31.75
```

```

smallest positive number

``````000000.01 = 0.25
```

```

smallest negative number

``````111111.11 = -0.25
```

```

and the smallest (i.e. the most negative)

``````100000.00 = -32
```

```
+5

source

see like this:

you have a normal binary representation

let's say 8-bit words ...

the first bit (MSB) is 128, the second is 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 that our 8-bit word has 2 decimal places ...

now we start with the first bit (MSB) 2 ^ 5 and end with the last bit beeing 2 ^ -2

there is no magic here ...

now to turn this into binary's complement: just negate the value of the first bit

so instead of 2 ^ 5 it would be -2 ^ 5

so base 10 -0.75 will be in binary's 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))

+1

source

The number stored in two's complement inverts the sign of the highest bit value (so, for example, for a 16-bit number, the top bit is -32768, not +32768). All other bits behave as usual. Therefore, when doing math on verbose numbers, the top word of each number should be treated as two's complement (since its topmost bit would be the topmost bit of the total), but all other words in each number should be treated as unsigned quantities.

For example, a 16-bit binary's complement number has location values ​​(-32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, and 1). Divide into two 8-bit parts, these parts will have space values ​​(-32768, 16384, 8192, 4096, 2048, 1024, 512 and 256); and (128, 64, 32, 16, 8, 4, 2 and 1). The first set of values ​​is in a two-part 8-bit number, times 256; the last set is an unsigned 8-bit number.

0

source

All Articles