Download - Binary numbers and arithmetic
![Page 1: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/1.jpg)
Binary numbers and arithmetic
![Page 2: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/2.jpg)
ADDITION
![Page 3: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/3.jpg)
Addition (decimal)
5
4
1
19
14
5
10
5
51
11
5
61
111
99
21 11
![Page 4: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/4.jpg)
Addition (binary)
0
0
0
1
0
1
1
1
0
10
1
11
![Page 5: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/5.jpg)
Addition (binary)
11000
01011
101101111
![Page 6: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/6.jpg)
Addition (binary)
So can we count in binary?
0
0
0
1
0
1
1
1
0
10
1
11
![Page 7: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/7.jpg)
Counting in binary (4 bits)0123456789101112131415
00000001…
![Page 8: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/8.jpg)
MULTIPLICATION
![Page 9: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/9.jpg)
Multiplication (decimal)
143
130
13
11
13
![Page 10: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/10.jpg)
Multiplication (binary)
10001111
1101000
11010
1101
1011
1101
![Page 11: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/11.jpg)
Multiplication (binary)
10001111
1101000
11010
1101
1011
1101
It’s interesting to note that binary multiplication is a sequence of shifts and adds of the first term (depending on the bits in the second term.
110100 is missing here because the corresponding bit in the second terms is 0.
![Page 12: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/12.jpg)
REPRESENTING SIGNED (POSITIVE AND NEGATIVE) NUMBERS
![Page 13: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/13.jpg)
Representing numbers (ints)
• Fixed, finite number of bits.
bits bytes C/C++ Intel Sun8 1 char [s]byte byte16 2 short [s]word half32 4 int or long [s]dword word64 8 long long [s]qword xword
![Page 14: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/14.jpg)
Representing numbers (ints)
• Fixed, finite number of bits.
bits Intel signed unsigned8 [s]byte -27..+27-1 0..+28-116 [s]word -215..+215-1 0..+216-132 [s]dword -231..+231-1 0..+232-164 [s]qword -263..+263-1 0..+264-1
In general, for k bits, the unsigned range is [0..+2k-1] and the signed range is [-2k-1..+2k-1-1].
![Page 15: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/15.jpg)
Methods for representing signed ints.
1. signed magnitude
2. 1’s complement (diminished radix complement)
3. 2’s complement (radix complement)
4. excess bD-1
![Page 16: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/16.jpg)
Signed magnitude
• Ex. 4-bit signed magnitude– 1 bit for sign– 3 bits for magnitude
111101117
111001106
110101015
110001004
101100113
101000102
100100011
100000000
NN
![Page 17: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/17.jpg)
Signed magnitude
• Ex. 4-bit signed magnitude– 1 bit for sign– 3 bits for magnitude
111101117
111001106
110101015
110001004
101100113
101000102
100100011
100000000
NN
![Page 18: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/18.jpg)
1’s complement(diminished radix complement)
• Let x be a non-negative number.• Then –x is represented by bD-1+(-x) where
b = baseD = (total) # of bits (including the sign bit)
• Ex. Let b=2 and D=4.Then -1 is represented by 24-1-1 = 1410 or 11102.
![Page 19: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/19.jpg)
1’s complement(diminished radix complement)
• Let x be a non-negative number.• Then –x is represented by bD-1+(-x) where
b = base & D = (total) # of bits (including the sign bit)
• Ex. What is the 9’s complement of 1238910?Given b=10 and D=5. Then the 9’s complement of 12389= 105 – 1 – 12389= 100000 – 1 – 12389= 99999 – 12389= 87610
![Page 20: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/20.jpg)
1’s complement(diminished radix complement)
• Let x be a non-negative number.• Then –x is represented by bD-1+
(-x) whereb = baseD = (total) # of bits (including the
sign bit)
• Shortcut for base 2?– All combinations used, but 2
zeros!100001117
100101106
101001015
101101004
110000113
110100102
111000011
111100000
NN
![Page 21: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/21.jpg)
2’s complement(radix complement)
• Let x be a non-negative number.• Then –x is represented by bD+(-x).
– Ex. Let b=2 and D=4. Then -1 is represented by 24-1 = 15 or 11112.
– Ex. Let b=2 and D=4. Then -5 is represented by 24 – 5 = 11 or 10112.
– Ex. Let b=10 and D=5. Then the 10’s complement of 12389 = 105 – 12389 = 100000 – 12389 = 87611.
![Page 22: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/22.jpg)
2’s complement(radix complement)
• Let x be a non-negative number.• Then –x is represented by bD+(-x).
– Ex. Let b=2 and D=4. Then -1 is represented by 24-1 = 15 or 11112.
– Ex. Let b=2 and D=4. Then -5 is represented by 24 – 5 = 11 or 10112.
• Shortcut for base 2?
100101117
101001106
101101015
110001004
110100113
111000102
111100011
000000000
NN
![Page 23: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/23.jpg)
2’s complement(radix complement)
• Shortcut for base 2?– Yes! Flip the bits and add 1.
100101117
101001106
101101015
110001004
110100113
111000102
111100011
000000000
NN
![Page 24: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/24.jpg)
2’s complement(radix complement)
• Are all combinations of 4 bits used?– No. (Now we only have one
zero.)– 1000 is missing!
• What is 1000?• Is it positive or negative?• Does -8 + 1 = -7 work in 2’s
complement?
100101117
101001106
101101015
110001004
110100113
111000102
111100011
000000000
NN
![Page 25: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/25.jpg)
excess bD-1 (biased representation)
• For pos, neg, and 0, x is represented by
bD-1 + x
• Ex. Let b=2 and D=4. Then the excess 8 (24-1) representation for 0 is 8+0 = 8 or 10002.
• Ex. Let b=2 and D=4. Then excess 8 for -1 is 8 – 1 = 7 or 01112.
![Page 26: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/26.jpg)
excess bD-1
• For pos, neg, and 0, x is represented bybD-1 + x.
• Ex. Let b=2 and D=4. Then the excess 8 (24-1) representation for 0 is 8+0 = 8 or 10002.
• Ex. Let b=2 and D=4. Then excess 8 for -1 is 8 – 1 = 7 or 01112.
000111117
001011106
001111015
010011004
010110113
011010102
011110011
100010000
NN
![Page 27: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/27.jpg)
2’s complement vs. excess bD-1
• In 2’s, positives start with 0; in excess, positives start with 1.
• Both have one zero (positive).
• Remaining bits are the same.
000111117
001011106
001111015
010011004
010110113
011010102
011110011
100010000
NN
![Page 28: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/28.jpg)
Summary of methods for representing signed ints.
1111000110011000111101117
1110001010101001111001106
1101001110111010110101015
1100010011001011110001004
1011010111011100101100113
1010011011101101101000102
1001011111111110100100011
1000100000001111100000000
821
nnnnnnN
excesssCompsCompsignedMag
1000=-8| 0000 unused
![Page 29: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/29.jpg)
BINARY ARITHMETIC
Signed magnitude1’s complement2’s complementExcess K (biased)
![Page 30: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/30.jpg)
BINARY ARITHMETICSigned magnitude
![Page 31: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/31.jpg)
Addition w/ signed magnitude algorithm
• For A - B, change the sign of B and perform addition of A + (-B) (as in the next step)
• For A + B:• if (Asign==Bsign) then { R = |A| + |B|; Rsign = Asign; }
• else if (|A|>|B|) then { R = |A| - |B|; Rsign = Asign; }
• else if (|A|==|B|) then { R = 0; Rsign = 0; }
• else { R = |B| - |A|; Rsign = Bsign; }
• Complicated?
![Page 32: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/32.jpg)
BINARY ARITHMETIC2’s complement
![Page 33: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/33.jpg)
Representing numbers (ints) using 2’s complement
• Fixed, finite number of bits.
bits Intel signed8 sbyte -27..+27-116 sword -215..+215-132 sdword -231..+231-164 sqword -263..+263-1
In general, for k bits, the signed range is [-2k-1..+2k-1-1].So where does the extra negative value come from?
![Page 34: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/34.jpg)
Representing numbers (ints)
• Fixed, finite number of bits.
bits Intel signed8 sbyte -27..+27-116 sword -215..+215-132 sdword -231..+231-164 sqword -263..+263-1
In general, for k bits, the signed range is[-2k-1..+2k-1-1].So where does the extra negative value come
from?
10008
100101117
101001106
101101015
110001004
110100113
111000102
111100011
000000000
nn
![Page 35: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/35.jpg)
Addition of 2’s complement binary numbers
• Consider 8-bit 2’s complement binary numbers.– Then the msb (bit 7) is the sign bit. If this bit is 0,
then this is a positive number; if this bit is 1, then this is a negative number.
– Addition of 2 positive numbers.– Ex. 40 + 58 = 98
10001010
11101000
10000100111
![Page 36: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/36.jpg)
Addition of 2’s complement binary numbers
• Consider 8-bit 2’s complement binary numbers.– Addition of a negative to a
positive.
– What are the values of these 2 terms?
• -88 and 122• -88 + 122 = 34
00100010 1
01111010
100001011111
![Page 37: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/37.jpg)
So how can we perform subtraction?
![Page 38: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/38.jpg)
Addition of 2’s complement binary numbers
• Consider 8-bit 2’s complement binary numbers.
• Subtraction is nothing but addition of the 2’s complement.– Ex. 58 – 40 = 58 + (-40) = 18
00010010 1
11011000
101011001111
discard carry
![Page 39: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/39.jpg)
Carry vs. overflow
![Page 40: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/40.jpg)
Addition of 2’s complement binary numbers
• Carry vs. overflow when adding A + B– If A and B are of opposite sign, then overflow
cannot occur.
– If A and B are of the same sign but the result is of the opposite sign, then overflow has occurred (and the answer is therefore incorrect).
• Overflow occurs iff the carry into the sign bit differs from the carry out of the sign bit.
![Page 41: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/41.jpg)
Addition of 2’s complement binary numbers
class test { public static void main ( String args[] ) { byte A = 127; byte B = 127; byte result = (byte)(A + B); System.out.println( "A + B = " + result ); }}
#include <stdio.h>
int main ( int argc, char* argv[] ){ char A = 127; char B = 127; char result = (char)(A + B); printf( "A + B = %d \n", result );
return 0;}
Result = -2 in both Java (left) and C++ (right). Why?
![Page 42: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/42.jpg)
Addition of 2’s complement binary numbers
class test { public static void main ( String args[] ) { byte A = 127; byte B = 127; byte result = (byte)(A + B); System.out.println( "A + B = " + result ); }}
Result = -2 in both Java and C++.Why?What’s 127 as a 2’s complement
binary number?
What is 111111102?
Flip the bits: 00000001.Then add 1: 00000010.This is -2.
11111110
01111111
01111111
![Page 43: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/43.jpg)
BINARY ARITHMETIC1’s complement
![Page 44: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/44.jpg)
Addition with 1’s complement
• Note: 1’s complement has two 0’s!
• 1’s complement addition is tricky (end-around-carry).
100001117
100101106
101001015
101101004
110000113
110100102
111000011
111100000
NN
![Page 45: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/45.jpg)
8-bit 1’s complement addition
• Ex. Let X = A816 and Y = 8616.
• Calculate Y - X using 1’s complement.
![Page 46: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/46.jpg)
8-bit 1’s complement addition
• Ex. Let X = A816 and Y = 8616.
• Calculate Y - X using 1’s complement. Y = 1000 01102 = -12110
X = 1010 10002 = -8710
~X = 0101 01112
(Note: C=0 out of msb.)
1101 1101
0111 0101
0110 1000
Y - X = -121 + 87 = -34 (base 10)
![Page 47: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/47.jpg)
8-bit 1’s complement addition
• Ex. Let X = A816 and Y = 8616.
• Calculate X - Y using 1’s complement.
![Page 48: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/48.jpg)
8-bit 1’s complement addition
• Ex. Let X = A816 and Y = 8616.
• Calculate X - Y using 1’s complement.X = 1010 10002 = -8710
Y = 1000 01102 = -12110
~Y = 0111 10012
(Note: C=1 out of msb.) 0010 0010
1
0001 0010 1
1001 0111
1000 1010
X - Y = -87 + 121 = 34 (base 10)
end around carry
![Page 49: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/49.jpg)
BINARY ARITHMETICExcess K (biased)
![Page 50: Binary numbers and arithmetic](https://reader033.vdocument.in/reader033/viewer/2022061307/56814543550346895db20d50/html5/thumbnails/50.jpg)
Binary arithmetic and Excess K (biased)
Method: Simply add and then flip the sign bit.-1 0111
+5 1101
-- ----
+4 0100 -> flip sign -> 1100
+1 1001
-5 0011
-- ----
-4 1100 -> flip sign -> 0100
+1 1001
+5 1101
-- ----
+6 0110 -> flip sign -> 1110
-1 0111
-5 0011
-- ----
-6 1010 -> toggle sign -> 0010000111117
001011106
001111015
010011004
010110113
011010102
011110011
100010000
NN
(Not used for integer arithmetic but employed in IEEE 754 floating point standard.)