Digital Logic�Lecture 3�� �Binary Arithmetic
By
Zyad Dwekat
The Hashemite University
Computer Engineering Department
The Hashemite University
2
Outline
The Hashemite University
3
Introduction
The Hashemite University
4
Numbers in Computers
The Hashemite University
5
Signed Binary Numbers
The Hashemite University
6
Signed Binary Numbers Representation
The Hashemite University
7
Complements
The Hashemite University
8
Diminished Radix Complement
(r – 1)’s complement = (rn - 1) – N
The Hashemite University
9
Binary 1’s Complement – Simple Rule
Number | 1’s complement |
01101 | 10010 |
11010001 | 00101110 |
01 | 10 |
The Hashemite University
10
Floating Point Diminished Radix Complement
The Hashemite University
11
Floating Point Diminished Radix Complement -- Examples
Number | (r – 1)’s Complement |
101.12 | 🡪 10112 1’s Complement = 01002 🡪 (final result) 01.002 |
11101.112 | 🡪 11101112 1’s Complement = 00010002 🡪 (final result) 10.002 |
The Hashemite University
12
Radix Complement
r’s complement = rn – N
r’s complement = 1 + (r - 1)’s complement
The Hashemite University
13
Radix Complement -- Examples
Number | r’s Complement |
10010002 | 01110002 |
11101.112 | 🡪 11101112 2’s Complement = 00010012 🡪 (final result) 10.012 |
The Hashemite University
14
Remember ...
The Hashemite University
15
Number Ranges I
The Hashemite University
16
Number Ranges II
The Hashemite University
17
Number Format and Arithmetic Operations
The Hashemite University
18
Arithmetic Operations -- Notes
The Hashemite University
19
Sign Extension Concept I
11101 + 00111010
The Hashemite University
20
Sign Extension Concept II
11101 + 00111010
The Hashemite University
21
Overflow
The Hashemite University
22
Overflow Detection
The Hashemite University
23
Binary Addition and subtraction
The Hashemite University
24
Binary Addition – Basic
0
+ 0
0
0
+ 1
1
1
+ 0
1
1
+ 1
1 0
Carry Bit
(a)
(b)
(c)
(d)
1
+ 1
1
11
(e)
Carry Bit
Remember Counting Principles
The Hashemite University
25
Unsigned Binary Addition --Examples
10011001
+ 00101100
11000101
0101
+ 1001
1110
1011
+ 0101
10000
1010
+ 0100
1110
1011
+ 1100
10111
(a)
(b)
(c)
(d)
(e)
All calculations are right, no overflow, (Cout = 0)!!
The Hashemite University
26
Unsigned Addition – Other Systems
The Hashemite University
27
Unsigned Binary Subtraction I
A – B = A + (-B)
The Hashemite University
28
Unsigned Binary Subtraction II
The Hashemite University
29
r’s Complement Unsigned Subtraction
The Hashemite University
30
Examples I
Ex. 1: Compute 2510 – 2510 using 2’s complement, n = 8.
000110012 = (2510)
+ 111001112 = (-2510)
---------------------------
1000000002 = (010)
discard
The calculation is right, no overflow, (Cin = Cout)!!
The Hashemite University
31
Examples II
Ex: Compute 11510 – 3910 using 2’s complement, n = 8.
11510 - 3910 = 11510 + (-3910)
-3910 = 110110012 (2’s)
011100112 = (11510)
+ 110110012 = (-3910)
---------------------------
1010011002 = (7610)
discard
The calculation is right, no overflow, (Cin = Cout)!!
The Hashemite University
32
Examples III
Ex. 3: Compute 01012 – 11002 using 2’s complement, n = 4.
– 11002 = 01002 (2’s)
01012
+ 01002
-------------
10012
The calculation is wrong !! Cin != Cout, Overflow
The Hashemite University
33
Examples IV
Ex. 4: Compute 00012 – 01102 using 2’s complement, n = 4.
– 01102 = 10102 (2’s)
00012
+ 10102
-------------
10112 => no carry out
The answer is the 2’s complement of 1011= - (0101)
The calculation is right, no overflow, (Cin = Cout)!!
The Hashemite University
34
(r – 1)’s Complement Unsigned Subtraction
The Hashemite University
35
Examples I
Ex. 1: Compute 2510 – 2510 using 1’s complement, n = 8.
000110012 = (2510)
+ 111001102 = (-2510)
---------------------------
111111112
Final answer = - (1’s of 111111112) = - 000000002
The calculation is right, no overflow, (Cin = Cout)!!
The Hashemite University
36
Examples II
Ex. 2: Compute 11510 – 3910 using 1’s complement, n = 8.
11510 - 3910 = 11510 + (-3910)
-3910 = 110110002 (1’s)
011100112 = (11510)
+ 110110002 = (-3910)
---------------------------
1010010112
+ 12
---------------------------
010011002 = (7610)
discard
The calculation is right, no overflow, (Cin = Cout)!!
The Hashemite University
37
Examples III
Ex. 3: Compute 01012 – 11002 using 1’s complement, n = 4.
– 11002 = 00112 (1’s)
01012
+ 00112
-------------
10002 🡪 No carry out
The calculation is wrong !! Cin != Cout, Overflow
The Hashemite University
38
Example IV
Ex. 4: Compute 112 – 10.112 using 2’s complement where n = 8.
– 00000010.112 = 11111101.012 (2’s)
00000011.002
+ 11111101.012
-------------
100000000.012
discard
The calculation is right, no overflow, (Cin = Cout)!!
The Hashemite University
39
Examples V
Ex. 5: Compute 00012 – 01102 using 1’s complement, n = 4.
– 01102 = 10012 (1’s)
00012
+ 10012
-------------
10102 => no carry out
The answer is the 1’s complement of 1011= - (0100)
The calculation is right, no overflow, (Cin = Cout)!!
The Hashemite University
40
Signed Binary Arithmetic
The Hashemite University
41
2’s Complement Signed Binary Addition
The Hashemite University
42
1’s Complement Signed Binary Addition
The Hashemite University
43
Signed Binary Subtraction
The Hashemite University
44
Example I
Find the following using 2’s complement (signed):
115 - 39 = 115 + (-39)
First: convert into binary
115(10) = 01110011(2) (2‘s)
-39(10) = 11011001(2) (2’s)
Second: perform subtraction using 2’s complement:
01110011 = (115)
+ 11011001 = (-39)
---------------------------
101001100 = (76)
discard
The calculation is right, no overflow, (Cin = Cout)!!
The Hashemite University
45
Example II
Find the following using 2’s complement (signed):
68 + 39
First: convert into binary:
68(10) = 01000100(2) (2‘s)
99(10) = 01100011(2) (2’s)
Second: add using 2’s complement:
01000100 = (68)
+ 01100011 = (99)
---------------------------
10100111 = (-89)
The calculation is wrong !! Cin != Cout
The actual answer (i.e. 167) exceeds the largest limit (i.e. 127) =>OVERFLOW
The Hashemite University
46
Example III
Find the following using 2’s complement (signed):
-78 - 52 = (-78) + (-52)
First: convert into binary:
-78(10) = 10110010(2) (2‘s)
-52(10) = 11001100(2) (2’s)
Second: subtract using 2’s complement :
10110010 = (-78)
+ 11001100 = (-52)
---------------------------
101111110 = (126)
The calculation is wrong !! Cin != Cout overflow
discard
The Hashemite University
47
Examples IV
Ex. : Compute 00012 – 01102 using 1’s complement, n = 4, signed.
– 01102 = 10012 (1’s)
00012
+ 10012
-------------
10102 => no carry out
The answer is 1011
The calculation is right, no overflow, (Cin = Cout)!!
The Hashemite University
48
Examples V
Ex. 5: Compute 00012 – 01102 using 2’s complement, n = 4, signed.
– 01102 = 10102 (2’s)
00012
+ 10102
-------------
10112 => no carry out
The answer is 1011
The calculation is right, no overflow, (Cin = Cout)!!
The Hashemite University
49
Additional Notes