Binary
Arithmetic
Binary Addition
Binary Subtraction
Binary Multiplication
Binary Division
Signed Numbers
Copyright 1993-95, Thomas P. Sturm
Binary Arithmetic
Arithmetic can be done on binary numbers just as it is done on decimal numbers. The only difference is that there are only two symbols in binary (instead of 10 in decimal), hence the tables to "memorize" are much shorter.
Addition:
Addend / 0 / 1Augend
0 / 0 / 1
1 / 1 / 10
Subtraction:
Subtrahend / 0 / 1Minuend
0 / 0 / 1
1 / * / 0
* we have no way of representing negative numbers yet
Multiplication:
Multiplicand / 0 / 1Multiplier
0 / 0 / 0
1 / 0 / 1
Binary Addition
To add two binary numbers, start at the right hand side as you would for decimal numbers.
For example, in an 8-bit computer, add 16 to 6 (decimal)
00010000
+00000110
00010110
Need to worry about a "carry" whenever we add 1 + 1 (binary).
For example, in an 8-bit computer, to add 29 to 77 (decimal)
00011101
+01001101
01101010
However, adding large numbers can present a problem, for example 176 + 230 (decimal):
10110000
+11100110
110010110
A carry off the left side of an unsigned number is overflow. (The result is greater than 255, the largest number that can be held in 8 bits)
Binary Subtraction
To subtract two binary numbers, again start at the right side of the number, as you would for decimal numbers.
For example, in an 8-bit computer, subtract 9 from 15 (decimal)
00001111
-00001001
00000110
Need to worry about a borrow whenever we subtract 1 from 0.
For example, in an 8-bit computer, subtract 9 from 17 (decimal)
00010001
-00001001
00001000
Borrows can span a long distance, for example 17 - 11 (decimal)
00010001
-00001011
00000110
However, subtracting a large number from a small one can create a problem, since we have no way of representing negative numbers. For example:
00010110
-00100100
(1)11110010
Binary Multiplication
To multiply two numbers, we could use the methods learned for decimal numbers, for example 11 x 5
00001011
x00000101
00001011
00000000
00001011
0000110111=00110111
However this is cumbersome for larger numbers, for example 11 x 13
00001011
00001101
00001011
00000000
00001011
00001011
00010001111=10001111
Instead, use the shift-and-add algorithm.
multiplicand: / multiplier: / product00001011 / 00001101 / 00000000
00010110 / 00000110 / 00001011
00101100 / 00000011 / 00001011
01011000 / 00000001 / 00110111
10110000 / 00000000 / 10001111
Binary Division
To divide two numbers, we could use the methods learned for decimal numbers, for example 26 / 5
00000101(quotient)
00000101|00011010
000101
110
101
1(remainder)
Signed Numbers
Subtraction quickly leads to overflow because there are no negative numbers.
We need a way of coding negative numbers. We would prefer a way that would allow the four functions (addition, subtraction, multiplication, and division to "work" without special handling of the sign.
Generally, we use one bit to represent the sign of a number. Generally this is the leftmost bit. The means that about half of the numbers will be positive, and about half of the numbers will be negative.
There are four methods in use today for representing signed numbers. All are used by the VAX in various places.
- Sign/Magnitude
- One's Complement
- Two's Complement
- Biased
Sign/Magnitude
Sign bit is independent of magnitude bits. 0 is positive, 1 is negative.
For example +4 is 00000100 (same as unsigned), -4 is 10000100
(instead of representing 132 in unsigned).
Arithmetic:
Addition:00000100(4)
+10000100+(-4)
10001000(-8)doesn't work directly
(Nor does subtraction, multiplication, or division)
Need to provide special logic circuits to handle the sign.
This is used wherever you would need special logic circuits to handle the sign anyway, such as the sign on a floating point number.
One's Complement
Instead of just changing the sign bit for negative numbers, change all of the bits.
For example, 4 = 00000100 (same as unsigned and sign/magnitude), -4 = 11111011 (instead of 251 in unsigned or -123 in sign/magnitude)
Addition:00000100(4)
+11111100+(-3)
100000000
+1
00000001(1)
Need to "conserve" the carry to get addition to work.
Subtraction:00000100(4)
-00000101-(5)
(1)11111111
-1
11111110(-1)
Multiplication and division also need to conserve carries to work
Addition:00000100(4)
+11111011+(-4)
11111111(-0)or "dirty zero"
One's complement is used primarily for logical operations on the VAX.
Two's Complement
Two's complement is harder to form than one's complement, but doesn't require the adjustments for carry and borrow.
Two's complement doesn't have the "dirty zero" problem inherent in one's complement.
In two's complement, we count down 2 = 00000010, 1 = 00000001, 0 = 00000000, -1 = 11111111, -2 = 11111110
To form the negative of a number, start at the right, copy all of the 0 bits (if any) moving right to left until a 1 bit is encountered, copy the rightmost 1 bit, then toggle (change) all of the remaining bits.
For example, 4 = 00000100 (same as unsigned, sign/magnitude, and one's complement, -4 = 11111100 (252 in unsigned,
-124 in sign/magnitude, -3 in one's complement)
Addition:00000100(4)
+11111100+(-4)
100000000(0)
00000100(4)
+11111101+(-3)
100000001(1)
00000100(4)
+11111011+(-5)
111111111(-1)
Two's Complement Subtraction
Subtraction:
00000100(4)
-00000100-(4)
00000000(0)
00000100(4)
-00000011-(3)
00000001(1)
00000100(4)
-00000101-(5)
(1)11111111(-1)
11111100(-4)
-11111011-(-5)
00000001(1)
11111100(-4)
-11111101-(-3)
(1)11111111(-1)
Multiplication and division also work by ignoring borrows and carries.
Biased representation
Would like a way where there is no discontinuous break from the positive to the negative numbers - want to "count through" zero. Just "bias" the unsigned numbers by half the range.
Consider a 4-bit computer for a change. Examine the 16 bit patterns and see what they represent using each of the "encoding/decoding" conventions.
Binary / Unsigned / Sign/Magnitude / One's
Complement / Two's
Complement / Biased
0000 / 0 / 0 / 0 / 0 / -8
0001 / 1 / 1 / 1 / 1 / -7
0010 / 2 / 2 / 2 / 2 / -6
0011 / 3 / 3 / 3 / 3 / -5
0100 / 4 / 4 / 4 / 4 / -4
0101 / 5 / 5 / 5 / 5 / -3
0110 / 6 / 6 / 6 / 6 / -2
0111 / 7 / 7 / 7 / 7 / -1
1000 / 8 / -0 / -7 / -8 / 0
1001 / 9 / -1 / -6 / -7 / 1
1010 / 10 / -2 / -5 / -6 / 2
1011 / 11 / -3 / -4 / -5 / 3
1100 / 12 / -4 / -3 / -4 / 4
1101 / 13 / -5 / -2 / -3 / 5
1110 / 14 / -6 / -1 / -2 / 6
1111 / 15 / -7 / -0 / -1 / 7
Biased looks like "two's complement with the sign backwards"
Arithmetic doesn't work, but adding unsigned to biased to get biased does.
Used in floating point representations in the exponent portion.
Fundamentals of Computer Science1Binary Arithmetic