Difference between revisions of "User:Saul/cs and math"
m (→One's Compliment) |
(→Two's Compliment) |
||
Line 56: | Line 56: | ||
==== Two's Compliment ==== | ==== Two's Compliment ==== | ||
+ | Two's compliment can represent numbers from '''-2<sup>bits - 1</sup>''' to '''2<sup>bits - 1</sup> - 1''', eg. two's compliment in 4 bits can represent numbers from -8 to 7<br> | ||
+ | Two's compliment is a solution to the problems that one's compliment has.<br> | ||
+ | To turn a binary number to its negative, start by doing the same thing as one's compliment, then just add 1 to the result for example: | ||
+ | <source> | ||
+ | 0101 (unsigned binary : 5 in decimal) | ||
+ | 1010 (one's compliment) | ||
+ | 1011 (two's compliment : -5 in decimal) | ||
+ | </source> | ||
+ | ===== Examples ===== | ||
+ | Here is a few conversions from decimal to binary ('''two's compliment'''): | ||
+ | <source> | ||
+ | 1000 : -8 | ||
+ | 1001 : -7 | ||
+ | 1010 : -6 | ||
+ | 1011 : -5 | ||
+ | 1100 : -4 | ||
+ | 1101 : -3 | ||
+ | 1110 : -2 | ||
+ | 1111 : -1 | ||
+ | 0000 : +0 | ||
+ | 0001 : +1 | ||
+ | 0010 : +2 | ||
+ | 0011 : +3 | ||
+ | 0100 : +4 | ||
+ | 0101 : +5 | ||
+ | 0110 : +6 | ||
+ | 0111 : +7 | ||
+ | </source> | ||
+ | ===== Other Notes ===== | ||
+ | Two's compliment is like the standard way of looking at binary but the most significant bit now represents the negative version of itself as this table shows: | ||
+ | {| class="wikitable" | ||
+ | |- | ||
+ | ! -8 !! 4 !! 2 !! 1 !! Calculation !! Result (Decimal) | ||
+ | |- | ||
+ | | 1 || 0 || 0 || 0 || 1x-8 + 0x4 + 0x2 + 0x1 || -8 | ||
+ | |- | ||
+ | | 1 || 0 || 0 || 1 || 1x-8 + 0x4 + 0x2 + 1x1 || -7 | ||
+ | |- | ||
+ | | 1 || 0 || 1 || 0 || 1x-8 + 0x4 + 1x2 + 0x1 || -6 | ||
+ | |- | ||
+ | | 1 || 0 || 1 || 1 || 1x-8 + 0x4 + 1x2 + 1x1 || -5 | ||
+ | |- | ||
+ | | 1 || 1 || 0 || 0 || 1x-8 + 1x4 + 0x2 + 0x1 || -4 | ||
+ | |- | ||
+ | | 1 || 1 || 0 || 1 || 1x-8 + 1x4 + 0x2 + 1x1 || -3 | ||
+ | |- | ||
+ | | 1 || 1 || 1 || 0 || 1x-8 + 1x4 + 1x2 + 0x1 || -2 | ||
+ | |- | ||
+ | | 1 || 1 || 1 || 1 || 1x-8 + 1x4 + 1x2 + 1x1 || -1 | ||
+ | |- | ||
+ | | 0 || 0 || 0 || 0 || 0x-8 + 0x4 + 0x2 + 0x1 || 0 | ||
+ | |- | ||
+ | | 0 || 0 || 0 || 1 || 0x-8 + 0x4 + 0x2 + 1x1 || 1 | ||
+ | |- | ||
+ | | 0 || 0 || 1 || 0 || 0x-8 + 0x4 + 1x2 + 0x1 || 2 | ||
+ | |- | ||
+ | | 0 || 0 || 1 || 1 || 0x-8 + 0x4 + 1x2 + 1x1 || 3 | ||
+ | |- | ||
+ | | 0 || 1 || 0 || 0 || 0x-8 + 1x4 + 0x2 + 0x1 || 4 | ||
+ | |- | ||
+ | | 0 || 1 || 0 || 1 || 0x-8 + 1x4 + 0x2 + 1x1 || 5 | ||
+ | |- | ||
+ | | 0 || 1 || 1 || 0 || 0x-8 + 1x4 + 1x2 + 0x1 || 6 | ||
+ | |- | ||
+ | | 0 || 1 || 1 || 1 || 0x-8 + 1x4 + 1x2 + 1x1 || 7 | ||
+ | |} |
Revision as of 02:05, 3 August 2019
Contents
Bases
Binary
Unsigned
In unsigned binary all numbers are positive.
Here is a few conversions from decimal to binary:
Binary : Decimal
000 : 0
001 : 1
010 : 2
011 : 3
100 : 4
101 : 5
110 : 6
111 : 7
One's Compliment
One's compliment was an old way of specifying signed binary.
The way of specifying a number as negative in binary was to set the most significant bit (The left most bit) to a 1.
0XXX would be a positive number and 1XXX would be a negative number.
In one's compliment negative numbers are represented by setting the most significant bit (The left most bit) and flipping all the other individual bits to the opposite.
For example to get -2 in binary:
010 (+2 in decimal)
-> 110 (Set the most significant bit to 1)
-> 101 (flip all other bits, -2 in decimal)
Here is a few conversions from decimal to binary (one's compliment):
Binary : Decimal
1000 : -7
1001 : -6
1010 : -5
1011 : -4
1100 : -3
1101 : -2
1110 : -1
1111 : -0
0000 : +0
0001 : +1
0010 : +2
0011 : +3
0100 : +4
0101 : +5
0110 : +6
0111 : +7
The issue with one's compliment is that there are two zeros - 000 = +0, 111 = -0 and adding one's compliment numbers together does not always correctly sum, for example:
5 + -3 = 0 (Decimal)
0101 + 1100 = 10001 = 0001 (drop the left most bit as it is out out of the 4-bit scope)
Two's Compliment
Two's compliment can represent numbers from -2bits - 1 to 2bits - 1 - 1, eg. two's compliment in 4 bits can represent numbers from -8 to 7
Two's compliment is a solution to the problems that one's compliment has.
To turn a binary number to its negative, start by doing the same thing as one's compliment, then just add 1 to the result for example:
0101 (unsigned binary : 5 in decimal)
1010 (one's compliment)
1011 (two's compliment : -5 in decimal)
Examples
Here is a few conversions from decimal to binary (two's compliment):
1000 : -8
1001 : -7
1010 : -6
1011 : -5
1100 : -4
1101 : -3
1110 : -2
1111 : -1
0000 : +0
0001 : +1
0010 : +2
0011 : +3
0100 : +4
0101 : +5
0110 : +6
0111 : +7
Other Notes
Two's compliment is like the standard way of looking at binary but the most significant bit now represents the negative version of itself as this table shows:
-8 | 4 | 2 | 1 | Calculation | Result (Decimal) |
---|---|---|---|---|---|
1 | 0 | 0 | 0 | 1x-8 + 0x4 + 0x2 + 0x1 | -8 |
1 | 0 | 0 | 1 | 1x-8 + 0x4 + 0x2 + 1x1 | -7 |
1 | 0 | 1 | 0 | 1x-8 + 0x4 + 1x2 + 0x1 | -6 |
1 | 0 | 1 | 1 | 1x-8 + 0x4 + 1x2 + 1x1 | -5 |
1 | 1 | 0 | 0 | 1x-8 + 1x4 + 0x2 + 0x1 | -4 |
1 | 1 | 0 | 1 | 1x-8 + 1x4 + 0x2 + 1x1 | -3 |
1 | 1 | 1 | 0 | 1x-8 + 1x4 + 1x2 + 0x1 | -2 |
1 | 1 | 1 | 1 | 1x-8 + 1x4 + 1x2 + 1x1 | -1 |
0 | 0 | 0 | 0 | 0x-8 + 0x4 + 0x2 + 0x1 | 0 |
0 | 0 | 0 | 1 | 0x-8 + 0x4 + 0x2 + 1x1 | 1 |
0 | 0 | 1 | 0 | 0x-8 + 0x4 + 1x2 + 0x1 | 2 |
0 | 0 | 1 | 1 | 0x-8 + 0x4 + 1x2 + 1x1 | 3 |
0 | 1 | 0 | 0 | 0x-8 + 1x4 + 0x2 + 0x1 | 4 |
0 | 1 | 0 | 1 | 0x-8 + 1x4 + 0x2 + 1x1 | 5 |
0 | 1 | 1 | 0 | 0x-8 + 1x4 + 1x2 + 0x1 | 6 |
0 | 1 | 1 | 1 | 0x-8 + 1x4 + 1x2 + 1x1 | 7 |