Binary Subtraction using 1`s and 2`s Complement

Download Report

Transcript Binary Subtraction using 1`s and 2`s Complement

Introduction to Computer Science
Dr. Nagy Ramadan
E-mail: [email protected]
Lecture - 4
1
Outline

Part 1 - Binary Addition

Part 2 - Binary Subtraction

Part 3 - Subtraction Using the Complement Method

Part 4 - Decimal Subtraction using 9’s and 10’s
Complement

Part 5 - Binary Subtraction using 1’s and 2’s Complement
2
Binary Addition
Rules for binary addition are:
0
0
1
1
1
+
+
+
+
+
0
1
0
1
1
= 0
= 1
= 1
= 0 with 1 to carry for the next column
+ 1 = 1 with 1 to carry for the next column
Ex 1: Find the sum of the binary numbers 1101 & 110 and verify the result
using decimal numbers
Solution:
(10011)2 = 1*24 + 0*23 + 0*22 + 1*21 + 1*20 = (19)10
3
Ex2: Perform the following binary addition operation then verify the result
using decimal numbers:
110101.101 + 10110.111
Solution:
(1001100.1)2 = 1*26 + 1*23 + 1*22 + 1*2-1
= 64 + 8 + 4 + 0.5 = (76.5)10
4
Binary Subtraction
Rules for binary subtraction are:
0–0=0
1–0=1
1–1=0
0 – 1 = 1 , with 1 borrowed from the next column
Ex 1: Use the direct binary subtraction to get the result of:
1100101 – 100111
Verify the result in decimal system.
Solution:
(10011)2 = 1*24 + 1*21 + 1*20 = (19)10
5
Subtraction Using the Complement Method
The “complement method” allows performing binary
subtraction in the form of binary addition which is
much easier. This greatly simplifies the design of
the electronic circuits of the digital computers.
Examples:

Decimal Subtraction using 9’s and 10’s Complement

Binary Subtraction using 1’s and 2’s Complement
6
Decimal Subtraction using 9’s and 10’s Complement
Ex 1: Decimal subtraction using 9’s complement
Solution:
The number 6832 is the 9’s complement of 3167
7
Ex 2: Decimal subtraction using 10’s complement
Solution:
The 10’s complement = the 9’s complement + 1
The number 6833 is the 9’s complement + 1 of the number
3167. Therefore, it is called the 10’s complement.
8
Binary Subtraction using 1’s and 2’s Complement

The 1’s complement of a binary number is simply obtained
by replacing every 1 by 0 , and every 0 by 1.

The 2’s complement of a binary number can be obtained in
two ways:

By adding 1 to the 1’s complement.

Start the binary number from right. Leave the binary
digits unchanged until the first 1 appear, then replace
every 1 by 0 , and every 0 by 1.
9
Ex 1: Obtain the two’s complement of the binary number 1011010.110
First solution
Second solution
10
Ex 2: Calculate the following binary Subtraction: 11101.101 – 1011.11 ,
then verify the result in decimal System.
Solution
(10001.111)2 = 16 + 1 + 0.5 + 0.125 = (17. 875)10
11
Important Note:

When using the complement methods in subtraction and
having no additional 1 in the extreme left cell, then , this
means a negative result.

In this case, the solution is the negative of
1’s
complement of the result (if using 1’s complement
initially), or the negative of 2’s complement of the result
(if using 2’s complement initially).
12
Ex 2: Calculate the following binary Subtraction: 1101.101 – 11011.11 ,
then verify the result in decimal System.
Solution
Therefore, the result = - (the 1’s complement of 10001.110) = - 01110.001
Or = - ( the 2’s complement of 10001.111 ) = - 01110.001
13
Questions
14