Computer Arithmatic

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 48

Computer Education

Lecture 08
(Computer Arithmetic)
Review – multiplying powers
• For common bases, add powers.

ab  ac = ab+c

26  210 = 216 = 65,536

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 AB
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.

Original number 01100110


1’s Complement 10011001
+1
2’s Complement 10011010
Representation of Negative Numbers
using 2’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 2’s complement of the result.
Example
• Represent -5410 in 2’s complement form using 8
bits. Modulus of -5410 = 54
2 54
2 27 0 5410 = 01101102
2 13 1 5410 = 001101102 (in 8 bits)
2 6 1 Take 1’s complement of
2 3 0 00110110
2 1 1 = 11001001
0 1 + 1
= 110010102
BINARY ADDITION
• Two 1-bit values

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

Represent negative numbers in 1’s complement

-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

Convert the 1’s complement result into


decimal. As MSB is 1 so it’s a negative
number

101010112 = 01010100 = -84


Exercises
• Perform following calculations using 1st
complement method.

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

Take 1’s complement of 29 =


00011101 and then add 1

-2910 = 111000112 (2’s complement)

Add 2’s complement representation


and ignore the end carry.

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

101011002 = 01010011 = -84


Exercises
• Perform following calculations using 2nd
complement method.

a) 25 – 50
b) –26 – 48
c) – 47 + 50
FRACTIONS
• Decimal to decimal.

3.14 => 4 x 10-2 = 0.04


1 x 10-1 = 0.1
3 x 100 = 3
3.14
FRACTIONS
• Binary to Decimal
10.1011 => 1 x 2-4 = 0.0625
1 x 2-3 = 0.125
0 x 2-2 = 0.0
1 x 2-1 = 0.5
0 x 20 = 0.0
1 x 21 = 2.0
2.6875
DECIMAL TO BINARY
• 3.14579
.14579
x 2
3.14579 0.29158
x 2
0.58316
x 2
1.16632
x 2
0.33264
x 2
0.66528
x 2
1.33056
11.001001... etc.
OCTAL TO DECIMAL
• 127.548

127.548 => 1 x 82 = 64.000


2 x 81 = 16.000
7 x 80 = 7.000
5 x 8-1 = 0.625
4 x 8-2 = 0.0625
87.687510
EXAMPLE
• Convert 630.48 into decimal equivalent.

630.4 => 6 x 82 = 384


3 x 81 = 24
0 x 80 = 0
4 x 8-1 = 0.5
408.5

630.48 = 408.510
DECIMAL TO OCTAL
• 185.310

8 185 Result Fractional Part Integral Part


8 23 1 8 x 0.3 2.4 4 2
8 2 7 8 x 0.4 3.2 2 3
0 2 8 x 0.2 1.6 6 1
8 x 0.6 4.8 8 4
8 x 0.8 6.4 4 6

18510 = 02718
0.310 = 0.231468
185.310 = 0271.231468
HEXADECIMAL TO DECIMAL
• 2B.C416

2B.C416 => 2 x 161 = 32.000


B x 160 = 11.000
C x 16-1 = .075
4 x 16-2 = .015
43.09010
Remember that
A = Ten
B = Eleven
C = Twelve
D = Thirteen
E = Fourteen
F = Fifteen
HEXADECIMAL TO DECIMAL
• Convert 758.D116 into decimal equivalent.
758.D1 => 7 x 162 = 1792
5 x 161 = 80
8 x 160 = 8
D x 16-1 = 0.8125
1 x 16-2 = 0.0039
1880.8164

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

• Because C is the repeating value therefore, as


a convention, we shall take it once only.
EXAMPLE
• Convert 185.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

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

Assume Zeros Assume Zeros

( 0 1 0 1 1 0 . 0 1 0 )2

(1 6 . 4 )16

Works both ways (Octal to Hex & Hex to Octal)


EXERICSE – CONVERT...
Hexa-
Decimal Binary Octal decimal
29.8
101.1101
3.07
C.82

Don’t use a calculator!

Skip answer Answer


Exercise – Convert …
Answer

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

You might also like