Computer Arithmatic
Computer Arithmatic
Computer Arithmatic
Lecture 08
(Computer Arithmetic)
Review – multiplying powers
• For common bases, add powers.
ab ac = ab+c
or…
26 210 = 64 210 = 64k
Binary Addition (1 of 2)
• Two 1-bit values
A B A+B
0 0 0
0 1 1
1 0 1
1 1 10
“two”
BINARY ADDITION
• Two n-bit values
– Add individual bits
– Propagate carries
– Example:
1
10101 21
+ 11001 + 25
101110 46
EXAMPLE
• Add 010111012 and 001100102
• Solution:
1 1 1 0 0 0 0
0 1 0 1 1 1 0 1
+ 0 0 1 1 0 0 1 0
1 0 0 0 1 1 1 1
EXAMPLE
• Add 111101 and 10111.
1 1 1 1 1 1
1 1 1 1 0 1 = 61
+ 1 0 1 1 1 = 23
1 0 1 0 1 0 0 = 84
≥ (2)10
Multiplication (1 of 3)
• Decimal (just for fun)
35
x 105
175
000
35
3675
Multiplication (2 of 3)
• Binary, two 1-bit values
A B AB
0 0 0
0 1 0
1 0 0
1 1 1
Multiplication (3 of 3)
• Binary, two n-bit values
– As with decimal values
– E.g.,
1110
x 1011
1110
1110
0000
1110
10011010
MULTIPLICATION
• Decimal
35
x 105
175
000
35
3675
EXAMPLE
• Multiply the Binary numbers 1111 and 111.
1111
x 111
1111
1111
1111
1101001
EXAMPLE
• Bit by bit.
1 0 1 1 1
x 1 0 1 0
0 0 0 0 0
1 0 1 1 1
0 0 0 0 0
1 0 1 1 1
1 1 1 0 0 1 1 0
REPRESENTATION OF NEGATIVE
NUMBERS
• There are many methods of representing signed
number in binary:
– Sign-Magnitude Method
– 1’s Complement Method
– 2’s Complement Method
– Access Notation
1’s COMPLEMENT METHOD
• 1’s complement of a binary number can be
directly obtained by changing all 0’s to 1’s and
all 1’s to 0’s.
• Example:
Take 1’s complement of the binary number
01100110 directly.
Original number 01100110
1’s Complement 10011001
Representation of Negative Numbers
using 1’s Complement Method
• Algorithm
– First determine the number of bits to represent
the number.
– Convert the modules of the given number in
binary.
– Place a 0 in MSB and binary conversion of the
number in remaining bits.
– Take 1’s complement of the result.
Example
• Represent -5410 in 1’s complement form using 8
bits.
2 54
2 27 0 5410 = 01101102
2 13 1 5410 = 001101102 (in 8 bits)
2 6 1 So -5410 in 1’s complement
2 3 0 form:
2 1 1 -5410 = 110010012
0 1
2’s COMPLEMENT METHOD
• Algorithm
– First taking 1’s complement.
– And then adding 1 in the result.
Example
• Take 2’s complement of the binary number
01100110 directly.
A B A+B
0 0 0
0 1 1
1 0 1
1 1 10
“two”
BINARY SUBTRACTION
• Modern day computers use 2’s complement
or 1’s complement method for performing
subtraction.
• Example:
Calculate 38 – 29 using 8-bit 1’s complement
method.
38 + (-29)
• Write both the numbers in binary form using
8 bits.
3810 = 001001102
2910 = 000111012
-2910 = 111000102
Add the number & its 1’s complement
001001102
+ 111000102
Carry: 1
Add End 2Carry:+
00001000 1
000010012
EXAMPLE
• Calculate – 54 – 30 using 8-bit 1’s complement
method.
• Solution:
(– 54) + (– 30)
5410 = 001101102
3010 = 000111102
Represent negative numbers in 1’s complement
-5410 = 110010012
-3010 = 111000012
Add numbers in its 1’s complement form
110010012
+ 111000012
Carry: 1
Add End 2Carry:+
10101010 1
101010112
a) 0111000 + 1011100
b) 25 – 50
c) –26 – 48
d) – 47 + 50
SUBTRACTING USING 2’s COMPLEMENT
METHOD
• Example:
Calculate 38 – 29 using 8-bit 2’s complement
method.
Solution:
38 + (-29)
3810 = 001001102
2910 = 000111012
Represent negative numbers in 2’s complement
001001102
+ 111000112
Carry: 1
000010012
Ignore Carry, the answer is
00001001
EXAMPLE
• Calculate – 54 – 30 using 8-bit 2’s complement
method.
• Solution:
(– 54) + (– 30)
5410 = 001101102
3010 = 000111102
Represent negative numbers in 2’s complement
-5410 = 110010102
-3010 = 111000102
Add 2’s complement representation
110010102
+ 111000102
Carry: 1
101011002
Ignore Carry, the answer is
10101100
Convert the 2’s complement result into decimal.
As MSB is 1 so it’s a negative number
a) 25 – 50
b) –26 – 48
c) – 47 + 50
FRACTIONS
• Decimal to decimal.
630.48 = 408.510
DECIMAL TO OCTAL
• 185.310
18510 = 02718
0.310 = 0.231468
185.310 = 0271.231468
HEXADECIMAL TO DECIMAL
• 2B.C416
758.D116 = 1880.816410
DECIMAL TO HEXADECIMAL
• Convert 0.310 into hexadecimal
Result Fractional Part Integral Part
16 x 0.3 4.8 8 4
16 x 0.8 12.8 8 12 = C
16 x 0.8 12.8 8 12 = C
0.310 = 0.4C16
18510 = B916
0.310 = 0.4C16
185.310 = B9.4C16
DECIMAL TO BINARY
• Convert 0.5610 into binary. Give answer up to
6 decimals.
Result Fractional Part Integral Part
2 x 0.56 1.12 12 1
2 x 0.12 0.24 24 0
2 x 0.24 0.48 48 0
2 x 0.48 0.96 96 0
2 x 0.96 1.92 92 1
2 x 0.92 1.84 84 1
2 x 0.84 1.68 68 1
2 x 0.68 1.36 36 1
0.5610 = 0.100011112
EXAMPLE
• Convert 56.2510 into binary.
2 56 Result
Fractional Integral
2 28 0 Part Part
2 14 0 2 x 0.25 0.5 5 0
2 7 0 2 x 0.50 1.0 0 1
2 3 1
2 1 1
0 1 5610 = 01110002
0.2510 = 0.012
56.2510 = 0111000.012
HEXADECIMAL TO BINARY
• Convert A1.0316 into Binary.
A = 1010
1 = 0001
0 = 0000
3 = 0011
Remember that
A = Ten
B = Eleven
C = Twelve A1.0316 = 10100001.000000112
D = Thirteen
E = Fourteen
F = Fifteen
BINARY TO HEXADECIMAL
• Convert 101100.12 into hexadecimal.
• Solution:
• First divide your number into groups of 4 bits
starting from the right.
0010 1100 . 1000
2 C 8
101100.12 = 2C.816
OCTAL − HEXADECIMAL
• Convert to Binary as an intermediate step
Example:
( 2 6 . 2 )8
( 0 1 0 1 1 0 . 0 1 0 )2
(1 6 . 4 )16
Hexa-
Decimal Binary Octal decimal
29.8 11101.110011… 35.63… 1D.CC…
5.8125 101.1101 5.64 5.D
3.109375 11.000111 3.07 3.1C
12.5078125 1100.10000010 14.404 C.82