Update
This commit is contained in:
parent
4b7428db54
commit
b5006e62e5
19 changed files with 131 additions and 28 deletions
|
@ -14,7 +14,9 @@ TODO: disadvantages?
|
|||
|
||||
*N* bit number in two's complement can represent numbers from *-(2^N) / 2* to *2^N / 2 - 1* (including both). For example with 8 bits we can represent numbers from -128 to 127.
|
||||
|
||||
**How does it work?** EZ: the highest (leftmost) bit represents the sign: 0 is positive (or zero), 1 is negative. To negate a number negate all its bits and add 1 to the result (with possible overflow). (There is one exception: negating the smallest possible negative number will give the same number as its positive value cannot be represented.)
|
||||
**How does it work?** EZ: the highest (leftmost) bit represents the sign: 0 is positive (or zero), 1 is negative. To negate a number negate all its bits and add 1 to the result (with possible overflow). (There is one exception: negating the smallest possible negative number will give the same number as its positive value cannot be represented.)
|
||||
|
||||
In other words given *N* bits, the positive values representable by two's complement with this bit width are the same as in normal unsigned representation and any representable negative value *-x* corresponds to the value *2^N - x*.
|
||||
|
||||
**Example:** let's say we have a 4 bit number `0010` (2). It is positive because the leftmost bit is 0 and we know it represents 2 because positive numbers are the same as the straightforward representation. To get number -2, i.e. multiply our number by -1, we negate the number, which gives `1101`, and add 1, which gives `1110`. We see by the highest 1 bit that this number is negative, as we expected. As an exercise you may try to negate this number back and see we obtain the original number. Let's just now try adding 2; we expect that adding 2 to -2 will give 0. Sure enough, `1110` + `0010` = `0000`. Etcetc. :)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue