Unit - II: Computer Arithmetic
Unit - II: Computer Arithmetic
Computer Arithmetic
01-08-2013
Introduction
Data Representation
Fixed Point Representation
Floating Point Representation
IEEE format Floating Point
Summarize.
Questions?
Data Representation
Data Representation
Data Types
Complements
Fixed Point Representations
Floating Point Representations
Character Codes
Data types
Computer is dealing with Instructions( operation or
control words) and Data (operands)
Data types are classified as:
1. Numbers used in arithmetic computations[Numeric]
2. Letters of alphabets used in data processing [Non
Numeric]
3. Other discrete symbols used for specific
purposes[Non numeric]
Basic Format
Data
- Numeric Data deals with Numbers( Integer, real)
a) Fixed Point
- Binary
- Decimal
b) Floating Point
- Binary
- Decimal
- Non-numeric Data deals with Letters, Symbols
n 1
a R
i m
Signed Magnitude
Ones Complement
Twos Complement
Sign-and-Magnitude
Negative numbers are usually written by
writing a minus sign in front.
Example:
- (12)10 , - (1100)2
Negative Numbers:
Sign-and-Magnitude (Cont)
Example: an 8-bit number can have 1-bit sign and 7bit magnitude.
sign
magnitude
Examples:
11012 = 1310
01002 = 410
0 0100 = +410
1 0100 = -410
12
Negative Numbers:
Sign-and-Magnitude (Cont)
To negate a number, just invert the sign bit.
Examples:
- (0 0100001)sm = (1 0100001)sm
- (1 0000101)sm = (0 0000101)sm
Overflow in signed magnitude numbers occur when the result occupy more number of bits
Example:
-7= 1 111
-4 =1 100
1011 [result occupies 4 bit hence overflow]
1s and 2s Complement
Two other ways of representing signed
numbers for binary numbers are:
1s-complement
2s-complement
Ones complement
representation
0 0100 = +410
1 1011 = -410
15
1 = 0, and 1 - 1 = 0
16
1s Complement Addition/Subtraction
1.
2.
3.
1.
2.
Twos complement
1 0011 = -1310
01002 = 410
0 0100 = +410
1 1011 = -410
1 1100 = -410
20
2s Complement Addition/Subtraction
1.
2.
3.
1.
2.
2s Complement Addition/Subtraction
0011
+ 0100
------0111
-------
-2
+ -6
----8
----
1110
+ 1010
------11000
-------
+6
+ -3
---+3
----
0110
+ 1101
------10011
-------
+4
+ -7
----3
----
0100
+ 1001
------1101
-------
Overflow
If the numbers are unsigned, overflow occurs when there
is a carry out of the most significant bit
For signed numbers, overflow detected by comparing the
sign of the result against the sign of the numbers
If one number is positive and another is negative,
overflow cannot occur
If both are positive or both are negative, compare the
carry into the sign bit and carry out of the sign bit
If these two carries are not equal, then there is an
overflow
Signed overflow
With twos complement and a 4-bit adder, for example, the largest
represent able decimal number is +7, and the smallest is -8.
What if you try to compute 4 + 5, or (-4) + (-5)?
0100
+ 0101
01001
(+4)
(+5)
(-7)
11 0 0 (-4)
+ 1 0 1 1 (-5)
1 0111
(+7)
We cannot just include the carry out to produce a five-digit result, as for
unsigned addition. If we did, (-4) + (-5) would result in +23!
Also, unlike the case with unsigned numbers, the carry out cannot be
used to detect overflow.
The easiest way to detect signed overflow is to look at all the sign bits.
0100
+ 0101
01001
(+4)
(+5)
(-7)
1100
+ 1011
10111
(-4)
(-5)
(+7)
25
Signed Overflow
carry generated, but no overflow
Decimal
S.M.
1s comp.
2s comp.
7
6
5
4
3
2
1
0
-0
-1
-2
-3
-4
-5
-6
-7
-8
0111
0110
0101
0100
0011
0010
0001
0000
1000
1001
1010
1011
1100
1101
1110
1111
0111
0110
0101
0100
0011
0010
0001
0000
1111
1110
1101
1100
1011
1010
1001
1000
0111
0110
0101
0100
0011
0010
0001
0000
1111
1110
1101
1100
1011
1010
1001
1000
27
How many negative and positive numbers can be represented in each of the
different systems on the previous page?
Smallest
Largest
Unsigned
Signed
Magnitude
Ones
complement
Twos
complement
0000 (0)
1111 (15)
1111 (-7)
0111 (+7)
1000 (-7)
0111 (+7)
1000 (-8)
0111 (+7)
In general, with n-bit numbers including the sign, the ranges are:
Smallest
Largest
Unsigned
Signed
Magnitude
Ones
complement
Twos
complement
0
2n-1
-(2n-1-1)
+(2n-1-1)
-(2n-1-1)
+(2n-1-1)
-2n-1
+(2n-1-1)
28
2 representations for 0
Simple
Need to consider both sign and magnitude in arithmetic
Different logic for addition and subtraction
1s complement representation:
2 representations for 0
Complexity in performing addition and subtraction
2s complement representation:
Only one representation for 0
Arithmetic works easily
Negating is fairly easy
Decimal Value
V(F) = V(M) * RV(E)
M: Mantissa
E: Exponent
R: Radix
sign
0
.1234567
mantissa
==> +.1234567 x 10+04
sign
0
04
exponent
Note:
In Floating Point Number representation, only Mantissa(M) and
Exponent(E) are explicitly represented. The Radix(R) and the position
of the Radix Point are implied.
Example
A binary number +1001.11 in 16-bit floating point number representation
(6-bit exponent and 10-bit fractional mantissa)
0
or
Sign
0 00100 100111000
Exponent
Mantissa
0 00101 010011100
Normal Form
- There are many different floating point number representations of
the same number
Need for a unified representation in a given computer
- the most significant position of the mantissa contains a non-zero digit
Biased Exponent
All negative exponent are represented with a +ve
Quantity by adding a bias value
Single Precision
Bias Value = +127
Double Precision
Bias Value = +1023
Advantage: Exponents contain only +ve value.
Simple to compare the magnitudes.
IEEE-754 Examples
LSB
(4 bits)
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
NUL
SOH
STX
ETX
EOT
ENQ
ACK
BEL
BS
HT
LF
VT
FF
CR
SO
SI
DLE
DC1
DC2
DC3
DC4
NAK
SYN
ETB
CAN
EM
SUB
ESC
FS
GS
RS
US
SP
!
#
$
%
&
(
)
*
+
,
.
/
0
1
2
3
4
5
6
7
8
9
:
;
<
=
>
?
@
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
[
\
]
m
n
a
b
c
d
e
f
g
h
I
j
k
l
m
n
o
7
P
q
r
s
t
u
v
w
x
y
z
{
|
}
~
DEL
Null
Start of Heading (CC)
Start of Text (CC)
End of Text (CC)
End of Transmission (CC)
Enquiry (CC)
Acknowledge (CC)
Bell
Backspace (FE)
Horizontal Tab. (FE)
Line Feed (FE)
Vertical Tab. (FE)
Form Feed (FE)
Carriage Return (FE)
Shift Out
Shift In
Data Link Escape (CC)
DC1
DC2
DC3
DC4
NAK
SYN
ETB
CAN
EM
SUB
ESC
FS
GS
RS
US
DEL
Device Control 1
Device Control 2
Device Control 3
Device Control 4
Negative Acknowledge (CC)
Synchronous Idle (CC)
End of Transmission Block (CC)
Cancel
End of Medium
Substitute
Escape
File Separator (IS)
Group Separator (IS)
Record Separator (IS)
Unit Separator (IS)
Delete