Binary Arithmetic - Negative Numbers and Subtraction: Not All Integers Are Positive
Binary Arithmetic - Negative Numbers and Subtraction: Not All Integers Are Positive
Binary Mathematics Binary Addition: this is performed using the same rules as decimal except all numbers are limited to combinations of zeros (0) and ones (1). Using 8-bit numbers
1 11
0000 11102 which is 1410 + 0000 01102 which is + 610 -----------------------------0001 01002 2010
1 2 310 - 1 910
------------
1 0 410
If we are using a paper and pencil, binary subtraction can be done using the same principles as decimal subtraction. Binary Subtraction: Use standard mathematical rules: 0000 11102 which is which is 1410
- 0000 01102
0000 10002 ----------------810
- 610
-----------
This is rather straightforward. In fact no borrowing was even required in this example.
-1 -2 -3 -4 -5 -6 -7
If we were using 8-bits the left-most bit will contain the sign. This would leave 7 bits to hold the number. XXXX XXXX | This is the sign bit
This sign bit is reserved and is no longer one of the digits that make up the binary number. Remember if the sign bit is zero (0) the binary number following it is positive. If the sign bit is one (1) the binary number following it is negative. Using the sign-magnitude system the largest positive number that can be stored by an 8-bit computer is:
Sign
(8)
(4)
(2)
(1)
+ 12710
This is: 64 + 32 + 16 + 8 + 4 + 2 + 1 = 12710 If there were a one (1) in the first bit, the number would be equal to - 12710
Sign
(8)
(4)
(2)
(1)
- 12710
Over time it has become obvious that a system that even further reduces the number of available bits while meaningful, is not especially useful. Then of course there is still the problem of how to deal with these positive and negative numbers. While this representation is simple, arithmetic is suddenly impossible. The standard rules of arithmetic dont apply. Creating a whole new way to perform arithmetic isnt overly realistic. Fortunately another technique is available.
Twos Complement
Twos complement is an alternative way of representing negative binary numbers. This alternative coding system also has the unique property that subtraction (or the addition of a negative number) can be performed using addition hardware. Architects of early computers were thus able to build arithmetic and logic units that performed operations of addition and subtraction using only adder hardware. (As it turns out since multiplication is just successive addition and division is just successive subtraction it was possible to use simple adder hardware to perform all of these operations. Lets look at an example:
(8)
(4)
(2)
(1)
Sign (?)
(?)
(?)
(?)
(?)
(?)
(?)
Negative numbers in the 2s complement form need to be obtained by: (1) Finding its corresponding positive number (+6 is: 0 0 0 0 0 1 1 0) (2) Flip all digits (10, 01):
00000110 11111001
(3) Add 1 to the flipped number:
Therefore, since
0 0 0 0 1 1 1 02 +)1 1 1 1 1 0 1 02 --------------------------1 0 0 0 0 1 0 0 02 |
th
Overflow (9 bit in our 8-bit system), IGNORE! It is always a good habit to examine if the answer we obtained (000010002) is correct. We do so by converting it back to decimal numbers and check if its indeed 810.
1210 = 000011002
Finding -910 would require the following three steps: (1) Finding -910s corresponding positive number (+910 is: 0 0 0 0 1 0 0 1) (2) Flip all digits (10, 01):
00001001 11110110
(3) Add 1 to the flipped number:
| IGNORE Overflow
0000 11002 + 1111 01112 -----------------------1 0000 00112 this is the positive number 3
2510 = 000110012
Finding -1410 would require the following three steps: (1) Finding -1410s corresponding positive number (+1410 is: 0 0 0 0 1 1 1 0) (2) Flip all digits (10, 01):
00001110 11110001
(3) Add 1 to the flipped number:
| IGNORE Overflow
0001 10012 + 1111 00102 -----------------------1 0000 10112 this is the positive number 11
IT Worked!
910 = 000010012
Finding -1410 would require the following three steps: (1) Finding -1410s corresponding positive number (+1410 is: 0 0 0 0 1 1 1 0) (2) Flip all digits (10, 01):
00001110 11110001
(3) Add 1 to the flipped number:
Ugh Since the answer starts with a 1 in its sign bit, it is a negative number. However, since there is no direct meaning for each digit for a negative 2s complement number, well need to find its corresponding positive number in order to find out: (Note that we do the flipping/adding 1 thing to convert any positive 2s complement number to its corresponding negative number and vice versa):
1111 10112 flip all digits: 0000 01002 add 1 +) 0000 00012 --------------------0000 01012
Example 5: -25 + 18
Step 1: Determine the 2s complement form for the numbers:
1810 = 000100102
Finding -2510 would require the following three steps: (1) Finding -2510s corresponding positive number (+2510 is: 0 0 0 1 1 0 0 1) (2) Flip all digits (10, 01):
00011001 11100110
(3) Add 1 to the flipped number:
Ugh Since the answer starts with a 1 in its sign bit, it is a negative number. However, since there is no direct meaning for each digit for a negative 2s complement number, well need to find its corresponding positive number in order to find out: (Note that we do the flipping/adding 1 thing to convert any positive 2s complement number to its corresponding negative number and vice versa):
1111 10012 flip all digits: 0000 01102 add 1 +) 0000 00012 --------------------0000 01112