0% found this document useful (0 votes)
205 views21 pages

ICT Lec-10,11 Computer Arithmetic

Computer arithmetic uses binary numbers rather than decimal. Binary addition, subtraction, multiplication, and division follow specific rules. To represent negative numbers, binary uses signed magnitude, one's complement, and two's complement methods. One's complement complements all bits, while two's complement adds 1 to the one's complement. Binary subtraction is performed by taking the one's complement of the number being subtracted and adding it to the original number.

Uploaded by

Shoaib Kareem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
205 views21 pages

ICT Lec-10,11 Computer Arithmetic

Computer arithmetic uses binary numbers rather than decimal. Binary addition, subtraction, multiplication, and division follow specific rules. To represent negative numbers, binary uses signed magnitude, one's complement, and two's complement methods. One's complement complements all bits, while two's complement adds 1 to the one's complement. Binary subtraction is performed by taking the one's complement of the number being subtracted and adding it to the original number.

Uploaded by

Shoaib Kareem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

CSC101

Introduction to ICT

Muhammad Sharjeel
[email protected]

1
Lecture - 7

Computer Arithmetic

2
Binary vs Decimal
Information is handled in a computer by electronic components
Electronic components operate in binary mode (can have only two states ‘on’ or ‘off’)
The binary number system has only two digits (0 and 1) and is best suitable for expressing these two possible
states
Simple circuit design
Less expensive
More reliable circuits

Binary Arithmetic
Binary arithmetic is simple to learn as binary number system has only two digits
There are rules defined for the four basic arithmetic operations (+, -, x and ÷) using the binary
numbers

3
Binary Addition
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0 Plus a carry of 1 to the next higher column

Examples:
1 1 1 1 1 1 1

1 0 0 1 1 1 0 0 1 1 1

+ 1 0 0 1 + 1 1 0 1 1

1 1 1 0 0 1 0 0 0 0 1 0

* The addition of three or more 1s can be broken up into further steps. First, we add first two digits. The third digit is added to the
result of first addition and so on.
4
Binary Subtraction
0 - 0 = 0
0 - 1 = 1 With a borrow from the next column
1 - 0 = 1
1 - 1 = 0

Examples:

0 10 0 1 1

1 0 1 1 10 1 1 10 10 10 1 0 1

- 1 0 0 1 1 1 - 1 0 1 1 0 0

0 0 0 1 1 0 0 0 1 1 0 0 1

5
Binary Multiplication
0 x 0 = 0
0 x 1 = 0
1 x 0 = 0
1 x 1 = 1

Example:
1 0 1 0
x 1 0 0 1
1 0 1 0

0 0 0 0 -
0 0 0 0 - -
1 0 1 0 - - -

1 0 1 1 0 1 0

6
Binary Division
0 ÷ 0 = Divide by zero error
0 ÷ 1 = 0
1 ÷ 0 = Divide by zero error
1 ÷ 1 = 1

Examples:

0 1 1 1 0 0 1 1 0

1 1 1 0 1 1 1 1 0 1 1 1 0 0 1 1 0
1 1 0
1 1
1 0 0 1
1 0 1 1 1 0

1 1 1 1 1
1 1 0
1 0
1 0
7
Negative Numbers in Binary
We have seen positive numbers representation till now, how would negative numbers be represented in the
binary number system?

To represent negative number, in the binary number system, three methods;


Signed Magnitude
One’s Complement
Two’s Complement

8
Negative Numbers in Binary
Signed Magnitude
Use the leftmost digit as a sign indication, and treat the remaining bits as if they represented an unsigned
number
The convention is that, if the leftmost digit (most significant bit) is 0 the number is positive, if it’s 1 the
number is negative

Example:
(00001101)2 = (13)10
(10001101)2 = (-13)10

9
Negative Numbers in Binary
Signed Magnitude
A major drawback --- it doesn’t support binary arithmetic

