Rimon_Ater_Integers

advertisement
5.1 Introduction
Sum 111111 (+63)
Binary arithmetic is essential for performing
arithmetic operation in computer
--------
systems. To understand what is going on in the
computer’s arithmetic logic
unit(ALU), the basics of binary addition,
subtraction, multiplication , and division
5.3 Binary Subtraction
operations must be understood. These
operations can only be understood if we get
familiar with the storage of number in
computer memory.
Binary Addition
Binary addition is exactly same as decimal
addition, except the rules. The binary
addition rules are shown in Table 5.1
Example 5.1 Add (110101)2 with (101110)2
Carry
Augend 010011 (+19)
Addend 101100 (+44)
---------
5.3.1 Unsigned Numbers
Likewise in the decimal system we subtract
decimal digit from a smaller digit by
The method which has been described till now
for binary subtraction is not well
borrowing from next column, the same rule can
be adopted in binary subtraction also.
suited for the computer. If this method is
adopted we must have separate
The rules for binary subtraction are shown in
Table 5.2
algorithm for subtraction. As you know that
subtraction of b from a means
addition of (-b) to a.
Table 5.2
In computer, binary subtraction is transferred
into addition by using:
A
(Minuend)
a. One’s complement method.
b. Two’s complement method.
B
The advantage is that we could use a single
algorithm to implement addition
(Subtrahend)
as well as subtraction. In general there are
(radix-1)’s complement and radix’s
A-B
complement. 1’s and 2’s complement of
positive numbers is identical to sign
magnitude.
(Difference)
Methods to represent negative numbers
Borrow
The three widely used methods to represent
negative numbers:
Signed Numbers
a. Signed bit magnitude representation
b. 1’s complement representation
c. 2’s complement representation
a) Represent the magnitude of given number in
binary.
5.3.2.1 Singed bit magnitude representation
b) Replace 0 by 1 and 1 by 0 in binary number
obtained in step (a)
In this method we use one extra bit as most
significant bit(MSB), 0 for positive
number , 1 for negative numbers.
For example:
For example: One’s complement of -20
Binary representation of 20 10100 step(a)
-12 1 1010
Replace 0 by 1 and 1 by 0 01011 step(b) [One’s
complement of -20]
+12 0 1010
5.3.2.3 Two’s Complement Method
5.3.2.2 One’s Complement Method
This method involves following steps:
Bit complementing: It is the process of replacing
0 by 1 and 1 by 0 in a binary
a) Represent the magnitude of given number in
binary.
number.
Bit complement of 0011100011 is 11100011100
.
b)Scan the number so obtained in step a) from
RHS to LHS, replace 0 by 1 and 1 by 0
after the first occurrence of 1.
For example: Two’s complement of -20
This method involves following steps:
Binary representation of 20 10100 step(a)
Replace 0 by 1 and 1 by 0 01100 step(b) [Two’s
complement of -20]
after the first occurrence of 1
Second Approach
a) Represent the number to its one’s
complement.
5.3.2.4 Subtraction using 1’s Complement
The rules of subtraction by this method are as
follow:
i) Write the 1’s complement of both subtrahend
and minuend.
ii) Add the numbers using the binary addition
rule.
iii) If the result overflow then the overflow bit is
removed and added to the
b) Add 1 to its one’s complement.
result
For example: Two’s complement of -20
iv) If there is no overflow then the result is recomplemented and a sign bit is
attached to it to obtain the final result.
Binary representation of +20 10100
Replace 0 by 1 and 1 by 0 01011 step(a) [One’s
complement of -20]
Add 1 1
--------
01100
-------
Example 5.4
Subtract (-3) from 5 using 1’s complement
method:
If one of the numbers say multiplicand or
multiplier is negative then the rules of
multiplication by this method are as follow:
Carry 1
Minuend (+5) 0101 (1’s complement of +5)
Subtrahend (-3) 1100 (1’s complement of -3)
------
0 0001
| __ 1
---------
---------
(+30) 11110 (Product)
---------
5.4.2 Multiplication of Signed Numbers
i) Write the 2’s complement of both subtrahend
and minuend.
ii) Multiply the numbers using the binary
multiplication rule with leading bits of
Let us assume we have hypothetical 8 digit
computer out of which four digits are
partial products are made 1 or 0 depending
upon multiplication is taken with 1 or 0
used for mantissa and two digits are used for
exponent with a provision of sign of
respectively.
mantissa and sign of exponent.
iii) All partial products are added using binary
addition rule to get final result.
Assumed decimal position
Figure 5.1 Fixed point representation in
Memory
This representation is called fixed point
representation, since the position of
decimal point is fixed after 6 positions from left.
In such a representation largest
positive number we can store 999999.99 and
smallest positive number we can
store 000000.01. This range is quite inadequate.
In general floating representation of a number
of any base may be written as:
N = ±Mantissa x (Base) ±exponent
number in computer memory (with four digit
mantissa)
Implied decimal point
±
Figure 5.2 Floating point representation in
memory(4 digit mantissa)
Normalized Floating Representation
It has been noted that a number may have
more than one floating point representations.
In
order to have unique representation of nonzero numbers a normalized floating point
representation is used.
±
A floating point representation in decimal
number system is normalized floating point iff
mantissa is less than 1 and greater than equal
to .1 or 1/10(base of decimal number
system).
i.e.
.1= |mantissa| <1
±
A floating point representation in binary
number system is normalized floating point iff
mantissa is less than 1 and greater than equal
to .5 or 1/2(base of binary number system).
i.e.
.5= |mantissa| <1
In general, a floating point representation is
called normalized floating point
representation iff mantissa lies in the range:
Sign of Implied
1/base= |mantissa| <1
Mantissa Decimal point
Representation of normalized floating point
number in computer memory with four digit
Figure 5.3 Normalized floating point
representation in memory(4 digit mantissa)
mantissa:
±
Note : In computer, storage of floating point
numbers is taken place in normalized
form.
Mantissa
Mantissa
Disadvantages of floating point representation
• All the eight digits cannot be stored, since two
digits are required by exponent.
Exponent
• Some specific rules are to be followed when
arithmetic operations are performed
with such numbers.
5.6.3 Arithmetic operations with Normalized
Floating Point Numbers
5.6.3.1 Addition
Mantissa
Exponent
Sign of
exponent
Sign of
Sign of
exponent
For adding two normalized floating point
numbers following rules are to be
followed:
Sum . 8023E05
-----------
a) Their exponents are to be made same if they
are not same.
b) Add their mantissa to get the mantissa of
resultant.
Example5.18
c) Result is written in normalized floating point
number.
Add .3456E05 and .5456E07
Sol. Here exponents are not equal, therefore
firstly make exponents same such that
d) Check the overflow condition.
mantissa of number with smaller exponent
sifted towards R.H.S. equal to the number
Example 5.17
of digits smaller exponent less than with larger
exponent i.e. 7-5=2.
Add .4567E05 to .3456E05
Sol. Here exponents are equal, we have to add
only mantissa and exponent remains
.3456E05 .. .0034E07 Addend
.5456E07 Augend
unchanged.
----------Addend .3456E05
.5490E07 Sum
Augend .4567E05
--------------------Example 5.19
Sol
unchanged.
Add .3456E03 and .5456E07
.3456E05 Addend
Sol. Here exponents are not equal, therefore
firstly make exponents same such that
.7567E05 Augend
mantissa of number with smaller exponent
sifted towards R.H.S. equal to the number
of digits smaller exponent less than with larger
exponent i.e. 7-3=4.
-----------
1.1023E05->.1102E06 Sum (Last digit of
mantissa is chopped)
.3456E03 .. .0000E07 Addend
----------.5456E07 Augend
Example 5.21
-----------
.5456E07 Sum
-----------
Example 5.20
Add .3456E03 and .7567E05
Sol. Here exponents are equal, we have to add
only mantissa and exponent remains
Add .3456E99 and .7567E99
Sol. Here exponents are equal, we have to add
only mantissa and exponent remains
unchanged.
Rules to subtract a number from other are as
follows:
a. Their exponents are to be made same if they
are not same.
b. Subtract mantissa of one number from other
to get the mantissa of resultant.
.3456E99 Addend
.7567E99 Augend
c. Result is written in normalized floating point
number.
----------d. Check the underflow condition
1.1023E05->.1102E100 Sum (Last digit of
mantissa is chopped) OVERFLOW
-----------
Example 5.22
As per exponent part can not store more than
two digits, the number is larger than the
Subtract .3456E05 from .4567E05
largest number that can be stored in a memory
location. This condition is called
overflow condition and computer will intimate
an error condition.
Sol. Here exponents are equal, we have to
subtract mantissa and exponents remain
unchanged.
.4567E05 Minuend
5.6.3.2. Subtraction
.3456E05 Subtrahend
-----------
-----------
.1111E05 Difference
Example 5.24
-----------
Subtract .3456E03 from .5433E07
Sol. Here exponents are not equal, therefore
firstly make exponents same such that
Example 5.23
mantissa of number with smaller exponent
sifted towards R.H.S. equal to the number
Subtract .3456E05 from .5456E07
of digits smaller exponent less than with larger
exponent i.e. 7-3=4.
Sol. Here exponents are not equal, therefore
firstly make exponents same such that
.5433E07
mantissa of number with smaller exponent
sifted towards R.H.S. equal to the number
.3456E03 .. .0000E07
of digits smaller exponent less than with larger
exponent i.e. 7-5=2.
.5456E07
.3456E05 .. .0034E07
-----------
.5422E07
-----------
Example 5.26
.5433E07
Subtract .5345E-99 from .5433E-99
-----------
Sol. Here exponents are equal, we have to
subtract only mantissa and exponent
Example 5.25
Subtract .5345E05 from .5433E05
Sol. Here exponents are equal, we have to
subtract only mantissa and exponent
remains unchanged.
.5433E-99
.5345E-99
remains unchanged.
------------
.5433E05
.0088E-99 -> .8800E-101 (UNDERFLOW)
.5345E05
-----------
-----------
As per exponent part can not store more than
two digits, the number is smaller than
.0088E05->.8800E03
the smallest number that can be stored in a
memory location. This condition is called
-----------
underflow condition and computer will intimate
an error condition.
5.6.2.3 Multiplication
Product of mantissa
If two normalized floating point numbers are to
be multiplied following rules are
.4454 x .3456 = .1539302
followed:
a) Exponents are added to give exponent of the
product.
b) Mantissas of two numbers are multiplied to
give mantissa of the product.
Sum of exponents
c) Result is written in normalized form.
23-45 = -18
d) Check for overflow/underflow condition.
Product is .1539E-18
(m1 x 10e1 ) x (m2 x10e2 ) =
(m1xm2)x10(e1+e2)
Discarded
Example 5.27 Find the product of following
normalized floating point representation
with 4 digit mantissa.
.4454E23 and .3456E-45
Sol.
Example 5.28
Sum of exponent
Sol. .5000E04 x .4000E02 = .2000E02
23-45 = -18
Example 5.29
Sol.
Product is .0648502E-18 -> .648502E-19
.6543E05 x .2255E03 = .14754465E08
Resultant product is .6485E-19
= .1475E08 (digits 4465 of the mantissa are
discarded)
Example 5.30 Find the product of following
normalized floating point representation
Example 5.30 Find the product of following
normalized floating point representation
with 4 digit mantissa.
.4454E23 and .1456E-45
Sol.
Product of mantissa
.4454 x .3456 = .0648502
with 4 digit mantissa.
.4454E50 and .3456E51
Sol.
Product of mantissa
.4454 x .3456 = .1539302
Sum of exponent
5.6.2.4 Division
50+51 = 101
If two normalized floating point numbers are to
be divided following rules are to be
followed:
Product is .1539E101 (OVERFLOW)
a. Exponent of second number is subtracted
from first number to obtain of the
result.
As per exponent part can not store more than
two digits, the number is larger than the
b. Mantissas of first number is divided by
second number to obtain mantissa of
largest number that can be stored in a memory
location. This condition is called
the result
overflow condition and computer will intimate
an error condition.
c. Result is written in normalized form.
d. Check for overflow/underflow condition.
Product is .1539E-101 (UNDERFLOW)
(m1 x 10e1 ) ÷ (m2 x10e2 ) = (m1÷m2)x10(e1e2)
As per exponent part can not store more than
two digits, the number is smaller than
the smallest number that can be stored in a
memory location. This condition is called
underflow condition and computer will intimate
an error condition.
Discarded
Example 5.32 Division of .8888E-05 by .2000 E 03
Sol. .8888E-05 ÷ .2000 E -03 = (.8888 ÷ .2000) E2
= 4.4440E-2 = .4444E-1
Input
Number
*100
5.6.3 Errors in number representation
A computer has finite word length and so only a
fixed number of digits are stored and
used during computation. This would mean that
even in storing an exact decimal
number in its converted form in the computer
memory, an error is introduced. This
error is machine dependent. After the
computation is over, the result in the machine
Note: For numbers close to 1, absolute error
and relative error are nearly equal.
For numbers not close to 1 there can be great
difference.
e) Inherent error
Error arises due to finite representation of
numbers.
form is again converted to decimal form
understandable to the users and some more
error may be introduced at this stage.
For example
1/3 = 0.333333 ……..
Error-2(e2) Error-1(e1) Output
Number+
2 = 1.414………
e1+e2
Error introduced
22/7 = 3.141592653589793…………..
when data stored
Error introduced
when information
retrieved
It is noticed that every arithmetic operation
performed during computation, gives rise
to some error, which once generated may
decay or grow in subsequent calculations.
In some cases error may grow so large as to
make the computed result totally
redundant and we call such a procedure
numerically unstable. In some case it can be
avoided by changing the calculation procedure,
which avoids subtractions of nearly
equal numbers or division by a small number or
discarded remaining digits of
mantissa.
5.7 Summary
In this lesson we have concerned about
representation of integer, floating
point numbers, and negative numbers in
computer memory and binary
arithmetic. It has to be remembered that binary
arithmetic is little concern to
programmer. On the other hand computers do
all the calculation in binary
arithmetic only.
Download