Example:
Let’s add (10)10 and (-10)10 represented by ‘Signed Magnitude’ in the binary number system

00001010
+ 10001010
10010100 = (-20)10
x

10
Negative Numbers in Binary
One’s Complement
To obtain one’s complement, simply flip all the bits (digits) of a binary number
Suppose we have a binary number, 00001010
It’s one complement would be, 11110101
Notice that the complement is (245)10, which is (255)10 – (10)10.

That’s no co-incidence
In general, complement of a number is the largest number represented with the number of bits available
minus the number itself
Since we are using 8 bits here, the maximum number represented is 255 (232 – 1), so the complement of
(10)10 will be (245)10

11
Negative Numbers in Binary
One’s Complement
One’s complement can be used to represent negative numbers in binary number system
The one’s complement form of a negative binary number is the complement of its positive counterpart, which
can be obtained by applying the NOT to the positive counterpart

One’s complement has two representations of 0


00000000 (+0) and 11111111 (−0)

12
Negative Numbers in Binary
One’s Complement

Example:
Let’s add (10)10 and (-10)10 in binary number system using one’s complement

(00001010)2 = (10)10
one’s complement of (00001010)2 = (11110101)2 = (-10)10

00001010
+11110101
1 1 1 1 1 1 1 1 = (0)10 ✓

13
Negative Numbers in Binary
One’s Complement

Example:
Let’s add (69)10 and (-38)10 in binary number system using one’s complement

(01000101)2 = (69)10
(00100110)2 = (38)10
one’s complement of (00100110)2 = (11011001)2 = (-38)10

01000101
+11011001
1 0 0 0 1 1 1 1 0 = (286)10
x
14
Negative Numbers in Binary
One’s Complement

Example:
Let’s add (3)10 and (-2)10 in binary number system using one’s complement

(00000011)2 = (3)10
(00000010)2 = (2)10
one’s complement of (00000010)2 = (11111101)2 = (-2)10

00000011
+11111101
1 0 0 0 0 0 0 0 0 = (256)10
x
15
Negative Numbers in Binary
One’s Complement
So, it partially solves the binary arithmetic problem but there are some special cases left

Two’s Complement
Two's complement representation allows the use of binary arithmetic operations on signed integers, yielding
the correct results
Two’s complement of a binary number can be obtained by adding 1 to the one’s complement of that
number
(12)10 = (00001100)2
one's complement of (00001100)2 = (11110011)2
11110011
+00000001
11110100 (two's complement of (00001100)2)

16
Negative Numbers in Binary
Two’s Complement

Example:
Let’s add (12)10 with (-5)10 in binary number system using two’s complement

(12)10 = (00001100)2
(5)10 = (00000101)2
two’s complement of (00000101)2 = (11111011)2

00001100
+11111011
0 0 0 0 0 1 1 1 = (7)10

* How to add two negative numbers? e.g., (-5)10 + (-2)10


17
Negative Numbers in Binary
Subtraction using one’s complement

Find the one’s complement of the number you are subtracting


Add this to the number from which you are taking away

Two scenarios;
1. If there is a carry of 1, add it to obtain the result
2. if there is no carry, re-complement the sum and attach a negative sign

18
Negative Numbers in Binary
Scenario 1: If there is a carry
Example:
Subtract (0111000)2 from (1011100)2
One’s complement of (0111000)2 is (1000111)2

1 0 1 1 1 0 0
+ 1 0 0 0 1 1 1
1 0 1 0 0 0 1 1
+ 1
0 1 0 0 1 0 0

19
Negative Numbers in Binary
Scenario 2: If there is no carry
Example:
Subtract (100011)2 from (010010)2
One’s complement of (100011)2 is (011100)2

0 1 0 0 1 0
+ 0 1 1 1 0 0
1 0 1 1 1 0

Re-complement the answer


one’s complement of (101110)2 is (010001)2
Append a negative sign to the final answer
result = - 010001
20
THANK YOU

21

You might also like